C program using recursive function




















A function is said to be recursive if it is called within itself. Recursion is supported by the programming language C.

Below are two conditions that are critical for implementing recursion in C:. In this way, we can implement a recursive function in the C programming language. These functions are useful for solving money mathematical problems that require a similar process to be called several times. Examples of such problems are calculating the factorial of a number of Fibonacci series generation.

Recursive functions are the way to implement the equation in C programming language. A recursive function is called with an argument passed into it say n, memory in the stack is allocated to the local variables as well as the functions. All the operations present in the function are performed using that memory.

The condition for exit is checked if it fulfills. When compiler detects a call to another function it immediately allocates new memory on the top of the stack where a different copy of the same local variables and the function gets created.

Enter the same process continues. Once the first statement is finished, the second statement will execute. At this point, the tracing tree will be like below. Let us trace fun2 1. Again 1 is greater than 0 and hence the condition is satisfied and again two steps. In the first step, it will call itself bypassing n-1 i. So, at this point, the tracing tree of this recursive function is like the below.

The next call is fun2 0. Now fun2 0 , 0 is greater than 0, no. So, this call with parameter 0 has terminated. Now once this call has been terminated the control should go back to the previous call.

The previous call was fun2 1 , it will go back to the function call and execute the next statement i. At this call, the n value is 1 and hence it will print 1. Then it will go back to the previous call i.

For better understanding, please have a look at the following image. Once the fun2 2 call finishes, it goes back to the previous call i. And the output you will get from this function is 1 2 3 as shown in the below image. The output of example 1 was 3, 2, 1 and the output of example 2 is 1, 2, 3.

Now, let us compare both of the examples, in example 1, first, the printing was done and then the recursive call was made but in example 2, first the recursive call was made and then the printing was done at returning time.

Note: The most important thing that you need to understand in recursion is that recursion has two phases. One is the calling phase and the other one is returning phase. Now, let us take one example and see the differences between them. Suppose there is a room and in that room, there is a bulb and a door. From that room, you can enter into one more room, and again in that room, there is a door and there is also a bulb fixed in it.

Again, from that room, you can enter into one more room and in that room also there is a door and a fixed bulb. So, there are a total of 3 rooms i. Now if I give you the following two instructions,. Now if I make you stand in room 1 and ask you to perform the above two 2 steps recursively, then what will you do. Let us check. In the first room, you will switch on the bulb i. So, the first bulb that you have switched on. Then you will enter into next room i. Similarly, in the third room, recursively you will switch on the bulb i.

Now, there is no next room further. So, it was more like a recursive call, a function calling itself again and again. This is termination there is no next room. Reference Materials string. Start Learning C. Explore C Examples. Find G. D Using Recursion. Find Factorial of a Number Using Recursion. Calculate the Sum of Natural Numbers. C Recursion In this tutorial, you will learn to write recursive functions in C programming with the help of an example.

How recursion works? Table of Contents Recursion Introduction How recursion works? Previous Tutorial:. In this article, we will discuss the recursion importance and their working process with an example in detail. Recursion performs repetition on the function calls, and it stops the execution when the base case becomes true. A base case condition should be defined in the recursive function to avoid stack overflow error message. If no base case is defined it leads to infinite recursion.

When a function is called it pushes them into a stack each time for the reserving resources for each repetition calls. It gives the best at tree traversal.

There are two different types of recursion: Direct and indirect recursion. Consider a second type called indirect recursion which involves another function call. It can be viewed in the below illustration:.



0コメント

  • 1000 / 1000