C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture09_ProgOrgExamReview

# C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture09_ProgOrgExamReview

This preview shows pages 1–4. Sign up to view the full content.

1 ee312 Spring 2008 Lecture 9 1 Announcements Lecture 9 • Program Organization - Chapter 10 • Topics for today – Program Organization – Exam 1 Review • For Next Lecture Session – Functions and Recursion – Review Computer Basics from EE306 before we get into pointers…. ee312 Spring 2008 Lecture 9 2 Global (External) Variables Variables declared outside functions are said to be global One advantage of external variables is that they can be shared by several functions: #include <stdio.h> int i; /* i is a global variable */ void print_count(void) { printf("T minus %d and counting\n", i); } int main(void) { for (i = 10; i > 0; --i) print_count(); return 0; } Another advantage is that external variables retain their values throughout the execution of the program. External variables should be used sparingly – If an external variable is assigned an incorrect value, it is difficult to identify the guilty function. – Functions that use external variables are hard to reuse in other programs.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 ee312 Spring 2008 Lecture 9 3 Local Variables Variables declared inside a function are said to be local to that function. Example: float max(float a, float b) { float big; /* big is a local variable */ if (a > b) big = a; else big = b; return big; } A local variable exists only when the enclosing function is executing. A local variable is visible only to statements in the enclosing function. Names of local variables can be reused for other purposes in the same program. ee312 Spring 2008 Lecture 9 4 Parameters A function header can contain parameter declarations,e.g. void swap( int i, int j) { int temp; temp = i; i = j; j = temp; } A variable declared as a parameter in a function header exists only as long as the function is executing. It is a local variable known inside the function only. It derives its value from the actual arguments passed at function call time.
3 ee312 Spring 2008 Lecture 9 5 Inside of Blocks A compound statement can contain declarations, e.g. if (i < j) { int temp; temp = i; i = j; j = temp; } A variable declared in a block exists only as long as statements in the block are executing. A variable declared in a block is visible only to statements in the block. A block can appear anywhere a statement is allowed. ee312 Spring 2008 Lecture 9 6 Scope Rules When a declaration inside a block (or function) uses an identifier that is already visible, the new declaration temporarily “hides” the old one. At the end of the block, the identifier regains its old meaning. Example: Resolution rules: 1. Look for most local 2. Look for a parameter 3. Look for a global int i; /* Declaration 1 - global - static storage duration, file scope */ void f (int i) /* Declaration 2 - a parameter - block scope */ { i = 1; } void g (void) { int i; /* Declaration 3 - a local variable - block scope */ if (i > 0) { int i; /* Declaration 4 - local variable - block scope */ i = 2; } i = 3; } void h (void) { i = 4; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 43

C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture09_ProgOrgExamReview

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online