Anatomy of a Function Part 2

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 5: Functions 1 Based on slides created by Bjarne Stroustrup.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Functions Pass by Value Pass by Reference IC 210.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 17: Vectors and Arrays 1 Based on slides created by Bjarne Stroustrup.
Chapter 8: Advanced Method Concepts. Understanding Parameter Types Mandatory parameter – An argument for it is required in every method call Four types.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
User-Defined Functions II TK1914: C++ Programming.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
User-Defined Functions (cont’d) - Reference Parameters.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Introduction to Programming
Chapter 10: Void Functions
A Lecture for the c++ Course
Learning Objectives Pointers Pointer in function call
Dynamic Memory CSCE 121 J. Michael Moore.
Pointers Psst… over there.
Pointers Psst… over there.
Variables with Memory Diagram
Exceptions with Functions
IO Overview CSCE 121 J. Michael Moore
Manipulators CSCE 121 J. Michael Moore
Array & Pointers CSCE 121 J. Michael Moore.
Dynamic Memory Copy Challenge
Return by Reference CSCE 121 J. Michael Moore.
Functions Overview CSCE 121 J. Michael Moore
Anatomy of a Function Part 1
Anatomy of a Function Part 3
How Functions Work Part 1
Exceptions CSCE 121 J. Michael Moore
Subroutines – parameter passing
Functions Pass By Value Pass by Reference
Standard Template Library Model
Static in Classes CSCE 121 J. Michael Moore.
How Functions Work Part 2
Function Overloading CSCE 121 J. Michael Moore
Simulating Reference Parameters in C
Function “Inputs and Outputs”
Chapter 7: User-Defined Functions II
Guidelines for Writing Functions
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Creating and Using Pointer Variables in C++ By: Ed Brunjes
Dynamic Memory Copy Challenge
The Stack.
Anatomy of a Function Part 1
IO Overview CSCE 121 Strongly influenced by slides created by Bjarne Stroustrup and Jennifer Welch.
Presentation transcript:

Anatomy of a Function Part 2 CSCE 121 J. Michael Moore Strongly influenced by slides created by Bjarne Stroustrup and Jennifer Welch

Recall References Alternative name for an object Can have multiple names referring to the same object (location in memory) Once a reference is initialized, it cannot be changed to refer to a different object (i.e. location) int i = 7; int& r = i; // r is a reference to an int r = 9; // i becomes 9 cout << i << endl; // outputs 9

Pass by Reference (How) In function declaration Use “&” after the type to indicate that the formal argument should be passed by reference int f(int& a) { a = a+1; return a; } OR int f(int &a) { a = a+1; return a; } To call, same as call by value (i.e. what we did before)

Pass by Reference (Consequence) Reference (stored as an address) is copied Formal Argument is now a reference to the actual argument. i.e. the variable corresponding to the formal argument in the function point to the same object (memory location) of the actual argument. Actual argument can be changed in the function via the reference. i.e. Changing the variable (passed by reference) in the function changes the variable used as an actual argument in the function call.

Pros: Pass by Reference Recall that functions can only return a single value… So, with pass by reference, multiple variables can be “returned” from a function.

Cons: Pass by Reference You really do not want to change the value of the actual argument. If passed by reference, the function might inadvertently modify the actual argument. This is a side effect. (Side effects tend to be bad things that we want to avoid!) Regardless, this can result in obscure bugs that are difficult to track down.