Return by Reference CSCE 121 J. Michael Moore.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

CSE202: Lecture 12The Ohio State University1 Function Calling.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Week 8 Arrays Part 2 String & Pointer
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.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Introduction to C Programming CE
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Pointer Lesson 1 CS1313 Spring Pointer Lesson 1 Outline 1.Pointer Lesson 1 Outline 2.A Pointer Experiment 3.Point! 4.What is a Pointer? 5. NULL.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Lists in Python Lists as Arguments/Parameters. Lists as arguments to functions Just like other data types, lists can be sent to functions as arguments.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Introduction to Programming Using C
CS1010 Programming Methodology
Chapter 10: Void Functions
Anatomy of a class Part I
Dynamic Memory CSCE 121 J. Michael Moore.
CSCE 210 Data Structures and Algorithms
Operator Overloading CSCE 121 J. Michael Moore
Pointers and References
C Passing arrays to a Function
Anatomy of a Function Part 2
Object Oriented Programming COP3330 / CGS5409
Const in Classes CSCE 121 J. Michael Moore.
Heterogeneous aggregate datatypes
Pass by Reference, const, readonly, struct
Vectors.
Linked List Intro CSCE 121 J. Michael Moore.
Variables and Their scope
Array & Pointers CSCE 121 J. Michael Moore.
Dynamic Memory Copy Challenge
Pointers & Functions.
Anatomy of a Function Part 1
Anatomy of a Function Part 3
How Functions Work Part 1
7. 11 Introduction to C++ Standard Library Class Template vector (Cont
Static in Classes CSCE 121 J. Michael Moore.
Topics discussed in this section:
Pointers Lecture 2 Tue, Jan 24, 2006.
Simulating Reference Parameters in C
Essential Class Operations
Why did the programmer quit his job?
Destructor CSCE 121 J. Michael Moore.
Guidelines for Writing Functions
Chapter 9: Pointers and String
Pointers & Functions.
Move Semantics CSCE 121.
Structure (i.e. struct) An structure creates a user defined data type
Linked List Intro CSCE 121.
Pointers and References
CSCE 206 Lab Structured Programming in C
Dynamic Memory CSCE 121.
pointer-to-pointer (double pointer)
C Parameter Passing.
Presentation transcript:

Return by Reference CSCE 121 J. Michael Moore

Recall Reference Pass by Reference Address to memory location. Treat as if working with direct variable for the memory location. Pass by Reference Pass in the address. Modify calling variable inside function.

Return by Reference Return a reference to an object Modify returned variable after calling function is finished. We’ve already seen this in vector. myVec.at(2) = 7; at() returns a reference and assigns 7 to the memory location in the vector.

If we want to work with a local variable myVec.at(2) = 7; Suppose instead of this we decide we want to work with a variable. int z = myVec.at(2); z = 7; Copies value from the reference and assigns it to z. Setting z to 7, does not change the vector’s value at index 2.

Work with a local reference int& z = myVec.at(2); // or int &z z = 7; z refers to the same memory address as at(2). 7 is assigned to memory address at(2), i.e. the 3rd element in the vector

Writing a function to return by reference struct Chair { int height; } class Room { vector<Chair> chairs; public: void addChair(Chair chair); Chair getChair(int index); Chair Room::getChair(int index) { return chairs.at(index); Returns a copy

Writing a function to return by reference struct Chair { int height; } class Room { vector<Chair> chairs; public: void addChair(Chair chair); Chair& getChair(int index); Chair& Room::getChair(int index) { return chairs.at(index); Returns a reference Chair’s attributes can be modified.