Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.

Slides:



Advertisements
Similar presentations
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
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.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Starting Out with C++, 3 rd Edition 1 Chapter 9 – Pointers.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Functions Pass by Value Pass by Reference IC 210.
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.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Programming Functions: Passing Parameters by Reference.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Functions g g Data Flow g Scope local global part 4 part 4.
Functions Structured Programming 256 Chapter 6 Functions g prototypes g arguments g overloading g return values part I.
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
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.
1 Functions Chapter 7 2 Hope you can function! What is R2D2 doing here? What is his function? Who is Nibble? Can he function? IS he a function? Who is.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 Functions. 2 Chapter 7 Topics  Writing a Program Using Functional Decomposition  Writing a Void Function for a Task  Using Function Arguments and.
CPS120: Introduction to Computer Science Functions.
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
1 CS161 Introduction to Computer Science Topic #10.
CPS120: Introduction to Computer Science Lecture 14 Functions.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Functions g prototypes g arguments g overloading g return values part I Re-read Section 1.2.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
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.
User-Defined Functions (cont’d) - Reference Parameters.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
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.
Standard Version of Starting Out with C++, 4th Edition
Functions prototypes arguments overloading return values part I.
Pointer.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
User Defined Functions
CS150 Introduction to Computer Science 1
Counting Loops.
Pointers & Functions.
Functions, Part 2 of 3 Topics Functions That Return a Value
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Dynamic Memory.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers & Functions.
Standard Version of Starting Out with C++, 4th Edition
CS150 Introduction to Computer Science 1
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function prototype function call function definition Address operator

&& double Pythagorus(double &, double &); void main(void) {double height = 4.0, base = 3.0; cout << “Hypotenuse = “ << Pythagorus(height, base) << endl;... } & & double Pythagorus(double& a, double& b) {double c; c = sqrt(a*a + b*b); return c; } Passing Data by-reference Example * address of height address of base 5.0 c

& & double Pythagorus(double& a, double& b) {double c; a++; b++; c = sqrt(a*a + b*b); return c; } Passing Data - by Reference * * * * * address of height address of base back in main:cout << height; cout << base: c height base

Passing Data - by Reference In main() values referenced as 1 value stored a height 1 value stored b base In Pythagorus() values referenced as *

By-Reference, Another Example { float a, b, c, sum, product; void calc(float, float, float, float &, float &); // prototype cout << "Enter three numbers: "; cin >> a >> b >> c; calc(a, b, c, sum, product); // call cout << a<<“ + “<<b<<“ + “c<<“ = " << sum; cout << ‘\n’<<a<<“ * “<<b<<“ * “c<<“ = " << product; } void calc(float x, float y, float z, float &tot, float& multiply) {tot = x + y + z; // definition multiply = x * y * z; x++; y++; z--;// for demo purposes }

Another Example: What happens? calc(a, b, c, sum, product); void calc(float x, float y, float z,float &tot, float& multiply) {tot = x + y + z; multiply = x * y * z; x++; y++; z--; } 5 79 ?? a b c sumproduct  sum  product

Another Example: Program Output Output Enter three numbers: = 21 5 * 7 * 9 = 315 * x is 6, y is 8, z is 8 tot and sum refer to the same address product and multiply refer to the same address

Passing Data - by Reference void main(void) { int w = 3; void print_val(int &);// local function prototype cout <<"w before the function call is "<<w<<‘\n’; print_val(w); cout <<"w after the function call is "<<w<<‘\n’; } void print_val(int& q) {cout<<"Value passed to the function is "<<q<<endl; q = q *2;// doubles the value of w? cout<<"Value at the end of the function is "<< q <<endl; }

Passing Data - by Reference Output w before the function call 3 Value passed to the function is 3 Value at the end of the function is 6 w after the function call is 6 void print_val (int &q); This function doubles the value of any (int) variable sent to it (not just w).

Swap Routine void swap(float& num1, float& num2) { float temp; temp = num1; num1 = num2; num2 = temp; } What happens if we use call by- value for the swap function?

Data Type Mismatch value parameters implicit type conversion - value of the actual parameter is coerced to the data type of the formal parameter reference parameters coercion not possible because an address is passed, not a value - types must match *

A Comparison formalactual parameter isparameter may be valuevariable, constant, or expression type coercion may take place referencevariable only of exact same type as formal

What’s Happening???? call sequence 1. memory is allocated 2. parameters are passed 3. transfer of control return sequence 1. value of the return is stored 2. memory is deallocated 3. transfer of control *