Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are same static variables used for each recursive call to a function? [duplicate]

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
like image 946
Meathead Avatar asked Jun 18 '14 02:06

Meathead


People also ask

How are static variables used in recursive functions?

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.

Can two static variables have the same name?

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.

How do variables work in recursion?

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.

Can you have more than one recursive call?

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.


2 Answers

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.

like image 78
quantdev Avatar answered Oct 21 '22 05:10

quantdev


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.

like image 45
Al Bud Avatar answered Oct 21 '22 04:10

Al Bud