1 / 16

10. PROGRAM ORGANIZATION

10. PROGRAM ORGANIZATION. 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; }

simone
Download Presentation

10. PROGRAM ORGANIZATION

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 10. PROGRAM ORGANIZATION

  2. 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.

  3. Local Variables • A local variable is visible only to statements in the enclosing function. • Names of local variables can be used for other purposes in the same program.

  4. External Variables • • Variables declared outside functions are said to be external (or global). • • One advantage of external variables is that they can be shared by several functions: • #include <stdio.h> • int i; /* i is an external 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; • }

  5. External Variables • Another advantage: external variables retain their values throughout the execution of the program. • External variables should be used sparingly, for the following reasons: Making a change to an external variable (changing its type, for example) requires checking every function to see how the change will affect it. 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.

  6. Blocks • • C allows a compound statement to contain declarations as well: • { • declarations • statements • } • This is called a block. • • An example of a block inside an if statement: • if (i < j) { • int temp; • temp = i; • i = j; • j = temp; • }

  7. Blocks • 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. • The body of a function is a block. • In C99, declarations do not have to go at the beginning of a block (including a function body). Declarations and statements can be mixed, provided that there are no references to a variable prior to its declaration.

  8. Scope • When a declaration inside a block names 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.

  9. Scope • • Example: • int i; /* Declaration 1 */ • void f(int i) /* Declaration 2 */ • { • i = 1; • } • void g(void) • { • int i; /* Declaration 3 */ • if (i > 0) { • int i; /* Declaration 4 */ • i = 2; • } • i = 3; • } • void h(void) • { • i = 4; • }

  10. Function Declarations • Whenever a function call precedes the definition of the function, always declare the function first: #include <stdio.h> float max(float a, float b); /* declaration of function */ int main(void) { int x, y; printf("Enter two numbers: "); scanf("%d%d", &x, &y); printf("The larger number is %g\n", max(x, y)); return 0; }

  11. Function Declarations float max(float a, float b) /* definition of function */ { if (a > b) return a; else return b; } • The declaration of a function need not include the names of the parameters: float max(float, float); • Function declarations are often called prototypes. • In C99, calling a function without first defining or declaring it is an error.

  12. Organizing a C Program • A suggested layout for a C program: #include directives #define directives Declarations of external variables Declarations of functions other than main Definition of main Definitions of other functions

  13. Example: Guessing a Number #include <stdio.h> #include <stdlib.h> #include <time.h> int hidden_number; void initialize_number_generator(void); void choose_new_hidden_number(void); void read_guesses(void);

  14. Example: Guessing a Number int main(void) { char command; printf("Guess the hidden number between 1 and 100.\n\n"); initialize_number_generator(); do { choose_new_hidden_number(); read_guesses(); printf("Play again? (Y/N) "); scanf(" %c", &command); printf("\n"); } while (command == 'y' || command == 'Y'); return 0; }

  15. Example: Guessing a Number void initialize_number_generator(void) { srand(time(NULL)); } void choose_new_hidden_number(void) { hidden_number = rand() % 100 + 1; printf("A new hidden number has been chosen.\n"); }

  16. Example: Guessing a Number void read_guesses(void) { int guess, num_guesses = 0; for (;;) { num_guesses++; printf("Enter guess: "); scanf("%d", &guess); if (guess == hidden_number) { printf("You won in %d guesses!\n\n", num_guesses); return; } else if (guess < hidden_number) printf("Too low; try again.\n"); else printf("Too high; try again.\n"); } }

More Related