Recursion
- Recursion is the process in which , function calls or invoke itself repeatedly untill certain condition is met.
- Calls itself means ,in the body of function where we are defining a function, a call has given to the same function in that function body itself.
- This process is used in iterative type of problems.
- Recursion is used in Iterative computation where each action is stated in terms of previous result.
- Solving problem Using Recursion reduce the lines of code.
- Complex programs can be written very easily by recursion.
- They are easy to write but picking the logic is pretty much Difficult.
- At every problem, you have to specify some condition ,so that iteration can stop.
- Every problem which is solved with recursion ,that same problem can done with loops.
- But problem which is solved with loops that may not done with recursion.
We will write simple program for sum of n numbers with recursion.
Solution:
#include<stdio.h>
int sum(int n); //declaration
//definition
int sum(int n)
{
if(n == 1) //if n will be equal to 1, then the iteration will stop,
return 1; //and it will return 1.
else
return(n + sum(n-1) );//function calling it's self.
}
#include<stdio.h> int sum(int n); //declaration //definition int sum(int n) { if(n == 1) //if n will be equal to 1, then the iteration will stop, return 1; //and it will return 1. else return(n + sum(n-1) );//function calling it's self. }
Using Above defined function:
void main()
{
int result;
//calling function 'sum'.
result = sum(5);
printf("\nSum is %d",result);
}
void main() { int result; //calling function 'sum'. result = sum(5); printf("\nSum is %d",result); }
Ouput:
Sum is 15.
- As you saw, program was of few lines of code, but what was going on behind screen,let's understand it.
- Step by Step Explanation.
- step1:As function was call by passing 5 to it, As value at if block was checked and condition evaluated to false and control went to else block.
step2:at there is return statement which is returning current value of n which is 5 plus with function 'sum' with parameter n-1 which is 4 now - step3: Now function is called by passing 4 to it, and if condition was checked and evaluated to false and control went to else block.
step4: And again it returned current value of n which is 4 plus with function sum with parameter n-1 which is 3 now. - step5: Now function is called by passing 3 to it,and if condition was checked and evaluated to false and control went to else bloc k.
step6: and again it returned current value of n which is 3 plus with function 'sum' with parameter n-1 which is 2 now. - step7: Now again function is called by passing 2 to it,and if condition was checked and evaluated to false and control went to else block.
step8: And again it returned current value of n which is 2 plus with function 'sum' with parameter n-1 which is 1 now. - step9: Again function is called by passing 1 to it,and if condition was checked and now it evaluated to true and now it returned value 1,and as you know when function returns its value then control goes at that point where it was call ,now it will go to step no 7.
- Now function call at step no:8 has returned value 1 So it will get add with 2 and it will become 3.
- but 3 is a returned value for function which is called at step no:6.
So ,returned value will be added with 3 and will become 3+3 = 6,now value has become 6. - But value '6' is returned value by function which is called at step no:4.
So returned value will get added with 4 which will become 6+4=10,now value has become 10.
- But Again value 10 is returned value for function which was called at step no:2.
So returned value will get added with 5 which will become 10+5=15,now value has reached to 15. - As value 15 is returned value for function which is call at step no:1.
Finally the the actual function has returned the value which is 15, And we have assigned that value to variable result, and that is are final Output. - Hence , Sum of first five number is 15.
- As Recursion is an pretty much difficult topic as from the beginners point of view.
- So, to understand it more better ,do more practice on it.
- For getting good command on Recursion, do more practice on it.
Further Concepts:
Further Topics:
- What are Pointers?
- how to declare pointers?
- how to initialize a pointers?
- How to access the pointer variables?
- Function of '*' and '&' operators
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- How to declare Structure Pointer?
- Working with structure Pointer
- How to Create Structure Variable Dynamically ?
- Creating structure variable through malloc() and calloc()
People Also Searched:
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() functions
1 Comments
Explanation approach was good.
ReplyDeletePost a Comment