Unformatted text preview:

10. PROGRAM ORGANIZATIONLocal VariablesSlide 3External VariablesSlide 5BlocksSlide 7ScopeSlide 9Function DeclarationsSlide 11Organizing a C ProgramExample: Guessing a NumberSlide 14Slide 1510. PROGRAM ORGANIZATIONLocal 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;elsebig = b;return big;}• A local variable exists only when the enclosing function is executing.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.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;}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.Blocks• C allows a compound statement to contain declarations as well:{declarationsstatements}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;}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.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, theidentifier regains its old meaning.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;}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;}Function Declarationsfloat max(float a, float b) /* definition of function */{if (a > b)return a;elsereturn 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.Organizing a C Program• A suggested layout for a C program:#include directives#define directivesDeclarations of external variablesDeclarations of functions other than mainDefinition of mainDefinitions of other functionsExample: Guessing a Number#include <stdio.h>#include <stdlib.h>#include <time.h>int hidden_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");}Example: Guessing a Number{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");elseprintf("Too high; try again.\n");}}Example: Guessing a Numberint 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


View Full Document

UF CGS 3460 - PROGRAM ORGANIZATION

Download PROGRAM ORGANIZATION
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view PROGRAM ORGANIZATION and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view PROGRAM ORGANIZATION 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?