Reference Parameters.

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important.
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.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Topic 8 – Introduction to Pointers and Function Output Parameters.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Parameter Passing Mechanisms Reference Parameters Read § §
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Parameter Passing Mechanisms Reference Parameters § §
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Objects with Functions and Arrays. Objects can be Passed Class defines type – Can use as type of function or parameter.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions, Scope & File IO C++ Lecture 4 Bhaskar Bhattacharya.
Chapter 6: User-Defined Functions I
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Functions, locals, parameters, and separate compilation
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Lecture 6 C++ Programming
Pointers Psst… over there.
#include "std_lib_facilities
Using local variable without initialization is an error.
Pointers Psst… over there.
Functions Inputs Output
Multiple Files Revisited
Const in Classes CSCE 121 J. Michael Moore.
Heterogeneous aggregate datatypes
Pass by Reference, const, readonly, struct
6.12 Default Arguments.
Chapter 6 Methods: A Deeper Look
Arrays And Functions.
Classes & Objects: Examples
6 Chapter Functions.
Classes, Encapsulation, Methods and Constructors (Continued)
Pointers & Functions.
Pointers Call-by-Reference CSCI 230
Constructors and Other Tools
Objects with Functions and Arrays
Functions Pass By Value Pass by Reference
Functions with arrays.
Introduction to Programming
C-to-LC3 Compiler Over the course of the next two weeks, you will build a program that will compile C code to LC-3 assembly language Don't panic! You.
CS150 Introduction to Computer Science 1
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Arrays Arrays A few types Structures of related data items
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers & Functions.
Lecture 2 Arrays & Pointers September 7, 2004
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
CISC181 Introduction to Computer Science Dr
Standard Version of Starting Out with C++, 4th Edition
Templates Generic Programming.
4.1 Introduction Arrays A few types Structures of related data items
Functions Chapter No. 5.
Presentation transcript:

Reference Parameters

References Reference variable Alias / shortcut to another variable Declared with Type& Name

References References do not have their own value

References Refer to Variables A reference MUST refer to another variable

Reference Parameters Reference parameter indicates pass by reference: Standard is pass by value:

Reference Parameter x is a link back to num

Reference Parameter Uses Another way to get information back from a function:

Reference Parameter Uses Used in some library functions cin.get(char &c) get one char, possibly whitespace

Reference Parameter Uses Way to get multiple pieces of information back from function:

Reference Parameter Efficiency Value parameters require copying parameter What if parameter is huge? Reference parameter does not copy… links Minimal overhead Danger of modification!

Const References A const & can not be modified: Any attempt is compile error:

Function IO Ways to give functions info Global variables I set this random variable for you to read… Parameters Here is the list of things you asked for

Function IO Ways to give functions info Global variables I set this random variable for you to read… Parameters Here is the list of things you asked for

Returning Information Can get information from function with: Global variables I think you set this random variable… Reference Parameters I passed you these things by reference, you may have changed them for me… Return value You always give me back an int…

Returning Information Can get information from function with: Global variables I think you set this random variable… Reference Parameters I passed you these things by reference, you may have changed them for me… Return value You always give me back an int…

When To Reference Function produces 1 output return it Function needs to produce 2+ outputs extra reference parameters for outputs return void

When To Reference Function must modify inputs Are you sure??? If so use refs: Function needs to work with large data item: const refs