0% found this document useful (0 votes)
31 views34 pages

Chapter 4

Uploaded by

teseman2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views34 pages

Chapter 4

Uploaded by

teseman2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

CHAPTER FOUR

Function and Passing argument to function


Outlines
Definition of function
Declaration of function
Passing value of a function by Value
Passing value of a function by reference
12/15/2024 1

Other concepts of function


Definition of function
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
Functions are used to perform certain actions, and they are important for
reusing code.

Define the code once, and use it many times.

12/15/2024 2
Function

 Functions are building blocks of the programs.


 They make the programs more modular and easy to read and manage.
 All C++ programs must contain the function main( ).
 The execution of the program starts from the function main( ).
Dividing a complex problem into smaller chunks makes our program easy to
understand and reusable.
12/15/2024 3
There are two types of function:
1. Standard Library Functions: Predefined in C++
2. User-defined Function: Created by users
In this session, we will focus mostly on user-defined functions.
C++ allows the programmer to define their own function.
A user-defined function groups code to perform a specific task and that group
of code is given a name (identifier).
When the function is invoked from any part of the program, it all
executes the codes defined in the body of the 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

 Declared functions are not executed immediately. They are "saved


for later use", and will be executed later, when they are called.
 To call a function, write the function's name followed by two
parentheses () and a semicolon ;
 In the following example, myFunction() is used to print a text (the
action), when it is called:

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

 Information can be passed to functions as a parameter.


Parameters act as variables inside the function.
 Parameters are specified after the function name, inside the parentheses. You
can add as many parameters as you want, just separate them with a
comma:

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

• If multiple functions having same name but parameters of the functions


should be different is known as Function Overloading.
If we have to perform only one operation and having same name of
the functions increases the readability of the program.
Suppose you have to perform addition of the given numbers but there can
be any number of arguments, if you write the function such as a(int,int) for
two parameters, and b(int,int,int) for three parameters then it may be
difficult for you to understand the behavior of the function because
its name differs.
• Function overloading allows you to define multiple functions with the
same name but different parameters.
12/15/2024 23
Function overloading(cont…)
Parameters should have a different type
add(int a, int b)
add(double a, double b)
Parameters should have a different number
add(int a, int b)
add(int a, int b, int c)

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.

• Note: However, function overloading with different return types


and different parameter list is possible.(eg. if there are two functions:
int sum(int a, int b) and float sum(float a, float b ) can be overloaded.
12/15/2024 26
Note: In C++, function overloading is primarily based on
the function name and the types or number of its
parameters. The return type alone does not distinguish
overloaded functions.

Therefore, changing only the return type of a function


without changing its parameters does not create an
overload and may lead to a compilation error due to
ambiguity.
12/15/2024 27
Recursive Function
A function that calls itself is called a recursive function. When
a recursive function is called, it executes a set of instructions
and then calls itself to execute the same set of instructions with
a smaller input.
This process continues until a base case is reached, which is a
condition that stops the recursion and returns a value.

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

You might also like