'Recursive function, where the argument is passed in the different ways
Could someone please explain to me the difference between these three blocks of code?
#include <stdio.h>
void f(int a)
{
printf("%d", a);
if (a >= 1)
f(--a);
printf("%d", a);
}
int main()
{
f(3);
return 0;
}
Output: 32100012
#include <stdio.h>
void f(int a)
{
printf("%d", a);
if (a >= 1)
f(a - 1);
printf("%d", a);
}
int main()
{
f(3);
return 0;
}
Output: 32100123
#include <stdio.h>
void f(int a)
{
printf("%d", a);
if (a >= 1)
f(a--);
printf("%d", a);
}
int main()
{
f(3);
return 0;
}
Output: Infinite printing of the number 3
Solution 1:[1]
Third one f(a--); it is always calling f with the a == 3, then (after the function return) it will subtract 1 from a. But will never happen.
The second one - it simply calls the f with a minus 1 but not changing the a printing everything from 3 to 0 and then when the function returns the second print prints it in the reverse order as functions return in the FIFO basis.
The first one is basically very similar to the second one but a is decremented thus result printed by the second printf are 0012 not 0123
Solution 2:[2]
1) printf("%d", a); if (a >= 1) f(--a); printf("%d", a);
^^^^^^^^^^^^^^^^ ******************* ^^^^^^^^^^^^^^^^
*********** f(2) **
print a twice with something in between; something includes decrementing a. So f(3) prints 3<something_recursive_from_f(2)>2
2) printf("%d", a); if (a >= 1) f(a - 1); printf("%d", a);
^^^^^^^^^^^^^^^^ ********************* ^^^^^^^^^^^^^^^^
*********** f(2) ****
print a twice with something in between. So f(3) prints 3<something_recursive_from_f(2)>3
3) printf("%d", a); if (a >= 1) f(a--); printf("%d", a);
^^^^^^^^^^^^^^^^ ******************* ^^^^^^^^^^^^^^^^
*********** f(3) **
print a twice with something in between; something includes decrementing a. So f(3) prints 3<something_recursive_from_f(3)>2
... but something recursive is f(3) again... and f(3) again... and f(3) again... till your computer burns out.
Solution 3:[3]
Some visual aids.
Consider what happens in the first code sample:
f(3)
-> prints "3"
-> decrements a to 2
-> calls f(2)
-> prints "2"
-> decrements a to 1
-> calls f(1)
-> prints "1"
-> decrements a to 0
-> calls f(0)
-> prints "0"
-> prints "0"
-> prints "0"
-> prints "1"
-> prints "2"
With the second code sample:
f(3)
-> prints "3"
-> calls f(2)
-> prints "2"
-> calls f(1)
-> prints "1"
-> calls f(0)
-> prints "0"
-> prints "0"
-> prints "1"
-> prints "2"
-> prints "3"
And the third sample code:
f(3)
-> prints "3"
-> *post*-decrements a to 2
-> calls f(3)
-> prints "3"
-> *post*-decrements a to 2
-> calls f(3)
-> Oops! Infinite loop.
Read up on the differences between pre- and post-increment and decrement operators.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 0___________ |
| Solution 2 | |
| Solution 3 |
