C Short Notes Module 4
C Short Notes Module 4
Passing by value: It means the contents of the arguments in the calling function are not
changed, even if they are changed in the called function.
The content of the variable is copied to the formal parameter of the function definition,
preserving the contents of arguments in the calling function.
Passing by address: The address of the variable is passed here instead of the value.
Recursion:
A function that contains a function call to itself is called recursion.
ex:
#include<stdio.h>
//function to find the factorial of a number
int fact(unsinged int num)
{
if(num==0)
return 1;
else
return fact(num-1)*num;
}
void main( )
{
unsigned int n;
printf(“enter the number\n”);
scanf(“%d”,&n);
printf(“The factorial of a number is%d”, fact(n));
}
Two important conditions for recursive function:
1. Each time a function calls itself , it must be closer to the solution
2. There must be a decision criterion for stopping the process of computation.
Recursive and Iterative definition of a function
A function that contains a function call to itself is called recursion.
recursive definition of a function to find the factorial of a number:
ex://function to find the factorial of a number
int fact(unsinged int num)
{
if(num==0)
return 1; //base case
else
return fact(num-1)*num;
}
void main( )
{
unsigned int num;
printf(“enter the number”);
scanf(“%d”, &num);
printf(“The factorial of the number is %d”, fact(num));
}
Iterative definition of function to find factorial of a number:
int fact(unsinged int num)
{
unsigned int i;
int fact=1;
for(i=num;i>1;i--)
{
fact*=i;
}
return fact;
}
void main( )
{
unsigned int num;
printf(“enter the number”);
scanf(“%d”, &num);
printf(“The factorial of the number is %d”, fact(num));
}
Comparison Chart
RECURSION ITERATION
The statement in a body of function calls the Allows the set of instructions to be repeatedly
function itself. executed.
If the function does not converge to some If the control condition in the iteration
condition called (base case), it leads to statement never become false, it leads to
infinite recursion. infinite iteration.