Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 8 Scope, Lifetime, and More on Functions.

Similar presentations


Presentation on theme: "CHAPTER 8 Scope, Lifetime, and More on Functions."— Presentation transcript:

1

2 CHAPTER 8 Scope, Lifetime, and More on Functions

3 Scope l The region of program code l where it is legal l to reference an identifier if (alpha > 3) { int n; cin >> n; beta = beta + n; } scope of n

4 Local Scope l The scope of an identifier l declared inside a block l extends from the point of declaration l to the end of that block. if (alpha > 3) { int n; cin >> n; beta = beta + n; } scope of n

5 Global Scope l The scope of an identifier declared outside all functions l extends from the point of declaration l to the end of the entire file containing the program code int gamma; // Glo Var int main() { : } void SomeFunc() { : }

6 Name Precedence l The precedence that »A local identifier in a function has »over a global identifier »with the same name »in any references that the function makes »to the identifier l a.k.a. Name Hiding l s:\cp1\cpp\LocalGlo.cpp

7 Nonlocal Identifier l Nonlocal Identifier »Any identifier »declared outside a given block »is nonlocal with respect to that block int i = 5; { int j = 8; i = j * 9;// i is nonlocal to this block :

8 Scope Rules l Scope rules »Rules that determine »where in a program »a given identifier may be accessed »given the point »where that identifier is declared

9 Scope Rules 1. A function name has global scope. 2. A formal parameter's scope equals a local variable's scope in the outermost block of function body 3. The scope of global variables and constants extends from declaration to end of file. 4. Local variable scope extends from its declaration to the end of block in which it is declared, including nested blocks 5. Local identifiers have name precedence.

10 Scope Rules l See scope rules program (p. 394)

11 extern l allows one to reference a global variable located in another file extern int someInt; »someInt is a global variable »located in another file »no storage should be reserved for it here »a declaration, not a definition

12 Lifetime l The period of time l during program execution l when a variable l has memory allocated to it.

13 Automatic Variable l A variable for which l memory is allocated and deallocated l when control enters and exits the block l in which it is declared

14 Automatic Variable The storage class specifier auto can be prepended to their declarations to declare they are automatic. auto int total = 0; This is not required since auto is the default.

15 Static Variable l A variable for which l memory remains allocated l throughout the execution l of the entire program l Static objects use the static specifier: l static int Number = 0;

16 Unlike auto vars, static variables: l Retain their value from one execution of the block in which they are declared to the next. l Where used (scope) »same as auto : limited to block in which declared static variables »used in functions in which »we want a local variable to retain its value »from one call of the function to the next

17 Initialization AutoStatic Initialized when?Each time control Once only, the first time reaches the control reaches thedeclaration InitializerAny expressionConstant expression only See s:\cp1\cpp\scope.cpp

18 Interface Design ParameterData Flow pass-by-valueIncoming pass-by-referenceOutgoing pass-by-reference Incoming/Outgoing

19 What is a side effect? »An effect of one function on another »not a part of the explicitly defined interface »between them »Trouble.cpp

20 Global Constants const double FICA = 0.0675; l Easy to change l Program consistency l Good programming practice »If needed in many functions »If needed in only one, put const in that function

21 Value Returning Functions l Value Returning Function »receives data through parameter list »computes a single function value »returns this value to the calling code y = 3.8 * sqrt(x);

22 Function Value Type l The data type of the result value returned by a function l a.k.a. »function return type »function result type

23 Integer Exponentiation Function int Power (/* in */ int x,// base /* in */ int n) // power // This function computes x to the n power // Precond & Postcond p.410 { int result; // holds intermediate powers of x result = 1; while (n > 0) { result = result * x; n--; } return result; }// See Power.cpp

24 Integer Factorial Function int Factorial (/* in */ int x) // This function computes x! // Precond & Postcond p.411 { int result; // holds partial products result = 1; while (x > 0) { result = result * x; x--; } return result; }

25 Boolean Function l A function which »evaluates a condition »returns a boolean result

26 Boolean Function Example Boolean IsTriangle( /* in */ float angle1, // 1st angle /* in */ float angle2, // 2nd angle /* in */ float angle3 ) // 3rd angle // This function checks to see if its three incoming values // add up to 180 degrees, forming a valid triangle // Precond & Postcond p.412 { return (fabs(angle1+angle2+angle3-180.0) < 0.00000001); }

27 Ctype.h Library char argument; returns int (boolean) isalpha(ch)'A'-'Z', 'a'-'z' isalnum(ch)isalphaor'0'-'9' isdigit(ch)'0'-'9' islower(ch)'a'-'z' isupper(ch)'A'-'Z' isspace(ch)blank, tab, newline, CR, FF

28 Giving Functions a Name l Void Functions »Imperative verbs »PrintResults(a, b, c); »DoThis(x); l Value-returning Functions »Nouns or adjectives »z = 6.7 * Maximum(d,e,f); l Boolean Functions »Adjectives or phrases beginning with Is »while (Valid(m,n)) »if (IsTriangle(a,b,c))

29 Void vs. Value Functions l If returning more than one value or modify actual parameters, use void. l If module performs I/O use void. l If only one Boolean value is returned, use value. l If only one value is returned and it is to be used immediately in an expression, use value. l When in doubt, use void. l If both are acceptable, use the one you feel most comfortable implementing.

30 Control Abstraction l The separation l of the logical properties l of an action l from its implementation

31 Functional Cohesion l The principle that a module l should perform l exactly one abstract function

32 Communication Complexity l A measure l of the quantity of information l passing through l a module's interface

33 Define stub, driver. l Stub »A dummy procedure or function that assists in testing part of a program (p. 446) l Driver »A simple main function used to call a function begin tested (p. 447)

34


Download ppt "CHAPTER 8 Scope, Lifetime, and More on Functions."

Similar presentations


Ads by Google