Anatomy of a Function Part 3

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Procedures and Control Flow CS351 – Programming Paradigms.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
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.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Functions Pass by Value Pass by Reference IC 210.
J. Michael Moore Scope & Testing Modules CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
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.
Learners Support Publications Functions in C++
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Week 12 Methods for passing actual parameters to formal parameters.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Instructions for test_function
Function There are two types of Function User Defined Function
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Pointers and Pointer-Based Strings
Anatomy of a class Part II
Using local variable without initialization is an error.
Operator Overloading CSCE 121 J. Michael Moore
C Passing arrays to a Function
Variables with Memory Diagram
Anatomy of a Function Part 2
Const in Classes CSCE 121 J. Michael Moore.
Heterogeneous aggregate datatypes
Overloading the << operator
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 Templates CSCE 121 J. Michael Moore
Anatomy of a Function Part 1
How Functions Work Part 1
Pointers Call-by-Reference CSCI 230
Copy Constructor CSCE 121 J. Michael Moore.
Standard Template Library Find
Subroutines – parameter passing
Copy Assignment CSCE 121 J. Michael Moore.
Standard Template Library Model
Function Overloading CSCE 121 J. Michael Moore
STL: Traversing a Vector
Function “Inputs and Outputs”
Chapter 7: User-Defined Functions II
Essential Class Operations
Guidelines for Writing Functions
Pointers and Pointer-Based Strings
A simple function.
Anatomy of a Function Part 1
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Overloading the << operator
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
C Parameter Passing.
Presentation transcript:

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

Competing Challenges Pass by reference avoids copying large objects. Pass by value ensures there are no changes to the actual argument. What if we want both at the same time? Recall, when a variable is declared const, then its value cannot be changed. The solution is to use const when passing by reference. i.e. Pass by const reference

Pass by Const-Reference (How) In function definition, write the formal argument like: const type& var (OR const type &var) int f(const int& a) { int b; b = a+1; return b; }

Pros: Pass by Const-Reference Useful to pass large objects that should not be changed. Compiler will not allow changes to arguments passed by const-reference within the function. Actually, this applies to any parameter associated with a const. Copying is avoided. Both benefits at the same time. Benefit of pass by value. Benefit of pass by reference.

Cons: Pass by Const-Reference Compiler optimization makes the following meaningless. Accessing references is a bit slower than directly accessing the value. Reference address is read from memory and then the value is read from the reference address. I.e. two steps. Without reference, value can be accessed directly. For small objects compiled without optimization: The address will be copied. A small object’s value can be copied (pass by value) just as quickly as an address can be copied (pass by reference). For large objects: The savings in not copying far outweighs the slower access.