1 / 13

int log2(int n) { int log = 0; /* local variable */ while (n > 1) { n /= 2;

Chapter 10. Local Variables. A variable declared in the body of a function is said to be local to the function. In the following function, log is a local variable :. int log2(int n) { int log = 0; /* local variable */ while (n > 1) { n /= 2; log++;

sstahl
Télécharger la présentation

int log2(int n) { int log = 0; /* local variable */ while (n > 1) { n /= 2;

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. Chapter 10 Local Variables A variable declared in the body of a function is said to be local to the function. In the following function, log is a local variable: int log2(int n) { int log = 0; /* local variable */ while (n > 1) { n /= 2; log++; } return log; } 1

  2. By default, local variables have the following properties: • Automatic storage duration. The storage duration (or extent) of a variable is the portion of program execution during which storage for the variable exists. Storage for a local variable is "automatically" allocated when the function is called and deallocated when the function returns. • Block scope. The scope of a variable is the portion of the program text in which the variable can be referenced. A local variable has block scope: it is visible from its point of declaration to the end of the enclosing function body Since the scope of a local variable does not extend beyond the function to which it belongs, other functions can use the same name for other purposes. 2

  3. Recall, in automatic storage duration the storage for a local variable is "automatically" allocated when the function is called and deallocated when the function returns. Putting the word static in the declaration of a local variable causes it to have static storage duration instead of automatic storage duration. A variable with static storage duration has a permanent storage location, so it retains its value throughout the execution of the program. Parameters Parameters have the same properties as local variables(automatic storage duration and block scope). The only difference between parameters and local variables is that parameters are initialized automatically each time the function is called. The parameters are assigned the values of the arguments in the call. 3

  4. External Variables Passing arguments is one way to transmit information to a function. (such as passing by value). Functions can also communicate through external variables, variables that are declared outside the body of any function. The properties of external variables (or global variables, as they're sometimes called) are different from those of local variables: Properties of external variables (1) Static storage duration. External variables have static storage duration, just like local variables that have been declared static. A value stored in an external variable will stay there indefinitely. 4

  5. (2) An external variable has file scope: it is visible from its point of declaration to the end of the enclosing file. As a result, an external variable can be accessed by all functions that follow its declaration. The external variable name can not be re-used since its scope is global. vs. A local variable has block scope: it is visible from its point of declaration to the end of the enclosing function body The scope of a local variable does not extend beyond the function to which it belongs. The local variable names can be re-used outside of the enclosing function. 5

  6. Pros and Cons of External Variables External variables are convenient when many functions must share a variable or when a few functions share a large number of variables. However, it is better for functions to communicate through parameters rather than by sharing variables. Here's why: • If an external variable is changed during program modification then every function in the same file will need to be checked to see how the change affects it. • If an external variable is assigned an incorrect value, it may be difficult to identify the guilty function. • Functions that rely on external variables are hard to reuse in other programs. A function that depends on external variables is not "self-contained"; to use the function in another program, all external variables that the function needs will also have to come along. 6

  7. Blocks C allows compound statements to contain declarations as well: { declarations statements } The term block is used to describe such a compound statement. Here's an example of a block: if (i > j) { int temp; /* declaration */ temp = i; /* statements */ i = j; j = temp; } By default, the storage duration of a variable declared in a block is automatic: storage for the variable is allocated when the block is entered and deallocated when the block is exited. The variable has block scope; it can not be referenced outside the block. 7

  8. The body of a function is a block. Blocks are also useful inside a function body when we need variables for temporary use. Putting temporary variables in blocks has two advantages: (1) It avoids cluttering the declarations at the beginning of the function body with variables that are used only briefly. (2) It reduces name conflicts. In our example, the name temp can be used elsewhere in the same function for different purposes, the variable temp declared in the block is strictly local to the block. 8

  9. Scope - When a declaration inside a block names an identifier that is already visible the new declaration temporarily "hides" the old one, and the identifier takes on a new meaning. At the end of the block, the identifier regains its old meaning. The below identifier i has four different meanings: int i; /* Declaration 1 - external variable */ void f(int i) /* Declaration 2 - parameter of void function f */ { i = 1; /* statement of void function f */ } void g(void) { int i = 2; /* Declaration 3 - local parameter of function g */ if (i > 0) { /* begin block */ int i; /* Declaration 4 - local to block */ i = 3; /* statement within block */ } /* end block */ i = 4; /* statement in void function g */ } void h(void) { i = 5; /* statement in void function h */ } 9

  10. In Declaration 1, i is a variable with static storage duration and file scope. In Declaration 2, i is a parameter with block scope (within function f). In Declaration 3, i is an automatic variable with block scope (within function g). In Declaration 4, i is automatic variable and has block scope (within the if block) int i; /* Declaration 1 - external variable */ void f(int i) /* Declaration 2 - parameter of void function f */ { i = 1; /* statement of void function f */ } void g(void) { int i = 2; /* Declaration 3 - local parameter of function g */ if (i > 0) { /* begin block */ int i; /* Declaration 4 - local to block */ i = 3; /* statement within block */ } /* end block */ i = 4; /* statement in void function g */ } void h(void) { i = 5; /* statement in void function h */ } 10

  11. • The i = 1 assignment refers to the parameter in Declaration 2, not the variable in Declaration 1, since Declaration 2 hides Declaration 1. • The i > 0 test refers to the variable in Declaration 3, since Declaration 3 hides Declaration 1 and Declaration 2 is out of scope. • The i = 3 assignment refers to the variable in Declaration 4, which hides Declaration 3. The i = 4 assignment refers to the variable in Declaration 3. It can't refer to Declaration 4, which is out of scope. The i = 5 assignment refers to the variable in Declaration 1. int i; /* D1 */ void f(int i) /* D2 */ { i = 1; /* statement */ } void g(void) { int i = 2; /* D3 */ if (i > 0) { /* begin block */ int i; /* D4 */ i = 3; /* statement */ } /* end block */ i = 4; /* statement */ } void h(void) { i = 5; /* statement */ } 11

  12. Organizing a C Program For now, a program always fits into a single file. Chapter 15 shows how to organize a program that is split over several files. A program may contain the following: Preprocessor directives: #include and #define Type definitions Declarations of functions Function definitions External variables C imposes a few rules on the order of these items: A preprocessor directive does not take effect until the line at which it appears. A type name can not be used until it has been defined. A variable can not be used until it is declared. A function should be defined or declared prior to its first call. 12

  13. Order C programs in the following way: #include directives #define directives Type definitions Declarations of external variables Prototypes for functions other than main Definition of main Definitions of other functions Put #include directives first, since they bring in information that will likely be needed in several places within the program. #define directives create macros, which are used throughout the program. Put type definitions above the declarations of external variables since the declarations of these variables may refer to the type names just defined. Precede each function definition by a boxed comment that gives the name of the function, describes its purpose, discusses the meaning of each parameter, describes the return value, and lists any side effects. 13

More Related