- Scope is a region of the program where a defined variable is valid, and outside of this region that variable cannot be accessed.
Any variables declared as a parameter to a function only exist within the context of the function. In other words, the rest of the program does not have any ability to directly read or modify parameter variables aside from passing values to them in a function call. Because of this, parameter names may be the same as another variable name declared outside of the function or in another function. When the function does something with n from the example here, the n that it uses is the one in the parameter list, effectively making the "global" n inaccessible from within the function. This can be avoided by giving the parameter a different name from the "global" variable. Ordinarily, a function can access "global" variables just like any other part of the program.
Different functions may use the same parameter names, but the function will only use its own parameter by that name.
Variables Declared Within a Function
Just like with function parameters, if a variable is declared within the function body, it will be local to the function. So, if a variable of the same name is declared globally, code within the function can only access the local variable, whereas code outside the function can only access the global variable. Also, just like function parameters, the same name may be used for variable declarations in different functions. Each function will only use its version of the variable by that name.
The next piece of code will generate an error since a may not be accessed outside of the function where it was declared.
Global vs Local Variables
In the following example:
- x can be seen by everybody
- foo's local parameter is y
- foo's local variable is z
- foo cannot see main's a
- foo can see x
- main's local variable is a
- main cannot see foo's y or z
- main can see x
A locally defined identifier takes precedence over a globally defined identifier.
#define Within a Function
Running this code will result in the following output in the UART1 IO window:
Why? Because the closest #define for x above its use defines x = 5. The order of the code's execution doesn't matter here. The value is determined purely by its position within the source file's text. When using #define, just think of your source code as an ordinary linear text file. The label's value is valid from the line of the file where it is defined all the way to the end of the file unless it is redefined along the way.
C originally defined functions like this:
DO NOT use the old method! Use the new one only: