lecture 13: working with multiple programmers. headers header files: each standard library has a...

9
Lecture 13: Working with Multiple Programmers

Upload: hilary-stevenson

Post on 14-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

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

Lecture 13: Working with Multiple Programmers

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

HeadersHeader files: Each standard library has a corresponding header.

The function prototype for all the library functions.Definition of various data typesDefinition of constants needed by the library functionsLoad with #include<filename>

#include<math.h>

Custom header filesIncluding 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

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

Calling FunctionsCall by value

Copy of argument passed to functionChanges in function do not effect originalUse when function does not need to modify argument

To avoid accidental changes

#include<stdio.h>

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

memory

x 5

……

a 5

memory

x 15

……

a 5

memory

Before calling add10()

when calling add10()

Just before return from add10()

Just after return from add10()

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

Calling FunctionsCall by reference

Passes original argumentChanges in function effect originalOnly used with trusted functions

In C, all calls are by value.Possible to simulate call-by-reference in C

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

Scope RulesThe 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

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

Scope RulesBlock scope

Identifier declared inside a blockBlock scope begins at definition, ends at the terminating right brace ({) of the block.

Used for local variables, function parametersOuter blocks “hidden” from inner blocks if there is a variable with the same name in the inner block

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

Scope Rules - Example#include<stdio.h>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);}

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

Scope RulesFunction prototype scope

Identifiers used in the parameter list of a function prototype.Variable names are ignored by the compilerIdentifiers used in a function prototype can be reused elsewhere in the program without ambiguity.

Function scopeCan only be referenced inside a function body.Used only for labels (start:, case:, etc.)

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

Practice Question

#include<stdio.h>

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 10B. 10 10 5C. 10 5 10D. 10 5 1

Solu

tion

: D