Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

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)
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Functions Prototypes, parameter passing, return values, activation frams.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
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.
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
Functions Modules in C++ are called functions and classes
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
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.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPS120: Introduction to Computer Science Functions.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
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.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Instructor - C. BoyleFall Semester
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
1 2/2/05CS250 Introduction to Computer Science II Pointers.
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.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
Programming Principles II Lecture Notes 3.1 Void Functions Andreas Savva.
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.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
User-Defined Functions (cont’d) - Reference Parameters.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
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.
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.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Chapter 10: Void Functions
Functions and an Introduction to Recursion
A Lecture for the c++ Course
Pointers Psst… over there.
Pointers Psst… over there.
Chapter 5 Function Basics
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.
CS150 Introduction to Computer Science 1
Pointers & Functions.
Anatomy of a Function Part 1
Pointers Call-by-Reference CSCI 230
CS150 Introduction to Computer Science 1
Function “Inputs and Outputs”
Functions and an Introduction to Recursion
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science
Introduction To Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers and dynamic objects
Pointers & Functions.
Anatomy of a Function Part 1
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
Presentation transcript:

Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Pass by Value §What happens when a function is invoked using value parameters? a copy of the actual parameters' values is made this copy of the values is placed in the formal parameters of the function the function has no access to the original actual parameter variables. It cannot change them as it only has a copy of their values.

#include using namespace std; void change(float, float); void main() { float num1= 2.5, num2= 3.5; cout<<num1<<“ “<<num2; change(num1, num2); cout<<num1<<“ “<<num2; } void change (float x, float y) { cout <<x<<" " << y << endl; x = 10.5; y = 7.5; cout <<x<<" " << y << endl; } Memory num1 num2 x y

Pass by Reference Passing parameters by reference gives the called function access to the actual memory locations of the passed variables. Use & (ampersand) & = “the address of …” Ex. int function1 (int &, float &, int)

Pass by Reference §Any changes made to the variable within the body of the called function are maintained when control returns to the calling function. §I.e. the values of the actual parameters will change!!!

Parameter List FunctionName (Datatype& VariableName, Datatype& VariableName,... )  When the & is omitted - the parameters are accepted as Pass By Value.  When the & is included - the parameters are accepted as Pass By Reference.

Function Prototype Pass by value: int function1 ( int, int, int); Pass by reference: int function2 (int &, int &, int &); Mixed function: int function3 ( int&, int, int&);

Function Call int a =1, b=2, c=3, result1, result2, result3; result1 = function1 (a, b, c); result2 = function2 (a, b, c); result3 = function3 (a, b, c);

Actual Parameters When the formal parameter is: Value  actual parameter may be:  variable i.e. function (a,b,c);  const i.e. function (1, 2, 5);  expression i.e. function (a+b, b+1, c*c); Reference  actual parameter must be:  variable only i.e. function (a, b, c);

#include using namespace std; void change(float &, float&); void main() { float num_1= 2.5, num_2= 3.5; cout<<num1<<“ “<<num2; change(num_1, num_2); cout<<num1<<“ “<<num2; } void change (float & x, float &y) { cout <<x<<" " << y << endl; x = 10.5; y = 7.5; cout <<x<<" " << y << endl; } Memory 2.5 num_1 num_2 x y

Pass by Reference §What happens when a function is invoked using reference parameters?  the location (memory address) of the actual parameter, not its value, is passed to the function  there is only one copy of the info and it is used by both the calling and the called function  the actual parameter and formal parameter become synonyms for the same location in memory  all changes made to the formal parameters in the called function have an effect on the actual parameters in the calling function.

#include using namespace std; int change (int&, int, int); void main() { int a = 2, b = 4, c = -1; cout << a << “ “ << b << “ “ << c << endl; c = change (b, a, c); cout << a << “ “ << b << “ “ << c << endl; } int change (int& x, int y, int z) { cout << x<< “ “ << y << “ “ << z << endl; x++; y--; z = x + y; cout << x<< “ “ << y << “ “ << z << endl; return z; } c =