Scope and Parameter Passing

Similar documents
Scope and Parameter Passing

The Class Construct Part 1

Dynamic Allocation of Memory

The Critical-Path Algorithm

Friends and Unary Operators

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

LR Parsing - Conflicts

Minimal Spanning Trees

The Traveling Salesman Problem Brute Force Method

The Decreasing-Time Algorithm

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

The Class Construct Part 2

Programming Languages

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

The Plurality-with-Elimination Method

List Iterator Implementation

Solving Recursive Sequences by Iteration

Stacks and their Applications

Magnification and Minification

Recursive Linked Lists

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

The Graphics Pipeline

Dynamic Allocation of Memory

Building the Abstract Syntax Trees

Scheduling and Digraphs

The Pairwise-Comparison Method

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Recognition of Tokens

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

The Graphics Pipeline

Sampling Distribution Examples Sections 15.4, 15.5

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Rotations and Translations

Programming Languages

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Street-Routing Problems

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

The CYK Parsing Algorithm

Implementing Linked Lists

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Recursive Descent Parsers

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Fundamental Data Types

Function Definition Syntax Tree

XQuery FLOWR Expressions Lecture 35

Density Curves Sections

Nondeterministic Programming in C++

Inheritance: The Fundamental Functions

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Inheritance: The Fundamental Functions

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The Coefficient of Determination

Ambient and Diffuse Light

Displaying Distributions - Quantitative Variables

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

LR Parsing - The Items

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

The Projection Matrix

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Basic PHP Lecture 17

PHP Queries and HTML Forms Lecture 23

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

The Projection Matrix

The x86 Architecture

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Introduction to Compiler Design

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

Introduction to Databases

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Abstract Syntax Trees Synthetic and Inherited Attributes

XML. Lecture 29. Robb T. Koether. Fri, Mar 28, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XML Fri, Mar 28, / 23

BITG 1113: Function (Part 2) LECTURE 5

Binary Tree Applications

The Standard Template Library Classes

CSI33 Data Structures

Relational Databases Lecture 2

The x87 Floating-Point Unit

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

The Mesh Class. Lecture 23. Robb T. Koether. Hampden-Sydney College. Wed, Oct 18, 2017

Array Elements as Function Parameters

Relational Databases

The Normal Distribution

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

Transcription:

Scope and Parameter Passing Lecture 17 Sections 6.5, 6.10, 6.13 Robb T. Koether Hampden-Sydney College Fri, Oct 5, 2018 Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 1 / 33

1 The Scope of an Object Statement Scope Block Scope Function Scope Global Scope 2 Parameter Passing Value Parameters Reference Parameters 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 2 / 33

Outline 1 The Scope of an Object Statement Scope Block Scope Function Scope Global Scope 2 Parameter Passing Value Parameters Reference Parameters 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 3 / 33

The Scope of an Object The scope of an object is the part of the program over which that object is recognized. The scope of an object may be Statement scope A single statement. Block scope A block within a function. Function scope A function. Class scope A class (to be discussed soon). Global scope The entire program. An object cannot be accessed outside of its scope. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 4 / 33

The Scope of an Object When a variable passes out of scope, it ceases to exist. The memory that was allocated to it is deallocated. A variable whose scope is the entire program is called global. All other variables are local (to the statement, block, function, etc.). Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 5 / 33

Outline 1 The Scope of an Object Statement Scope Block Scope Function Scope Global Scope 2 Parameter Passing Value Parameters Reference Parameters 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 6 / 33

Statement Scope A volatile, or unnamed, object is an object that occurs as the value of an expression or a subexpression. For example, a + b is a volatile object in the statement c = a + b; The scope of a volatile object is the statement in which it occurs. When the statement is completely evaluated, the volatile object passes out of scope. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 7 / 33

Outline 1 The Scope of an Object Statement Scope Block Scope Function Scope Global Scope 2 Parameter Passing Value Parameters Reference Parameters 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 8 / 33

Block Scope If an object is declared within a block within a function, its scope is limited to that block. The scope extends from the point of declaration to the end of the block in which the variable is declared. When execution leaves the block, the object passes out of scope. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 9 / 33

Block Scope Block Scope if (a > b) int max = a; // Block scope else int max = b; // Block scope cout << "The max is " << max << endl; This will not work. Why? Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 10 / 33

Block Scope Block Scope if (a > b) int max = a; else int max = b; cout << "The max is " << max << endl; This will not work either. Why? Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 11 / 33

Block Scope Block Scope int max; // Function scope if (a > b) max = a; else max = b; cout << "The max is " << max << endl; This will work. Why? Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 12 / 33

Outline 1 The Scope of an Object Statement Scope Block Scope Function Scope Global Scope 2 Parameter Passing Value Parameters Reference Parameters 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 13 / 33

Function Scope Variables declared within a function are recognized only within that function. The scope extends from the point of declaration to the end of the function. When execution returns from the function, the object passes out of scope. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 14 / 33

Function Scope Function Scope int max(int a, int b) int larger; // Function scope if (a > b) larger = a; else larger = b; return larger; The variable larger is not accessible from anywhere outside of this function. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 15 / 33

Function Scope Variables declared within a function are recognized only within that function. The scope extends from the point of declaration to the end of the function. When execution returns from the function, the object passes out of scope. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 16 / 33

Function Scope Function Scope int max(int a, int b) if (a > b) return a; return b; This does the same thing, but is more concise. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 17 / 33

Function Scope Function Scope int max(int a, int b) return a > b? a b; This also does the same thing, but maybe is too concise. It s kinda hard to read. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 18 / 33

Outline 1 The Scope of an Object Statement Scope Block Scope Function Scope Global Scope 2 Parameter Passing Value Parameters Reference Parameters 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 19 / 33

Global Scope A variable is global if it is declared outside of any function. Global variables are recognized in all functions. Good programming practice strongly discourages the use of global variables. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 20 / 33

Examples of Scope Example ScopeExample.cpp Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 21 / 33

Outline 1 The Scope of an Object Statement Scope Block Scope Function Scope Global Scope 2 Parameter Passing Value Parameters Reference Parameters 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 22 / 33

Methods of Passing Parameters Parameters may be passed by one of two methods. By value. By reference. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 23 / 33

Outline 1 The Scope of an Object Statement Scope Block Scope Function Scope Global Scope 2 Parameter Passing Value Parameters Reference Parameters 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 24 / 33

Value Parameters When an object is passed by value, the value of the actual parameter is copied to the formal parameter. The function uses the local copy of the parameter. The copy constructor is used to make the copy (to be discussed later). The actual parameter is left unchanged, no matter what is done to the local copy. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 25 / 33

Value Parameters int main() f(a, b) int f(int c, int d) sum = c + d; a b a and b are local to main() Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 26 / 33

Value Parameters int main() f(a, b) int f(int c, int d) sum = c + d; a b Their values are stored locally Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 26 / 33

Value Parameters int main() f(a, b) int f(int c, int d) sum = c + d; a b c d Memory is allocated in f() for the copies Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 26 / 33

Value Parameters int main() f(a, b) int f(int c, int d) sum = c + d; a b c d copy Copies of a and b are made in f() Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 26 / 33

Value Parameters int main() f(a, b) int f(int c, int d) sum = c + d; a b c d copy c and d are local to f() Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 26 / 33

Outline 1 The Scope of an Object Statement Scope Block Scope Function Scope Global Scope 2 Parameter Passing Value Parameters Reference Parameters 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 27 / 33

Reference Parameters When an object is passed by reference, the address of the actual parameter is passed to the function. The formal parameter becomes a reference to the actual parameter, i.e., the actual parameter and the formal parameter are the same object. If the formal parameter is changed, so is the actual parameter. An ampersand & is used in the formal parameter list to indicate a reference parameter. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 28 / 33

Reference Parameters int main() f(a, b) int f(int& c, int& d) sum = c + d; a b a and b are local to main() Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 29 / 33

Reference Parameters int main() f(a, b) int f(int& c, int& d) sum = c + d; a b Their values are stored locally Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 29 / 33

Reference Parameters int main() f(a, b) int f(int& c, int& d) sum = c + d; a b No memory is allocated in f() Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 29 / 33

Reference Parameters int main() f(a, b) int f(int& c, int& d) sum = c + d; a b c and d refer to a and b in main() Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 29 / 33

Outline 1 The Scope of an Object Statement Scope Block Scope Function Scope Global Scope 2 Parameter Passing Value Parameters Reference Parameters 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 30 / 33

Examples of Parameter Passing Examples GCDExample.cpp GetWordFunc.cpp Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 31 / 33

Outline 1 The Scope of an Object Statement Scope Block Scope Function Scope Global Scope 2 Parameter Passing Value Parameters Reference Parameters 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 32 / 33

Assignment Assignment Read Sections 6.5, 6.10, 6.13. Robb T. Koether (Hampden-Sydney College) Scope and Parameter Passing Fri, Oct 5, 2018 33 / 33