6 Functions
6 Functions
2023-11-10 1
Functions
• A function is a group of statements that together
perform a task.
• Example
2023-11-10 2
Functions…
• A function can be implemented in two ways:
• Function definition
• Function declaration
2023-11-10 3
Defining a Function
• A function definition consists of a function header and
a function body.
Syntax
2023-11-10 4
Defining a Function…
Example
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.
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
2023-11-10 10
Function Declaration…
Example
Or
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.
Static variable
Local variable
Global variable
2023-11-10 16
Static Variables
• Preserve their values even after they are out of their
scope.
2023-11-10 17
Static Variables…
Example
2023-11-10 18
Static Variables…
Output
2023-11-10 19
Local Variables
• Variables that are declared inside a function are called
local variables to that function.
2023-11-10 20
Local Variables…
Example
2023-11-10 21
Local Variables…
Output
2023-11-10 22
Global Variables
• Global variables are defined outside of a function,
usually on top of the program.
2023-11-10 23
Global Variables…
Example
2023-11-10 24
Global Variables…
Output
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.
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.
2023-11-10 35
Call By Reference…
Output:
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.
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
2023-11-10 49
Base Case-Example…
return States
2023-11-10 50
Base Case-Example…
return States
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
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