5.3. Variable scope#

In the previous section, we found that we created a new variable n in factorial function to take in the value of number from the main function. This is because we cannot access number inside factorial function. Likewise, n cannot be accessed in the main function. We say the scope of number is within the main function, and the scope of n is factorial function.

This is similar to what we discussed in for loops in Scope of the loop variable. We said that the following would cause a compile-time error, since count is declared inside the loop and is undefined outside the loop. The scope of count is only in the for loop.

Code snippet causing compile-time error at line \(4\)

1for (int count = 1; count <= n; count++) {
2  printf("*");
3}
4count = 10;  // undefined variable

Similarly, variables inside a function cannot be accessed outside a function, even if they have similar names. For example, in the following code, the main function has a variable n, and it calls divideByTwo function by transferring the value of n. This is called “call by value”, as we call to transfer only the value of n. Inside divideByTwo function, another variable n is created, that is totally different from n in the main function. Download divideByTwo.c if you want to run the program yourself.

Code



#include <stdio.h>

double divideByTwo(double);
int main(void) { double n = 4.2, result; result = divideByTwo(n); return 0; }
double divideByTwo(double n) { n = n / 2; return n; }

To understand how variables from another function cannot be accessed and how “call by value” works under the hood, we look into how the variables of each function is stored in the main memory. The following video will execute the program one line at a time, and explain what happens in the main memory.

Quiz

0 Questions

X