|
|
 |
|
 |
|
|
Name Precedence The precedence that a local identifier in a function has over a global identifier with the same name in any references that the function makes to that identifier; also called name hiding. |
|
|
|
|
|
|
|
|
Here's an example that uses both local and global declarations: |
|
|
|
|
|
|
|
|
#include <iostream.h>
void SomeFunc( float );
const int a = 17; // A global constant
int b; // A global variable
int c; // Another global variable
int main()
{
b = 4; // Assignment to global b
c = 6; // Assignment to global c
SomeFunc(42.8);
return 0;
}
void SomeFunc( float c ) // Prevents access to global c
{
float b; // Prevents access to global b
b = 2.3; // Assignment to local b
cout < a = < a; // Output global a (17)
cout < b = < b; // Output local b (2.3)
cout < c = < c; // Output local c (42.8)
} |
|
|
|
|
|
|
|
|
In this example, function SomeFunc accesses global constant a but declares its own local variables b and c. Thus, the output would be |
|
|
|
|
|
|
|
|
Local variable b takes precedence over global variable b, effectively hiding global b from the statements in function SomeFunc. Formal parameter c also blocks access to global variable c from within the function. Formal parameters act just like local variables in this respect. |
|
|
|
|
|