Chapter - 9 Variable Scope and Functions. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 1

Similar documents
Chapter 9. Variable Scope and Functions

Fast Introduction to Object Oriented Programming and C++

G Programming Languages - Fall 2012

Functions and Recursion

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What are 3 differences between C and C++ const variables?

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Object Oriented Design

Chapter 15 - C++ As A "Better C"

C Functions. Object created and destroyed within its block auto: default for local variables

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Chapter 3 - Functions

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

EECS402 Lecture 02. Functions. Function Prototype

Pointers, Arrays and Parameters

Ch. 11: References & the Copy-Constructor. - continued -

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

LECTURE 19. Subroutines and Parameter Passing

User Defined Functions

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

CS201 Some Important Definitions

Scientific Computing

Scope. Chapter Ten Modern Programming Languages 1

C++ For Science and Engineering Lecture 15

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

6. Pointers, Structs, and Arrays. 1. Juli 2011

Ch. 12: Operator Overloading

Chapter 2. Procedural Programming

III. Classes (Chap. 3)

Object-Oriented Principles and Practice / C++

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Chapter 1 Getting Started

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Functions in C C Programming and Software Tools

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Programming Languages

Pointers and References

FORM 2 (Please put your name and form # on the scantron!!!!)

COEN244: Class & function templates

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

CSE 307: Principles of Programming Languages

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

PIC 10A. Lecture 17: Classes III, overloading

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

CS 376b Computer Vision

Memory and Pointers written by Cathy Saxton

Lesson 2 Variables and I/O

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Object-Oriented Programming for Scientific Computing

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

CA31-1K DIS. Pointers. TA: You Lu

2 ADT Programming User-defined abstract data types

Object Oriented Programming COP3330 / CGS5409

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

A brief introduction to C++

COMP6771 Advanced C++ Programming

Topic 6: A Quick Intro To C

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Introduction to C++ Systems Programming

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

The role of semantic analysis in a compiler

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

M.EC201 Programming language

EC 413 Computer Organization

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Reliable C++ development - session 1: From C to C++ (and some C++ features)

Variables and numeric types

G Programming Languages - Fall 2012

A Fast Review of C Essentials Part I

Function Overloading

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Chapter 7 - Notes User-Defined Functions II

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Introduction Of Classes ( OOPS )

Short Notes of CS201

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I.

Run Time Environment

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

10. Functions (Part 2)

Lecture on pointers, references, and arrays and vectors

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

A Fast Review of C Essentials Part II

Intermediate Programming, Spring 2017*

Transcription:

Chapter - 9 Variable Scope and Functions Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 1

Variable Scope and Class Variables are defined by two attributes: Scope The area where a variable is valid: Local or Global Storage Class Describes the storage allocation of the variable: Permanent or Temporary Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 2

Variable Scope Global variables are valid everywhere. Local variables are only valid inside the { where they are defined. Scope of global int global;// a global variable int main() { int local; Scope of local global = 1; local = 2; { // beginning a new block Scope int very_local; of = global + local; very_local // Block closed // very_local can not be used return (0); // a local variable // global can be used here // so can local // this is local to the block Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 3

Hidden Variables int total; // Total number of entries int count; // Count of total entries int main() { total = 0; count = 0; scope of local count { int count; // Local counter count = 0; scope of global count while (count < 10) { total += count; ++count; global count is hidden ++count; return (0); Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 4

Storage Class A variable is either permanent or temporary Permanent Variables Declared either as global variables or with the keyword static. Initialized only once (when the program begins). Destroyed only once (when the program ends). Temporary Variables Must be local variables Initialized each time they come into scope Destroyed when they go out of scope Temporary variables are allocated from a memory space called the stack. On PC class machines this is very limited (64K max, sometimes only 4K). Stack Overflow errors a re the result of allocating too many temporary variables. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 5

Permanent vs. Temporary #include <iostream> int main() { int counter; // loop counter for (counter = 0; counter < 3; ++counter) { int temporary = 1; static int permanent = 1; std::cout << "Temporary " << temporary << " Permanent " << permanent << '\n'; ++temporary; ++permanent; return (0); Temporary 1 Permanent 1 Temporary 1 Permanent 2 Temporary 1 Permanent 3 Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 6

Types of Declarations Declared Scope Storage Class Initialized Outside all blocks Global Permanent Once static outside all blocks Global Permanent Once Inside a block Local Temporar y Each time block is entered static inside a block Local Permanent Once Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 7

Namespaces namespace io_stuff { int output_count; // # chars sent int input_count; // # chars in ; int main() { ++io_stuff::output_count;... Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 8

Special namespaces std Used for system variables and classes (std:: cout) -- Global namespace. (Usage: ::global) <blank>file specific namespace (for variables not used outside the file.) Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 9

using don't use using Statement Imports variables from other namespaces into the current code: namespace foo { int foo1; // Something int foo2;// Something else ; using foo::foo1; foo1 = 2; using namespace foo; // Imports all foo1 = foo2 = 3; Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 10

Functions Comments at the beginning of a function: Name -- Name of the function Description -- What the function does Parameters -- Define each of the parameters to the function Return value -- Describe what the function returns /******************************************* * triangle -- compute area of a triangle * * * * Parameters * * width -- width of the triangle * * height -- height of the triangle * * * * Returns * * area of the triangle * *******************************************/ The function itself begins with: float triangle(float width, float height) Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 11

area return float = triangle(float area; size; (area); width //* area height // Size ofwidth, / the 2.0; our float triangle height){ Triangle Function It's use: #include <iostream> int main(){ size = triangle(1.3, 3.3); std::cout << "Area: " << size << "\n"; return (0); Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 12

Parameter Passing and Return size = triangle(1.3, 8.3) Turns into Triangle's variable width = 1.3 Triangle's height = 8.3 Begin execution of the first line of the function triangle. The return statement: return (area); //... size = triangle(1.3, 8.3) Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 13

Function Prototypes (declaration) Just like variables functions must be declared before they can be used. Typical prototype (declaration) float triangle(float width, float height); This function returns a floating point number (the first float) and takes two floating point parameters. This can also be written as: float triangle(float, float); This version is frowned upon because it doesn't tell the reader what the two parameters are. Also it's easier to make the first form by cutting out the first line of the function and pasting it where you need the prototype using your editor. Don't forget to add the semicolon at the end. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 14

Functions with no Declaration: int get_value(); or int get_value(void); parameter The second form is a holdover from C which uses (void) to indicate a function with no parameters and () to indicate a function with no parameter checking (i.e. any number of parameters of any type.) Usage: value = get_value; Functions that return nothing (subroutines) void print_answer(int answer); Usage: print_answer(45); Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 15

const float area return = float area; (area); width height) triangle(const //* area of/ the 2.0; float triangle width, constparameters and Returns Values Constant parameters can not be changed. { It's illegal to change the value of a constant parameter: width = 0.5; // Illegal Constant return values can not be changed. They can be assigned to another variable and changed. As it stands now, constant return values are not useful. A little later we'll see where const makes a difference. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 16

#include // void int std::cout return This a_count inc_counter(int (0); function <iostream> << = a_count 0; won'tcounter) // << work Random '\n'; counter Call by value Ordinary parameters are passed by "call by value." Values go in, but they don't come out. In the following program we try to change the value of count in inc_counter, but it doesn't work. { ++counter; main(){ inc_counter(a_count); Prints: 0 Changes made to simple parameters are not passed back to the caller. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 17

References Revisited Two things occur when we declare a reference parameter such as: int simple; int &ref = simple; // A simple variable // A reference parameter Part one is a reference declaration: int &ref= simple; // A reference parameter This creates a reference declaration. The second part binds the reference to the variable (in this case simple). int &ref = simple; // A reference parameter For simple reference declarations, declaration and binding always occur in one statement. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 18

#include // void int std::cout return Works a_count inc_counter(int (0); <iostream> << = a_count 0; &counter) // << Random '\n'; counter Reference Parameters { ++counter; int main(){ inc_counter(a_count); In this case the declaration and the binding occur at two different places. Changes to counter are reflected in a_count because counter is a reference. In other words counter is the same as a_count. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 19

int // for if biggest return Assume &biggest(int index; biggest; (index (array[biggest]); = the 0; index; = // // 1; first Current Index index array[], < isarray[index]) of < the n_elements; the int biggest n_elements) ++index) element { { Reference Return Values Usage: int item_array[5] = {1, 2, 5000, 3, 4; // An array std::cout << "The biggest element is " << biggest(item_array, 5) << '\n'; Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 20

Reference Return Values (cont.) Remember a reference is treated exactly the same as the real thing. biggest(item_array, 5) is the same as: So: item_array[2] // Zero the largest element biggest(item_array, 5) = 0; is the same as: // Zero the largest element item_array[2] = 0; Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 21

Legal: Constant Reference Return Values int &biggest(int array[], int n_elements); //... biggest(item_array, 5) = 0;// Zero the biggest elem. const return type prevents changing the returned reference. Illegal: const int &biggest(int array[], int n_elements); //... biggest(item_array, 5) = 0;// Generates an error Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 22

23 45 67 89 10 11 12 13 14 15 { #include const main() { if int return int (i1 &i <iostream> &min(const < (i2); = (0); (i1); min(1+2, int 3+4); &i1, const int &i2) Dangling References 4) Bind i1 to return (main's i) 1) tmp1 = 1+2 2) tmp2 = 3+4 3) bind tmp1 to i1, tmp2 to i2, call min 5) Destroy tmp1, tmp2 6) i is bound to tmp1, which is destroyed Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 23

Array Parameters Array parameters are automatically passed by call by address. Changes made to an element of an array will be passed back to the caller. int sum(int array[]); For single dimension arrays you don't need to specify the array size. For multi-dimensional arrays, all dimensions except the last must be specified: int sum_matrix(int matrix1[10][10]); int sum_matrix(int matrix1[10][]); int sum_matrix(int matrix1[][]); // Legal // Legal // Illegal Note: Array parameters are automatically turned to pass by reference. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 24

int * for /* return length Parameters Returns Loop doindex; (index length(char nothing string the until (index); -- = compute we 0; -- */// reach string[index] the string[]){ ofindex thelength into endwhose the of!= ofstring '\0'; alength string ++index) char. we want* Question: Why are All Strings of Length 0 no Matter How Long They Really Are? /************************************************* *************************************************/ /* */ Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 25

Function Overloading int square(int value) { return (value * value); float square(float value) { return (value * value); This is allowed in C++. The language is smart enough to tell the difference between the two versions. (Other languages such as FORTRAN or PASCAL or not.) Function must have different parameter. int get_number(void); float get_number(void); // Illegal. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 26

Style and Function Overloading Functions that are overloaded should perform roughly the same job. For example, all functions named square should square a number. The following is syntactically correct, but a style disaster: // Square an integer int square(int value); // Draw a square on the screen void square(int top, int bottom, int left, int right); Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 27

Default Parameters void draw(const rectangle &rectangle; double scale = 1.0) Tells C++, If scale is not specified, make it 1.0. The following are equivalent: draw(big_rectangle, 1.0); // Explicitly specify scale draw(big_rectangle); // Let it default to 1.0 Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 28

Unused Parameters The following generates a warning: button not used void exit_button(widget &button) { std::cout << "Shutting down\n"; exit (0); We can tell C++ that we have one parameter, a widget that we don t use, by not including a parameter name. void exit_button(widget &) { Good style, however, dictates that we put the parameter name, even as a comment: void exit_button(widget &/* button */) { Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 29

inline functions int square(int value) { return (value * value); int main() { //... x = square(x); Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 30

link mulsl unlk rts label jbsr addqw movel // // Set d1 return(d0) Put Call Restore... Store a6,#0 "void "int a6@(8),d1 = d1,d0 "main" a6@(-4),sp@- #4,sp d0,a6@(-4) up ret. x value x square onlocal = return stack square(int the value square(x) * stack vars. d1 value d0 value)" in X Generated Code Generates the following assembly code on a 68000 machine (paraphrased) // The next two lines do the work Notice that we use 8 instructions to call 2 instruction. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 31

label mulsl movel // //... d1 d0 Store d0 "main" d1,a6@(-4) a6@(-4),d0 = d0,d0 d0,a6@(-4) = xresult (x = square(x) * inline square The inline keyword causes the function to be expanded inline eliminating any calling overhead: inline int square(int value) { return (value * value); Generated code: // Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 32

inline notes The keyword inline is a suggestion. If the function can not be generated inline, then C++ will generate an ordinary function call automatically. (At least that's what it supposed to do. Some older compilers have problems.) Use inline for very short functions. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 33

Parameter Type Summary Type Call by value Constant call by value Reference Declaration function(int var) Value is passed into the function, and can be changed inside the function, but the changes are not passed to the caller. function(const int var) Value is passed into the function and cannot be changed. function(int &var) Reference is passed to the function. Any changes made to the parameter are reflected in the caller. Constant Reference function(const int &var) Value cannot be changed in the function. This form of a parameter is more efficient then constant call by value for complex data types. array Call by address function(int array[]) Value is passed in and may be modified. C++ automatically turns arrays into reference parameters. function(int *var) Passes a pointer to an item. Pointers will be covered later. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 34

Structured Programming How to write a term paper using structured programming techniques: Start with an outline Replace each step in the outline with more detailed sentences. Replace each sentence with more detailed sentences. Repeat until you've got enough details Structured programming techniques. Write a simple version of your program leaving most of the work up to func tions that you haven't written yet. Fill in some of the details by writing the functions you left out of the first cut. Keep writing functions until there are no more to write. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 35

Bottom Up Programing Write an overall design of your program. Write small functions that perform the basic functions. Make sure they are debugged Write small functions that build on the basic functions. Continue building until the program is written. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 36

int if /* return (number else fact(int (number (1); */ == number) 0) * fact(number-1)); Recursion Recursion occurs when a function calls itself either directly or indirectly. A recursive function must follow two basic rules: 1. It must have an ending point. 2. It must simplify the problem. Factorial function: fact(0) = 1 fact(n) = n * fact(n-1) In C++ this is: { Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 37

int if /* return (first else sum(int (array[first]); */ == first, last) int+ last, sum(first+1, int array[]) last, { array)); Summing an array recursively Example: Sum(1 8 3 2) = 1 + Sum(8 3 2) = 8 + Sum(3 2) = 3 + Sum (2) = 2 3 + 2 = 5 8 + 5 = 13 1 + 13 = 14 Answer = 14 Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 38