Chapter 4
Chapter 4
12/15/2024 2
Function
12/15/2024 4
12/15/2024 5
function
declaration
A function declaration is made by declaring the
return type of the function, name of the function and the
data types of the parameters of the function.
Always terminated by semicolon.
The general form of function
declaration :- return_type
12/15/2024 6
function_name(parameter list);
The return_type specifies the type of the data
the function returns.
The parameter list could be empty .
The parameter list should contain both data
type and name of the variable.
For example,
int factorial(int n, float j);
12/15/2024 7
Defining a Function
The general form of a C++ function definition is as follows
return_type
function_name( parameter list )
{
body of the function
}
A C++ function definition consists of a function header
and a function body. Here are all the parts of a
function
12/15/2024 8
Return Type − is the data type of the value the function returns.
Function Name − This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
Parameters − A parameter is like a placeholder. When a function is invoked, you pass a
value to the parameter. This value is referred to as actual parameter or argument.
The parameter list refers to the type, order, and number of the parameters of a function.
Parameters are optional; that is, a function may contain no parameters.
Function Body − The function body contains a collection of statements that define what
the function does.
12/15/2024 9
Calling A Function
12/15/2024 10
void myFunction() {
cout << "I just got executed!";
}
int main() {
cout << "I from Computer Science!";
myFunction(); // call the function
return 0;
}
12/15/2024 11
Parameters and Arguments
12/15/2024 12
What is Argument?
The values that are declared within a function when the
function is called are known as an argument.
These values are considered as the root of the function that
needs the arguments while execution, and it is also known as
Actual arguments or Actual Parameters.
there are two ways that arguments can be passed to a function:
12/15/2024 13
12/15/2024 14
Example of call by value
When a parameter is passed by value, a copy of the
variable is made. Changes made to the parameter inside
the function do not affect the original variable.
Before modifyValue: 5
Inside modifyValue: 15
After modifyValue: 5
12/15/2024 15
Example of call by reference:
When a parameter is passed by reference, a reference (or alias) to the original variable is
used. Changes made to the parameter inside the function will affect the original variable.
• Before modifyValue: 5
Before modifyValue: 5
• Inside modifyValue: 15 Inside modifyValue: 15
After modifyValue: 15
• After modifyValue: 15
12/15/2024 16
What are Default Arguments?
A C++ default argument is a value in the C++ function declaration automatically assigned by
the compiler if the calling function does not pass any value to that argument.
However, if arguments are passed when the function is called, the default arguments are
ignored.
Rules for defining default arguments –
The values passed in the default arguments can be overwritten if a value is explicitly passed to the function.
During the function call, the values are copied starting from left to right.
Once a default value for an argument is given, all subsequent
arguments must also have default values.
12/15/2024 17
Working of Default Arguments
Let’s look at the following cases of a code snippet:
Here, when the function temp() is called, both the default arguments are used by the function.
12/15/2024 18
Here, when the function temp(4) is called, the first argument becomes 4,
while the second argument still uses the default value. 12/15/2024 19
Here, when the function temp(4, -3.5) is called, both the default arguments are
overridden by the values explicitly passed to the function inside main().
12/15/2024 20
In this case, when the function temp(3.5) is called, the function throws
an error because we are trying a pass a floating point number, which
should be the second argument, and it cannot be passed without
passing the first argument
12/15/2024 21
12/15/2024 22
Function overloading
12/15/2024 24
Function overloading(cont…)
Parameters should have a different sequence of
parameters.
add(int a, double b)
add(double a, int b)
• Function overloading is possible in C++ but only if the
functions must differ from each other by the types
and the number of arguments in the argument list.
However, functions can not be overloaded if they
differ only in the return type. 12/15/2024 25
Why is Function overloading not possible with
different return types and identical parameters?
• During compilation, the function signature is checked. So, functions can
be overloaded, if the signatures are not the same. The return type of a
function has no effect on function overloading, therefore the same
function signature with different return type will not be overloaded.
Example: if there are two functions: int sum() and float sum(), these
two will generate a compile-time error as function overloading is not
possible here.
12/15/2024 28
12/15/2024 29
The recursion continues until some condition is met.
To prevent infinite recursion, if...else statement (or
similar approach) can be used where one branch
makes the recursive call and the other doesn't.
12/15/2024 30
12/15/2024 31
Global versus local variables
12/15/2024 32
12/15/2024 33
12/15/2024 34