I have a code which includes a recursive function. I have wasted a lot of time on recursion but I still couldn't get it really:
#include<stdio.h>
void count(int);
int main()
{
int x=10,z;
count(x);
}
void count(int m)
{
if(m>0)
count(m-1);
printf("%d",m);
}
When the 1st time count
is called with argument as 10. it fulfills the condition and then here starts the recursive part. what happens really when a function calls itself? I don't get it. Please explain with reference to stacks.
next time it calls itself it has a smaller value
So first it will call count with 10, then it will call it with 9, then 8, then 7..... all the way until this if statement isn't true:
What might be confusing you is the if statement only applies to the next line (printf isn't part of the if statement)
so you have:
So, the recursive calls will stop once m is not > 0, and then it will call the printf.
After it calls printf for when m is 0, then it will return from that 'count' call, (Back to where m was equal to 1), and then it will call the printf when m is 1, and then when m is 2, .....
So the output should be:
EDIT: In terms of a stack:
This is what the stack is doing:
->
->
...
->
-> (and then it starts printing and popping the method off the stack)
->
->
...
->
->
Each number refers to the line number.
The execution happens like this(with x=3) -
line number|value of x
1 3
2 3
3 3
4 2
3 2
4 1
3 1
4 0
5 0
5 1
5 2
5 3
Numbers printed on the screen 0 1 2 3
While
m
is greater than 0, we callcount
. Here is a representation of the stack calls:When a function is called the return address (of the next code to execute) is stored on the stack along with its current arguments. Once the function finishes the address and arguments are popped so the cpu will know where to continue its code execution.
Let's write the addresses of the function (for the purpose of this example only)
For m = 1:
The
if
is fullfield so we execute code ataddress 101
withm = 1
.address 102
andm = 1
are pushed to the stack and the function is executed again fromaddress 100
withm = 0
. Sincem = 0
we executeaddress 102
and printing 0 on console. The function ends and the last returnaddress (102)
and argumentm = 1
are popped and the line ataddress 102
is executed printing 1 on the screen.If you want a more specific answer, then you have to look into how compilers work. But generally speaking the compiler will create machine code for the function that includes a preamble, in this code enough memory is allocated on the stack (by decrementing a stack pointer) that a copy of the functions variables can be placed on the stack (we can store the state of the function).
The function will have values stored in registers and memory and these must be stored on the stack for each (recursive) call, otherwise the new call will overwrite the precious data!. So very basically the machine code looks like this (pseudo code)
By changing some data used for computation we can go back and read the same instructions again and get a different result. The abstraction of recursive function allows us to manipulate the data we want to change in a nice "call by value" way so that we don't overwrite it unintentionally.
This is done by the compiler when it stores the state of the calling function on the stack so we don't have to manually store it.
count
is called with an integer argument of10
.m
which is10
is greater than0
the functioncount
calls itself with an integer argument ofm
which is10
minus1
which equals9
.(m-1)
untilm
is not greater than0
and which point the program prints the value ofm
.The recursive function just modifies the parameter given to it and calls itself with that modified value until the desired result is returned (in this case
m
not being greater than0
).