1 / 95

A First Book of ANSI C Fourth Edition

A First Book of ANSI C Fourth Edition. Chapter 7 Modularity Using Functions: Part II. Objectives. Variable Scope Variable Storage Class Pass by Reference Case Study: Swapping Values Recursion Common Programming and Compiler Errors. 7.1 Variable Scope.

gema
Télécharger la présentation

A First Book of ANSI C Fourth Edition

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. A First Book of ANSI CFourth Edition Chapter 7 Modularity Using Functions: Part II

  2. Objectives • Variable Scope • Variable Storage Class • Pass by Reference • Case Study: Swapping Values • Recursion • Common Programming and Compiler Errors A First Book of ANSI C, Fourth Edition

  3. 7.1 Variable Scope • Now that we have begun to write programs containing more than one function, we can look more closely at the variables declared within each function and their relationship to variables in other functions. • This term refers to the scope of a variable, where scope is defined as the section of the program where the variable is valid or “know”. A First Book of ANSI C, Fourth Edition

  4. Local variables are only meaningful when used in expressions or statements inside the function that declares them. • This means that the same variable name can be declared and used in more than one function. • All the variables we have used until now have been local variables. A First Book of ANSI C, Fourth Edition

  5. A variable with global scope, more commonly termed a global variable, is one whose storage has been created for it by a declaration statement located outside any function. • These variables can be used by all functions in a program that are physically placed after the global variable declaration. • This is shown in Program 7.1 where we have purposely used the same variable name inside both functions contained in the program. A First Book of ANSI C, Fourth Edition

  6. Program 7.1 • #include<stdio.h> • int firtstnum; /*create a global variable named firstnum*/ • void valfun( ); /*function prototype (declaration) */ • int main( ) • { • int secnum; /* create a local variable named secnum */ • firstnum=10; /* store a value into the global variable */ • secnum=20; /* store a value into the local variable */ A First Book of ANSI C, Fourth Edition

  7. printf(“\nFrom main( ) : firstnum = %d”,firstnum); • printf(“\nFrom main( ) : secnum = %d”,secnum); • valfun( ); /* call the function valfun*/ • printf(“\nFrom main( ): firstnum=%d”,firstnum); • printf(“\nFrom main( ): sectnum=%d”,secnum); • return 0; • } A First Book of ANSI C, Fourth Edition

  8. void valfun( ) /* no values are passed to this function */ • { • int secnum; /* create a second local variable named secnum */ • secnum=30; /*this only affects this local variable value */ • printf(“\nFrom valfun( ): firstnum=%d”,firstnum); • printf(“\nFrom valfun( ): sectnum=%d\n”,secnum); • firstnum =40; /*this changes firstnum for both functions */ • return; • } A First Book of ANSI C, Fourth Edition

  9. The variable firstnum in Program 7.1 is a global variable because its storage is created by a declaration statement located outside a function. • Since both functions, main( ) and valfun ( ), follow the declaration of firstnum, both of the these functions can use this global variable with no further declaration needed. A First Book of ANSI C, Fourth Edition

  10. Program 7.1 also contains two separate local variables, both named secnum. • Storage for the secnum variable named in main( ) is created by the declaration statement located in main ( ). • A different storage area for the secnum variable in valfun ( ) is created by the declaration statement located in the valfun( ) function. A First Book of ANSI C, Fourth Edition

  11. Program 7.1 produces the following output. • From main( ) : firstnum = 10 • From main( ) : sectnum = 20 • From valfun( ) : firstnum =10 • From valfun( ) : sectnum = 30 • From main( ) again : firstnum = 40 • From main( ) again :secnum = 20 A First Book of ANSI C, Fourth Edition

  12. The scope of a variable is determined solely by the placement of the declaration statement that reserves storage for it, while the data type of the variable is determined by using the appropriate keyword( char, int, float, double, etc.) before the variable’s name in a declaration statement. A First Book of ANSI C, Fourth Edition

  13. Variable Scope (continued) A First Book of ANSI C, Fourth Edition

  14. 7.2 Variable Storage Class • The scope of a variable defines the location within a program where that variable can be used. • From this viewpoint, the scope of a variable can be thought of as the space within the program where the variable is valid. A First Book of ANSI C, Fourth Edition

  15. The four available storage classes are called auto, static, extern, andregister. • If one of these class names is used, it must be placed before the variable’s data type in a declaration statement. • To understand what the storage class of a variable means, we will first consider local variables (those variables created inside a function) and then global variables (those variables created outside a function). A First Book of ANSI C, Fourth Edition

  16. Local Variable Storage Classes • Local variables can only be members of the auto, static or register storage classes. • If no class description is included in the declaration statement, the variable is automatically assigned to the auto class. • Thus, auto is the default class used by C. • All the local variables we have used, since the storage class designation was omitted, have been auto variables. A First Book of ANSI C, Fourth Edition

  17. As long as the function has not returned control to its calling function, all automatic variables local to the function are “alive”-----that is , storage for the variables is available. • When the function returns control to its calling function, its local automatic variables “die”--that is, the storage of the variables is released back to the operating system. • This process repeats itself each time a function is called. A First Book of ANSI C, Fourth Edition

  18. Program 7.2 • #include<stdio.h> • void testauto ( ); /*function prototype */ • int main ( ) • { • int count; /* create the auto variable count */ • for (count = 1; count <=3; ++count) • testauto ( ); • return 0; • } A First Book of ANSI C, Fourth Edition

  19. void testauto ( ) • { • int num =0; /* create the auto variable num */ • /* and initialize to zero */ • printf(“\nThe value of the automatic variable num is %d”, num); • ++num; • return; • } A First Book of ANSI C, Fourth Edition

  20. Program 6.7 produces the following: • The value of the automatic variable num is 0 • The value of the automatic variable num is 0 • The value of the automatic variable num is 0 A First Book of ANSI C, Fourth Edition

  21. Local Variable Storage Classes (continued) Output is: The value of the automatic variable num is 0 The value of the automatic variable num is 0 The value of the automatic variable num is 0 A First Book of ANSI C, Fourth Edition

  22. There are cases , however, where we want a function to remember values between function calls. • This is the purpose of the static storage class. • A local variable that is declared as static causes the program to keep the variable and its latest value even when the function that declared it is through executing. A First Book of ANSI C, Fourth Edition

  23. Examples of static variable declarations are • static int rate; • static float taxes; • static double amount; • static char inKey; • static long years; A First Book of ANSI C, Fourth Edition

  24. A local static variable is not created and destroyed each time the function declaring the static variable is called. • Once created, local static variables remain in existence for the life of the program. • This means that the last value stored in the variable when the function is finished executing is available to the function the next time it is called. A First Book of ANSI C, Fourth Edition

  25. The initialization of static variables (both local and global ) is done only once, when the program is first compiled. • At compilation time, the variable is created and any initialization value is placed in it. • Thereafter, the value in the variable is kept without further initialization each time the function is called. • To see this works, consider Program 7.3. A First Book of ANSI C, Fourth Edition

  26. Program 7.3 • #include<stdio.h> • void teststat ( ); /*function prototype */ • int main ( ) • { • int count; /* count is a local auto variable */ • for (count = 1; count <=3; ++count) • teststat ( ); • return 0; • } A First Book of ANSI C, Fourth Edition

  27. void teststat ( ) • { • static int num =0; /* num is a local static variable */ • printf(“\nThe value of static variable num is now %d”, num); • ++num; • return; • } A First Book of ANSI C, Fourth Edition

  28. The output produced by Program 6.8 is • The value of static variable num is now 0 • The value of static variable num is now 1 • The value of static variable num is now 2 A First Book of ANSI C, Fourth Edition

  29. Local Variable Storage Classes (continued) Output is: The value of the static variable num is now 0 The value of the static variable num is now 1 The value of the static variable num is now 2 A First Book of ANSI C, Fourth Edition

  30. The remaining storage class available to local variables, the register class, is not used as extensively as either the automatic or static variable classes. • The only restriction in using the register storage class is that the address of a register variable, using the address operator &, cannot be taken. • This is easily understood when you realize that registers do not have standard memory addresses. A First Book of ANSI C, Fourth Edition

  31. Global Variable Storage Classes • Global variables are created by declaration statements external to a function. • By their nature, these externally defined variables do not come and go with the calling of any function. • Once an external (global) variable is created, it exists until the program in which it is declared is finished executing. A First Book of ANSI C, Fourth Edition

  32. Examples of declaration statements including these two class descriptions are • externint num; • extern double price; • static double yield; A First Book of ANSI C, Fourth Edition

  33. The purpose of the extern storage class is to extend the scope of a global variable beyond its normal boundaries. • Larger programs typically consist of many functions that stored in multiple files. • An example of this is shown in Figure 7.6, where the three functions main ( ), func1 ( ), and func2 ( ) are stored in one file and the two functions func3 ( ) and func4 ( ) are stored in a second file. A First Book of ANSI C, Fourth Edition

  34. For the files illustrated in Figure 7.6, the global variables price, yield, and coupon declared in file1 can only be used by the functions main ( ), func1( ), and func2 ( ) in this file. • The single global variable, interest, declared in file2 can only be used by the functions func3 ( ) and func4 ( ) in file2. A First Book of ANSI C, Fourth Edition

  35. Although the variable price has been created in file1, we may want to use it in file2. • Placing the declaration statement extern int price; in file2, as shown in Figure 7.7, allows us to do this. • Putting this statement at the top of file2 extends the scope of the variable price into file2 so that it may be used by both func3 ( ) and func4 ( ). A First Book of ANSI C, Fourth Edition

  36. Similarly, placing the statement extern float yield; in func4( ) extends the scope of this global variable, created in file1, into func4 ( ). • The scope of the global variable interest, created in file2, is extended into func1 ( ) and func2 ( ) by the declaration statement extern float interest; placed before func1 ( ). • Notice interest is not available to main ( ). A First Book of ANSI C, Fourth Edition

  37. The last global class, static, is used to prevent the extension of a global variable into a second file. • Global static variables are declared in the same way as local static variables, except that the declaration statement is placed outside any function. A First Book of ANSI C, Fourth Edition

  38. The scope of a global static variable cannot be extended beyond the file in which it is declared. • Static global variables cannot be subsequently extended to a second file using an extern declaration statement. • Trying to do so results in a compilation error. A First Book of ANSI C, Fourth Edition

  39. Global Variable Storage Classes (continued) A First Book of ANSI C, Fourth Edition

  40. Global Variable Storage Classes (continued) A First Book of ANSI C, Fourth Edition

  41. 7.3 Passing by Reference • In the normal course of operation, a called function receives values from its calling function, stores the passed values in its own local parameters, manipulates these parameters appropriately, and possibly returns a single value. • This method of calling a function and passing values to it is referred to as a function pass by value. A First Book of ANSI C, Fourth Edition

  42. Passing Addresses to a Function • To pass, store, and use addresses requires the use of the address operator(&), pointers, and the indirection operator(*). • Passing addresses to a function should be familiar to you, because we have been using addresses each time we called the scanf ( ) function. A First Book of ANSI C, Fourth Edition

  43. Passing Addresses to a Function • Output is: num = 22 The address of num is 124484 A First Book of ANSI C, Fourth Edition

  44. Storing Address • Besides displaying the address of a variable, as was done in Program 7.4, we can also store addresses in suitably declared variables. • For example, the statement • numAddr =&num; • stores the address corresponding to the variable num in the variable numAddr, as illustrated in Figure 7.9. A First Book of ANSI C, Fourth Edition

  45. Similarly, the statements • d=&m; • tabPoint=&list; • chrPoint=&ch; • store the addresses of the variables m, list, and ch in the variables d, tabPoint, and chrPoint respectively. A First Book of ANSI C, Fourth Edition

  46. Pointer Variables • The variables numAddr , d, tabPonit, and chrPiont are all called pointer variables, or pointers, for short. • Pointers are simply variables that are used to store the addresses of other variables. A First Book of ANSI C, Fourth Edition

  47. Using Addresses • To use a stored address, C provides us with an indirection operator, *(asterisk). • The * symbol, when followed immediately by a pointer ( no space allowed between the * and the pointer), means the variable whose address is stored in. • Thus, if numAddr is a pointer (remember that a pointer is a variable that contains an address), * numAddr means the variable whose address is stored in numAddr, A First Book of ANSI C, Fourth Edition

  48. Similarly, *tabPoint means the variable whose address is stored in tabPoint, and *chrPoint means the variable whose address is stored in chrPoint. • Figure 7.11shows the relationship between the address contained in a pointer variable and the variable ultimately addressed. A First Book of ANSI C, Fourth Edition

  49. A Pointer Variable y: • The Contents • of y is FFAA • The Contents of an Address • Address FFAA FFAA • is qqqq • Figure 7.11 using a pointer variable FFAA qqqq A First Book of ANSI C, Fourth Edition

  50. Although *d literally means the variable whose address is stored in d, this is commonly shortened to the variable pointed to by d. • Similarly, referring to Figure 7.11, *y can be read as the variable pointed to by y. • The value ultimately obtained, as shown in Figure 7.11, is qqqq . A First Book of ANSI C, Fourth Edition

More Related