Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.

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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
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.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Chapter 6: Functions.
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.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
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.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
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.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
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 every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
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.
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.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
1 CS161 Introduction to Computer Science Topic #10.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Functions g prototypes g arguments g overloading g return values part I Re-read Section 1.2.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
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.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
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.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
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.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
Standard Version of Starting Out with C++, 4th Edition
Functions prototypes arguments overloading return values part I.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
6.12 Default Arguments.
Value returning Functions
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.
Counting Loops.
Lecture 18 Arrays and Pointer Arithmetic
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
CS150 Introduction to Computer Science 1
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Functions Pass by reference, or Call by reference Passing addresses Use of & part 3

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

void main(void) {double height = 4.0, base = 3.0; && double Pythagorus(double &, double &); 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 c 5.0

& & 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: height base c Very Risky!

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

Passing Data - by Reference { 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--; } >sum ? >product ? ab 6 csumproduct product

Passing Data - by Reference Output Enter three numbers: = 21 5 * 7 * 9 = 315 * x is 6, y is 8, z is 8 (only changed in function) (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 &);// 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 Passing by reference is dangerous – especially in LARGE programs!! Void print_val (int &q); This function doubles the value of any int variable sent to it (not just q)

Swap Routine void swap(float& num1, float& num2) { float temp; temp = num1; num1 = num2; num2 = temp; } Now, here is perhaps a valid use! What happens if we use call by value for the function? Not much!

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 no coercion because an address is passed, not a value *

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 *

Function Call Overhead Always uses the Call & Return sequence: When is it worth it? –when it simplifies the design –when overhead is negligible –when it abstracts a task –when it hides or protects data –when it consists of at least __ lines of code – Does it provide a net decrease in program size?

Drivers and Stubs Function Driver –when function is complete –A short “main” that calls the function and then tests its output –Written to test the validity of the prototype, the call, and the arguments Function Stub – when main is complete –A short function that just prints a message –Written to test the validity of the call, the function header, and the formal parameters