Download presentation
Presentation is loading. Please wait.
Published byRalph Chapman Modified over 9 years ago
1
Chapter 7 Modularity Using Functions: Part II
2
A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available only to the function itself, they are said to be local to the function, or local variables Scope is the section of the program where the variable is valid or “known”
3
A First Book of ANSI C, Fourth Edition 3 Variable Scope (continued) A variable with a local scope has had storage set aside for it by a declaration statement made within a function body A variable with global scope is one whose storage has been created for it by a declaration statement located outside any function
4
A First Book of ANSI C, Fourth Edition 4 Variable Scope (continued)
5
A First Book of ANSI C, Fourth Edition 5 Variable Scope (continued) Program 7.1 produces the following output From main(): firstnum = 10 From main(): secnum = 20 From valfun(): firstnum = 10 From valfun(): secnum = 30 From main() again: firstnum = 40 From main() again: secnum = 20 While a function is executing, only the storage area for the variables and parameters created by this function are automatically accessed
6
A First Book of ANSI C, Fourth Edition 6 Variable Scope (continued) If a variable that is not local to the function is used by the function, the program searches the global storage areas for the correct name The scope of a variable does not influence the data type of the variable
7
A First Book of ANSI C, Fourth Edition 7 When to Use Global Declarations The scoping rules for symbolic constants and function prototypes are the same as for variables When a symbolic constant has a general meaning that is applicable throughout an application, it makes good programming sense to declare it globally at the top of a source code file Coding a function prototype as a global makes sense when the function is used by a number of other functions in a source code file –Doing so avoids repeating the prototype within each of the functions that will call it
8
A First Book of ANSI C, Fourth Edition 8 Misuse of Global Variables Except for symbolic constants and prototypes, global variables should almost never be used By making a variable global, you instantly destroy the safeguards C provides to make functions independent and insulated from each other Using global variables can be especially disastrous in large programs with many user- created functions –Because a global variable can be accessed and changed by any function following the global declaration, it is a time-consuming and frustrating task to locate the origin of an erroneous value
9
A First Book of ANSI C, Fourth Edition 9 Pass by Reference In pass by value, a called function receives values from its calling function, stores the passed values in its own local parameters, manipulates these parameters appropriately, and directly returns, at most, a single value Passing an address is referred to as a function pass by reference, because the called function can reference, or access, the variable using the passed address –Also referred to as a call by reference when the term applies only to those parameters whose addresses have been passed
10
A First Book of ANSI C, Fourth Edition 10 Passing Addresses to a Function Output is: num = 22 The address of num is 124484
11
A First Book of ANSI C, Fourth Edition 11 Storing Addresses numAddr = # A variable that can store an address is known as a pointer variable or pointer
12
A First Book of ANSI C, Fourth Edition 12 Using Addresses Indirection operator: * –*numAddr means the variable whose address is stored in numAddr Or, the variable pointed to by numAddr When using a pointer, the value obtained is always found by first going to the pointer for an address; this is called indirect addressing
13
A First Book of ANSI C, Fourth Edition 13 Declaring and Using Pointers In declaring a pointer variable, C requires that we also specify the type of variable that is pointed to –int *numAddr; This declaration can be read in a number of ways: as the variable pointed to by numAddr is an integer, or as numAddr points to an integer
14
A First Book of ANSI C, Fourth Edition 14 Output is: The address stored in milesAddr is 1244872 The value pointed to by milesAddr is 22 The value in miles is now 158 Declaring and Using Pointers
15
A First Book of ANSI C, Fourth Edition 15 Passing Addresses to a Function Sample run of Program 7.6: Enter a number: 24.6 The address that will be passed is 124484 The address received is 124484 The value pointed to by xnum is: 24.60
16
A First Book of ANSI C, Fourth Edition 16 Returns multiple values Passing Addresses to a Function
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.