CHAPTER 8 Scope, Lifetime, and More on Functions.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Modular Programming With Functions
Programming Languages and Paradigms
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 Scope, Lifetime, and More on Functions. 2 Chapter 8 Topics  Local Scope vs. Global Scope of an Identifier  Detailed Scope Rules to Determine which.
Methods Review. 2. Class-wide vs. local variables. 3. Why C# bans global variables. 4. Nested blocks. 5. Scope of identifiers.
chap13 Chapter 13 Programming in the Large.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
18. DECLARATIONS.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Cosc175/module.ppt1 Introduction to Modularity Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
chapter 8 Scope,Lifetime,and More on Functions
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
The Three Attributes of an Identifier
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
User-defined Functions
Scope, Parameter Passing, Storage Specifiers
Chapter 9 Scope, Lifetime, and More on Functions
User Defined Functions
User-defined Functions
Predefined Functions Revisited
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
The Three Attributes of an Identifier
Scope Rules.
Presentation transcript:

CHAPTER 8 Scope, Lifetime, and More on Functions

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

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

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() { : }

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

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 :

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

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.

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

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

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

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

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.

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;

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

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

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

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

Global Constants const double FICA = ; 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

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);

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

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

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; }

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

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+angle ) < ); }

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

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))

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.

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

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

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

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)