Functions
Mahit Kumar Paul
Assistant Professor, Dept. of CSE
RUET, Rajshahi-6204
[email protected]2023-11-10 1
Functions
• A function is a group of statements that together
perform a task.
• Example
int add(int x, int y)
{
int sum;
sum = x + y;
return sum;
}
2023-11-10 2
Functions…
• A function can be implemented in two ways:
• Function definition
• Function declaration
• A function definition provides the function header and
actual body of the function.
• A function declaration tells the compiler about a
function's name, return type, and parameters.
2023-11-10 3
Defining a Function
• A function definition consists of a function header and
a function body.
Syntax
return_type function_name ( parameter list )
{
//body of the function
}
2023-11-10 4
Defining a Function…
Example
int add (int x, int y)
{
int sum;
sum = x + y;
return sum;
}
2023-11-10 5
Defining a Function…
Example
2023-11-10 6
Defining a Function…
Output
Summation is = 11
2023-11-10 7
Parameters & Arguments
Parameters
• A parameter/formal parameter is a placeholder in a
function declaration/definition.
• Parameters are optional; that is, a function may contain
no parameters.
Arguments
• When a function is called, a value is passed to the
parameter. This value is referred to as actual parameter
or argument.
2023-11-10 8
Parameters & Arguments…
Example
Parameters
Arguments
2023-11-10 9
Function Declaration
• A function declaration tells the compiler about a
function's name, return type, and parameters.
Syntax
return_type function_name ( parameter list );
2023-11-10 10
Function Declaration…
Example
int add (int x, int y);
Or
int add (int, int);
2023-11-10 11
Function Declaration…
Example
2023-11-10 12
Function Declaration…
Output
Summation is = 11
2023-11-10 13
Function Declaration…
Example
2023-11-10 14
Function Declaration…
Output
Summation is = 11
2023-11-10 15
Scope Rules
• A scope is a region of the program where a defined
variable can have its existence and beyond that region
variable cannot be accessed.
• Variable types (Generally):
Static variable
Local variable
Global variable
2023-11-10 16
Static Variables
• Preserve their values even after they are out of their
scope.
• A static variable remains in memory while the program is
running. A normal variable is destroyed when function
call is over where the variable was declared.
2023-11-10 17
Static Variables…
Example
2023-11-10 18
Static Variables…
Output
Value in 1st call = 1
Value in 2nd call = 2
2023-11-10 19
Local Variables
• Variables that are declared inside a function are called
local variables to that function.
• Don’t have effect outside of that function.
2023-11-10 20
Local Variables…
Example
2023-11-10 21
Local Variables…
Output
Value in 1st call = 1
Value in 2nd call = 1
2023-11-10 22
Global Variables
• Global variables are defined outside of a function,
usually on top of the program.
• Global variables can be accessed from any function after
its declaration.
• Have value effect after its declaration.
2023-11-10 23
Global Variables…
Example
2023-11-10 24
Global Variables…
Output
Value in 1st call = 1
Value in 2nd call = 2
2023-11-10 25
Function Calling
• Two ways of function calling - By which arguments
can be passed:
Call by value
Call by reference
2023-11-10 26
Call By Value
• Copies the actual value of an argument into the
Parameter/Formal parameter of the called function.
• Changes made to the parameter inside the called function
have no effect on the argument of calling function.
2023-11-10 27
Call By Value…
Example
2023-11-10 28
Call By Value…
Output
IN main FUNCTION:
Before swap, value of a : 100
Before swap, value of b : 200
IN CALLED FUNCTION:
After swap, value of a : 200
After swap, value of b : 100
IN main FUNCTION:
After swap, value of a : 100
After swap, value of b : 200
2023-11-10 29
Call By Value…
Web: https://www.youtube.com/watch?v=HEiPxjVR8CU
2023-11-10 30
Call By Value…
2023-11-10 31
Call By Value…
2023-11-10 32
Call By Value…
2023-11-10 33
Call By Reference
• The call by reference method of passing arguments to a
function copies the address of an argument into the formal
parameter.
• Inside the function, the address is used to access the actual
argument used in the call. It means the changes made to
the parameter affect the passed argument.
• To pass a value by reference, argument pointers are passed
to the functions just like any other value.
• It is needed to declare the function parameters as pointer
types.
2023-11-10 34
Call By Reference…
Example
2023-11-10 35
Call By Reference…
Output:
Before swap, value of a : 100
Before swap, value of b : 200
After swap, value of a : 200
After swap, value of b : 100
2023-11-10 36
Call By Reference…
2023-11-10 37
Call By Reference…
2023-11-10 38
Call By Reference…
2023-11-10 39
Call By Reference…
2023-11-10 40
Call By Reference…
2023-11-10 41
Recursion
• The process in which a function calls itself
directly or indirectly is called recursion.
• The corresponding function is called as recursive
function.
2023-11-10 42
How Does Recursion Work
void recurse()
{
Recursive call
……….
recurse();
……….
}
int main()
{
………..
recurse();
………..
}
2023-11-10 43
Types of Recursion
Two types:
• Direct Recursion
• Indirect Recursion
2023-11-10 44
Direct Recursion
• A function fun is called direct recursive if it calls the
same function fun.
void directRecFun()
{
// Some code....
directRecFun();
// Some code...
}
2023-11-10 45
Indirect Recursion
• A function fun is called indirect recursive if it calls
another function say fun_new and fun_new calls fun
directly or indirectly. void indirectRecFun1()
{
// Some code...
indirectRecFun2();
// Some code...
}
void indirectRecFun2()
{
// Some code...
indirectRecFun1();
// Some code...
2023-11-10 } 46
Base Case
• The stopping criteria of recursion is called the base case.
• The recursion continues until some condition is met to
prevent it.
int fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1);
}
2023-11-10 47
Base Case-Example
Finding Factorial
2023-11-10 48
Base Case-Example…
Output
Enter the number: 5
Factorial of 5 is: 120
2023-11-10 49
Base Case-Example…
return States
fact(5) 5*fact(4) Pause
fact(4) 4*fact(3) Pause
fact(3) 3*fact(2) Pause
fact(2) 2*fact(1) Pause
fact(1) 1
2023-11-10 50
Base Case-Example…
return States
fact(5) 5*fact(4) = 120 resume
fact(4) 4*fact(3) = 24 resume
fact(3) 3*fact(2) = 6 resume
fact(2) 2*fact(1) = 2 resume
fact(1) 1
2023-11-10 51
Stack Overflow
• If base case is not reached or not defined, then
stack overflow problem may arise.
2023-11-10 52
Stack Overflow-Example
//Wrong base case. It may cause stack overflow
2023-11-10 53
Stack Overflow-Example…
If fact(10) is called, it will call fact(9), fact(8), fact(7) and so
on but number will never reach 100. So, the base case is not
reached. If the memory is exhausted by these functions on
stack, it will cause stack overflow error.
2023-11-10 54
2023-11-10 55