As per my understanding, each called function has some memory allocated to it in the program stack, and this holds true even if the same function calls itself recursively (i.e, each invocation has it's own memory in program stack). Please answer the following two questions arising from my program below:
If a variable is declared static in a function, will the same variable/same copy be used for all recursive calls of that function?
If the variable is not declared static (e.g, simply "int x"), will each recursive call to the function have its own copy of that variable? If yes, is that the way it normally happens when a function is called from other function, including the recursive calls?
#include<stdio.h>
#include<stdlib.h>
int main()
{
static int x=0;
x++;
printf("Team %d\n",x);
if(x<10)
main();
else
exit;
}
Output:
Team 1
Team 2
Team 3
Team 4
Team 5
Team 6
Team 7
Team 8
Team 9
Team 10
Here, we are creating a static integer variable (i.e. x) whose value is initialized with 0. Before calling the function itself, we are incrementing the static variable value by 1 and while returning we are adding x as shown in the below image.
A static variable in a function is particular to that function. That is, you can only access the variable in that function. Because of this, you could have a static variable in 5 functions, each with the same name.
Every recursive call is a separate function call, and as such, it maintains its own set of local variables. This means that for the list [1, 2, 3], there are three different frames on the call stack, each with a variable called count, set to the value of 1. When we reach our base case, we return zero.
A recursive implementation may have more than one base case, or more than one recursive step. For example, the Fibonacci function has two base cases, n=0 and n=1.
If a variable is declared static in a function, will the same variable/same copy be used for all recursive calls of that function?
Yes, as your code sample shows
If the variable is not declared static (e.g, simply "int x"), will each recursive call to the function have its own copy of that variable? If yes, is that the way it normally happens when a function is called from other function, including the recursive calls?
Yes, each call will have it's own stack frame with a copy of each local variable. This is what happens for every function call, whether they are recursive or not. And yes, this very principle allows, among other things, recursion.
Static variables live the life of the program, there is only one copy
Locals (Autos) get a new copy on the stack each function call.
Parameters passed by value get a new copy as well.
Parameters passed by reference (pointer) point to the copy that's passed to them. Therefore if it's a local in a recursive execution, it might be a unique copy; but usually it's a reference to a variable that called the function before the recursion started, therefore, the same copy.
It's better to do the functionality you're doing by passing a pointer to a variable, you have more control over subsequent instances of recursion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With