Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.

Similar presentations


Presentation on theme: "Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all."— Presentation transcript:

1 Lecture 13: Working with Multiple Programmers

2 Headers Header files: Each standard library has a corresponding header. The function prototype for all the library functions. Definition of various data types Definition of constants needed by the library functions Load with #include #include Custom header files Including function prototypes, definition of data types and constants. Save as filename.h Load with #include“filename.h” Source file and filename.h are in the same directory. Purposes: Reuse functions

3 Calling Functions Call by value Copy of argument passed to function Changes in function do not effect original Use when function does not need to modify argument To avoid accidental changes #include int add10(int x); int main( void ) { int a = 5; add10(a); printf(“%d\n”, a); return 0: } int add10(int x) { x += 10; return x; } … … … a 5 memory … … … a 5 x 5 … … … a 5 x 15 … … … a 5 memory Before calling add10() when calling add10() Just before return from add10() Just after return from add10()

4 Calling Functions Call by reference Passes original argument Changes in function effect original Only used with trusted functions In C, all calls are by value. Possible to simulate call-by-reference in C

5 Scope Rules The scope of an identifier is the portion of the program in which the identifier can be referenced. File scope Identifier defined outside function, know in all functions from the point at which the identifier is declared until the end of the file. Used for global variables, function definitions, function prototypes

6 Scope Rules Block scope Identifier declared inside a block Block scope begins at definition, ends at the terminating right brace ({) of the block. Used for local variables, function parameters Outer blocks “hidden” from inner blocks if there is a variable with the same name in the inner block

7 Scope Rules - Example #include void useGlobal( void ); void useLocal( void ); int x = 1; /* global variable */ int main( void ) { int x = 5; printf("x on entering main is %d\n", x); {/* start new scope */ int x = 7; /*local variable to new scope */ printf("x in inner scope of main is %d\n", x); }/* end new scope */ printf("x in outer scope of main is %d\n", x); useLocal(); useGlobal(); useLocal(); useGlobal(); printf("\nx on exiting main is %d\n", x); return 0; } void useLocal(void) { int x = 25; printf("\n x is %d on entering useLocal\n", x); x++; printf(" x is %d on exiting useLocal\n", x); } void useGlobal(void) { printf("\n x is %d on entering useGlobal.\n", x); x *= 10; printf(" x is %d on exiting useGlobal.\n", x); }

8 Scope Rules Function prototype scope Identifiers used in the parameter list of a function prototype. Variable names are ignored by the compiler Identifiers used in a function prototype can be reused elsewhere in the program without ambiguity. Function scope Can only be referenced inside a function body. Used only for labels (start:, case:, etc.)

9 Practice Question #include void anotherGlobal( void ); void useGlobal( void ); int x = 1; /* global variable */ int main( void ) { anotherGlobal(); printf(”%d ", x); return 0; } void anotherGlobal(void) { int x = 5; useGlobal(); printf(“%d ”, x); } void useGlobal(void) { x *= 10; printf(”%d ", x); } Q. What is the output of the following program? A.5 10 10 B.10 10 5 C.10 5 10 D.10 5 1 Solution: D


Download ppt "Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all."

Similar presentations


Ads by Google