Scope
Scope
Scope is a region of the program where a defined variable is valid, and outside of this region that variable cannot be accessed.

Parameters

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.

parameters2.png

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.

overloading.png

#define Within a Function

Running this code will result in the following output in the UART1 IO window:

5
5

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.

Historical Note

C originally defined functions like this:

DO NOT use the old method! Use the new one only:

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.