LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 

Slides:



Advertisements
Similar presentations
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)
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Parameter Passing Mechanisms Reference Parameters.
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.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Chapter 6: Functions.
Programming Functions: Passing Parameters by Reference.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
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.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Parameter Passing Mechanisms Reference Parameters Read § §
CPS120: Introduction to Computer Science Functions.
1 CS161 Introduction to Computer Science Topic #10.
CPS120: Introduction to Computer Science Lecture 14 Functions.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
POINTERS.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
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.
C++ Programming Lecture 12 Functions – Part IV
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
1 Another Example: Complex Class #ifndef _Complex_H #define _Complex_H class Complex { float re, im; // by default private public: Complex(float x = 0,
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Lecture 10 Oct 7, 02. Pass by value and reference ► Calling a function and passing the value of a variable as an argument is called pass by value. The.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
CSC1201: Programming Language 2
Chapter 5 Function Basics
A Lecture for the c++ Course
School of EECS, Peking University
Pointers and Pointer-Based Strings
Student Book An Introduction
Passing Arguments to a Function
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Lecture 8 – 9 Arrays with in a class
Anatomy of a Function Part 2
Heterogeneous aggregate datatypes
CS1201: Programming Language 2
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Pointers & Functions.
Functions Pass By Value Pass by Reference
Pass by Reference.
Object Oriented Programming Using C++
Dynamic Memory A whole heap of fun….
Simulating Reference Parameters in C
Function “Inputs and Outputs”
CS1201: Programming Language 2
Pointers and References
Pointers and Pointer-Based Strings
CSC1201: Programming Language 2
Pointers & Functions.
The Stack.
CS1201: Programming Language 2
Intro to Programming Week # 8 Functions II Lecture # 13
Functions Chapter No. 5.
Presentation transcript:

LECTURE 3 PASS BY REFERENCE

METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference,  and pass by address. ( will be addressed later)

PASS BY VALUE When passing arguments by value, the only way to return a value back to the caller is via the function’s return value.  By default, arguments in C++ are passed by value.  When arguments are passed by value, a copy of the argument is passed to the function.  Because a copy of the argument is passed to the function, the original argument can not be modified by the function.

PASS BY VALUE When arguments are passed by value, the called function creates a new variable of the same type as the argument and copies the argument’s value into it. As we noted, the function cannot access the original variable in the calling program, only the copy it created. Passing arguments by value is useful when the function does not need to modify the original variable in the calling program. In fact, it offers insurance that the function cannot harm the original variable.

ADV. AND DISADV. OF PASSING BY VALUE ADVANTAGES  Arguments passed by value can be:  variables (eg. x),  literals (eg. 6),  or expressions (eg. x+1) or (eg foo()).  Arguments are never changed by the function being called, which prevents side effects. DISADVANTAGES Copying large structs or classes can take a lot of time to copy, and this can cause a performance penalty, especially if the function is called many times.

PASS BY REFERENCE Passing arguments by reference uses a different mechanism. Instead of a value being passed to the function, a reference to the original variable, in the calling program, is passed. An important advantage of passing by reference is that the function can access the actual variables in the calling program. This provides a mechanism for passing more than one value from the function back to the calling program.

In pass by reference, we declare the function parameters as references rather than normal variables: #include Using namespace std ; Void duplicate (int& a, int& b, int & c); Int main () { Int x=1,y=3,z=7; Duplicate (x,y,z); Cout << “x=“<<x<<“, y=“<<y<<“,z=“<<z; Return 0; } Void duplicate (int& a, int& b, int & c) {a*=2; b*=2; c*=2; } PASS BY REFERENCE

PASSING BY REFERENCE Reference arguments are indicated by the ampersand (&) following the data type: int& a When the function is called, a will become a reference to the argument. Since a reference to a variable is treated exactly the same as the variable itself, then any changes made to the reference are passed through to the argument!

PASSING ARGUMENTS BY REFERENCE Sometimes we need a function to return multiple values. However, functions can only have one return value. One way to return multiple values is using reference parameters. Advantages of Pass By Reference:  It allows us to have the function change the value of the argument, which is sometimes useful.  Because a copy of the argument is not made, it is fast, even when used with large structs or classes.  We can return multiple values from a function.

EXAMPLE 1 void foo(int &y) // y is now a reference { cout << "y = " << y << endl; y = 6; cout << "y = " << y << endl; } // y is destroyed here int main() { int x = 5; cout << "x = " << x << endl; foo(x); cout << "x = " << x << endl; return 0; }

EXAMPLE 2 void AddOne(int &y) { y++; } int main() { int x = 5; cout << "x = " << x << endl; AddOne(x); cout << "x = " << x << endl; return 0; }

EXAMPLE 3 void swap(float &x, float &y); int main() { float a, b; cout << "Enter 2 numbers: " << endl; cin >> a >> b; if(a>b) swap(a,b); cout << "Sorted numbers: "; cout << a << " " << b << endl; return 0; } void swap(float &x, float &y) { float temp; temp = x; x = y; y = temp; }