Today we are going to discus about recursion topic in function.Simple words me ager aapko bata du iska use kerek hum kaafi bade programm ko short ker dete hai inka mean recursion ke progrma ki length normal function ke progream se kam hoti hai. Isme only thora sa thinking process hota hai . jisse hum ek statement aisa creat kerte hai jo ba rbar khub ko call kerta hai aur end me aapko result de deta hai.
Recursion:
Recursion is a powerful programming technique that can be used to solve the problem of smaller size.A function can call itself and the function is called as an recursion function.
Recursion is classified according to following criteria -
- Whether function itself directly or indirectly.
- Whether there are any pending operation form the recursion call.
- Patter of recursion call.
There are two type of recursion-
- Direct recursion
- Indirect recursion
Direct recursion:
A function is directly recursive if it call itself directly. That is a function body contains and explicit call to itself.
Indirect recursion:
A function is indirectly recursive if it is not called by itself , another function called it that is indirect recursion.
When a call a that is direct recursion and if a call b which is called by a in that place that is indirect recursion.
(Ager koi function khud ko call kerta hai bar bar to wo direct recursion hai lekin usi place per ager koi function kisi aur function ko call kerta hai to wha per indirect recursion hota hai. )
Wap to add m*n mean n time add m-
#include<stdio.h>
int mult (int m, int n)
main()
{
int m,n,res;
printf("Enter number:");
scanf("%d %d ",&m,&n);
res=mult(m,n);
printf("res=%d",s);
}
int mullt (int m, int n)
{
if (n==1)
return m;
else
return m+mult(m,n-1);
}
here you see this is the example of recursion where i add n times m for do without recursion we use loop but by recursion we can we first give return statement where i put when n equals to 1 then it will be stop and give output.n mean how much time we add m , after give both value-
this ever time give his value to last return statement and where every time value of n will decrease and at last remain 1 value of n then it will transfer that result in main functio than display on our screen.
Thanks,
If any question comment below,
Comments
Post a Comment