Prog II Chapter 2
Prog II Chapter 2
COMPUTER SCIENCES
Course Title: Computer 1
Programming II
Chapter 2:Functions
05/11/2025
OUTLINE
Introduction to Modular Programming (Functions);
Declaration of functions; Definition of functions
Calling a function
Scope of Variables
Function Arguments; Return Values
Default Parameters; Parameters passing; Call by
value; Call by reference
Recursive functions
05/11/2025 2
INTRODUCTION TO FUNCTIONS
05/11/2025 5
User-Defined Functions
C++ programs usually
have the following form:
// include statement
// function prototype
// main() function
// function definitions
05/11/2025 6
FUNCTIONS
In C++, a function is a group of statements that is given a name,
and which can be called from some point of the program.
Functions allow to structure programs in segments of code to
perform individual tasks.
The most common syntax to define a function is:
type name ( parameter1, parameter2, ...)
{
statements
}
Where:
- type : is the data type specifier of the data returned by the function.
- name is the identifier by which it will be possible to call the function
- parameters (as many as needed): Each parameter consists of a type followed by
an identifier, with each parameter being separated from the next by a comma.
-statements is the function's body. It is a block of statements surrounded by braces
{ }. 05/11/2025 7
DECLARING FUNCTIONS
05/11/2025 9
CALLING FUNCTION
A function call is an expression of the same type as the function and whose value
corresponds to the return value. The return value is commonly passed to a
suitable variable.
In this example the function pow()is first called using the arguments x and 3.0,
and the result, the power x3, is assigned to y.
As the function call represents a value, other operations are also possible.
Thus, the function pow() can be used to perform calculations for double values.
05/11/2025 10
CALLING FUNCTION CON…
05/11/2025 11
FUNCTION CATEGORIES
05/11/2025 12
FUNCTIONS CON…
}
We are passing values to the addition operation with two
parameters called X and Y.
Parameter is variable in the declaration of function.
Argument is the actual value of this variable that gets passed
05/11/2025 18
to function.
ARGUMENTS PASSED BY VALUE AND BY REFERENCE
05/11/2025 19
ARGUMENTS PASSED BY VALUE AND BY REFERENCE
05/11/2025 20
ARGUMENTS PASSED BY VALUE AND BY REFERENCE
05/11/2025 21
FUNCTION WITH NO RETURN VALUE AND NO ARGUMENT
No
Argumen
t
No
Return
05/11/2025 22
FUNCTION WILL NOT RETURN ANY VALUE BUT PASSES ARGUMENT
05/11/2025 23
DEFAULT VALUES IN PARAMETERS
05/11/2025 27
RECURSIVE FUNCTION
The function factorial we included a call to
itself, but only if the argument passed was
greater than 1, since, otherwise, the function
would perform an infinite recursive loop, in
which once it arrived to 0, it would continue
multiplying by all the negative numbers
(probably provoking a stack overflow at some
point during runtime).
05/11/2025 28
SCOPE OF VARIABLES
Scope is defined as the extent up to which something
can be worked with.
In programming also the scope of a variable is defined as
the extent of the program code within which the variable can
be accessed or declared or worked with.
scope of a variable is its lifetime in the program.
This means that the scope of a variable is the block of code
in the entire program where the variable is declared, used,
and can be modified.
There are mainly two types of variable scopes:
1.Local Variables
2.Global Variables
05/11/2025 29
GLOBAL VARIABLES
As the name suggests, Global Variables can be
accessed from any part of the program.
They are available through out the life time of a
program.
They are declared at the top of the program
outside all of the functions or blocks.
Declaring global variables: Global variables are
usually declared outside of all of the functions and
blocks, at the top of the program.
They can be accessed from any portion of the program.
05/11/2025 30
#DEMO GLOBAL VARIABLES
05/11/2025 31
LOCAL VARIABLES
Variables defined within a function or block
are said to be local to those functions.
Anything between ‘{‘ and ‘}’ is said to inside
a block.
Local variables do not exist outside the block
in which they are declared, i.e. they can
not be accessed or used outside that block.
Declaring local variables: Local variables
are declared inside a block.
05/11/2025 32
05/11/2025 33
END 05/11/2025 34