In this case, calling fun() is like calling any other function. For example:
int main() {
int a = 0;
foo(a);
printf("main a = %d\n", a);
}
void foo(int a) {
a = 1;
bar(a);
printf("foo a = %d\n", a);
}
void bar(int a) {
a = 2;
printf("bar a = %d\n", a);
}
Your call sequence look like this:
main();
foo();
bar();
And your output will be this:
bar a = 2
foo a = 1
main a = 0
The arguments are passed by value, so a
is copied and is actually a different variable in each function. The same happens with recursion.
main(); x = 3
fun(3); a = 3, so a > 0, nothing happens, return to main()
If you were to change the condition so fun() calls itself when a > 0 (read top-down)
main(); x = 3
fun(3); a = 3, a > 0 so --a = 2, fun(2)
fun(2); a = 2, a > 0 so --a = 1, fun(1)
fun(1); a = 1, a > 0 so --a = 0, fun(0)
fun(0); a = 0, so return to fun(1)
fun(1); printf("%d", a) displays 1, --a = 0, fun(0) /* same as fun(1) above */
fun(0); a = 0, so return to fun(1)
fun(1); nothing left to do so return to fun(2) /* same as fun(1) above */
fun(2); printf("%d", a) displays 2, --a = 1, fun(1)
fun(1); a = 1, a > 0 so --a = 0, fun(0) /* this is a new fun(1) */
fun(0); a = 0, so return to fun(1)
fun(1); printf("%d", a) displays 1, --a = 0, fun(0)
fun(0); a = 0, so return to fun(1)
fun(1); nothing left to do so return to fun(2)
fun(2); nothing left to do so return to fun(3)
fun(3); printf("%d", a) displays 3, --a = 2, fun(2) /* halfway point */
fun(2); a = 2, a > 0 so --a = 1, fun(1)
fun(1); a = 1, a > 0 so --a = 0, fun(0)
fun(0); a = 0, so return to fun(1)
fun(1); printf("%d", a) displays 1, --a = 0, fun(0)
fun(0); a = 0, so return to fun(1)
fun(1); nothing left to do so return to fun(2)
fun(2); printf("%d", a) displays 2, --a = 1, fun(1)
fun(1); a = 1, a > 0 so --a = 0, fun(0)
fun(0); a = 0, so return to fun(1)
fun(1); printf("%d", a) displays 1, --a = 0, fun(0)
fun(0); a = 0, so return to fun(1)
fun(1); nothing left to do so return to fun(2)
fun(2); nothing left to do so return to fun(3)
fun(3); nothing left to do so return to main()
And your output should be: 1213121 which reflects the tree structure of the calls:
3
/ \
/ \
2 2
/ \ / \
1 1 1 1