Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed."— Presentation transcript:

1 ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

2 Syllabus Variables Within Functions Variables Outside of Functions Function Parameters Function Declarations

3 2 Variables Within Functions Definition → Scope is the region of program text over which a name is known A variable declared inside a function has local scope and is accessible only within that function Changing a local variable's value in one function does not affect a variable with the same name in another function; they are totally different objects

4 3 Example: void fun1( void ) { // fun1 int x; x = 3; } //end fun1 void fun2( void ) { // fun2 int x; x = 5; fun1(); } //end fun2 int main( void ) { // main int x; x = 7; fun1(); fun2(); return 0; } //end main This x is local to fun1.This x is local to fun2.This x is local to main.

5 4 Variables Outside of Functions A variable declared outside of all functions (e.g., at the top of the source file) has global scope  A global variable is visible and accessible everywhere below its declaration  Changing a global variable in one function will affect its value everywhere  If a global variable and a local variable are in the same scope, then the local variable is accessed and not the global variable; we say: the local hides the global inside the local scope  Extern scope (the external scope for names known in multiple compilation units) is not handled here

6 5 Example: double g = 1.0; int m = 2; void fun1( void ) { // fun1 int x; } //end fun1 double h = 6.7; void fun2( void ) { // fun2 int x; g = 5; } //end fun2 int main( void ) { // main int m; m = 5; return 0; } //end main g is global and visible to fun1, fun2, main. h is global and visible to fun2, main. m is global and visible to fun1, fun2, ( main ). This would change the global g.This m is local and overrides the global m. This changes local m. Global m is unchanged.

7 6 Function Parameters A function’s formal parameter have local scope In C, all non-array arguments are passed-by-value: Only a copy of an argument is passed to the function  Changes made to value parameters within a function do not affect the original value of the arguments that were passed to the function  Changing a parameter's value in one function does not affect a variable with the same name in other functions

8 7 Example: void fun( int x, int y, int z ) { // fun int a = 8;... x = 7; } //end fun int main( void ) { // main int x = 0, u = 1; printf( "x = %d\n", x ); fun( x, u, 3 ); printf( "x = %d\n", x ); return 0; } //end main The output displayed is: x = 0 Why is this?

9 8 Start in main(): int x = 0, u = 1; Call the function fun(): fun( x, u, 3 ); main x 0 u 1 fun x 0 y 1 Z 3 copied 3

10 9 Execute fun(): int a = 8; x = 7; main x 0 fun u 1 x 0 y 1 Z 3 a 87

11 10 Go back to main(): return 0; main x 0 u 1

12 11 Function Declarations A function declared (via a prototype) inside another function has local scope and is visible and accessible only to that function A function declared outside of all functions (e.g., at the top of the source file) has global scope  A global function is visible and accessible everywhere below its declaration #include files typically contain function prototypes for various library functions


Download ppt "ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed."

Similar presentations


Ads by Google