{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

P.Lecture - 2.12.08

# P.Lecture - 2.12.08 - Announcements Assignment 3 out today...

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

Announcements • Assignment 3 out today – Assignment 3 and 4 logisitics • Exam 1 Thursday – 55 minute exam (9:30 – 10:25) • Program Development Measurement – SLOC, person hours, logic defects • Topics for today Topics for today – Scope of variables – Exam Review 1 Exam Review

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

View Full Document
Global (External) Variables Variables declared outside functions are global External variables can be shared by several (all) 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; } E l i bl i h i l h h h i f h External variables retain their values throughout the execution of the program External variables should be used sparingly, if at all If an external variable is assigned an incorrect value, it is difficult to identify the 2 guilty function Functions that use external variables are hard to reuse
Local Variables • Variables declared inside a function are local to that function Example: float max(float a, float b) { float big; /* big is a local variable */ float big; / big is a local variable / if (a > b) { big = a; } else { big = b; } return big; } • Local variable exists only when the enclosing function is executing L l i bl i i ibl l i h l i f i • 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 3

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

View Full Document
Parameters f h d d l • A function header can contain parameter declarations 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 • It derives its value from the arguments passed at function call time 4
Inside of Blocks • A block of statements can contain declarations 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 5 A block can appear anywhere a statement is allowed

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

View Full Document
Scope Rules • When a declaration inside a block (or function) uses an identifier
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 28

P.Lecture - 2.12.08 - Announcements Assignment 3 out today...

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

View Full Document
Ask a homework question - tutors are online