As with any function call, at the end, execution just starts back up at the next line:
printf("%d\n", i);
// into the function
count(i + 1);
// out of the function
printf("%d\n", i);
The important thing to know is that the value of i
doesn't get updated. They aren't the same variable. There are ten different 'versions' of i
.
printf("%d\n", i); // i is 3
count(i + 1);
printf("%d\n", i); // i is still three, but a version of the function just ran where i is 4
If you imagine just pasting the code in when you see count(i + 1), you get this when expanding count(8):
if(8 < 10){
printf("%d\n", 8);
count(8 + 1);
printf("%d\n", 8);
}
Now paste:
if(8 < 10){
printf("%d\n", 8);
if(9 < 10){
printf("%d\n", 9);
count(9 + 1);
printf("%d\n", 9);
}
printf("%d\n", 8);
}
And again:
if(8 < 10){
printf("%d\n", 8);
if(9 < 10){
printf("%d\n", 9);
if(10 < 10){
// we'll never get here
}
printf("%d\n", 9);
}
printf("%d\n", 8);
}
This is the code that actually gets run. The 'old' values of i
don't change. In the end you have 10 different i
variables from all the different times your called the function.