Functions Pass By Value Pass by Reference

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
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.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
CPS120: Introduction to Computer Science Functions.
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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
1 Workin’ with Pointas An exercise in destroying your computer.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Hello World Using C++ (L03) * Introduction C++ * Programming Style * Hello World Using C++ Hollow World Using C/C++ Dr. Ming Zhang.
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.
C++ Programming Lecture 12 Functions – Part IV
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
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 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Popping Items Off a Stack Using a Function Lesson xx
User-Written Functions
Topic Pre-processor cout To output a message.
Chapter 9: Pointers.
Two-Dimensional Arrays Lesson xx
Chapter 10: Void Functions
A Lecture for the c++ Course
FUNCTIONS IN C++.
Pointers and Pointer-Based Strings
Student Book An Introduction
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
void Pointers Lesson xx
Arrays & Functions Lesson xx
Structures Lesson xx In this module, we’ll introduce you to structures.
Object Oriented Programming COP3330 / CGS5409
Returning Structures Lesson xx
Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx  In this presentation, we will illustrate the difference between a pointer.
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
One-Dimensional Array Introduction Lesson xx
File I/O with Records Lesson xx
Passing Structures Lesson xx
Chapter 9: Pointers.
Popping Items Off a Stack Lesson xx
Value returning Functions
Arrays Kingdom of Saudi Arabia
6 Chapter Functions.
Pointer to Structures Lesson xx
Chapter 4 Implementing Free Functions
Function “Inputs and Outputs”
Chapter 7: User-Defined Functions II
Pointers and Pointer-Based Strings
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Arrays Arrays A few types Structures of related data items
Creating and Using Pointer Variables in C++ By: Ed Brunjes
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
CISC181 Introduction to Computer Science Dr
4.1 Introduction Arrays A few types Structures of related data items
C Parameter Passing.
Presentation transcript:

Functions Pass By Value Pass by Reference Lesson xx In this module, we’ll talk about the different ways arguments can be passed into functions.

Objectives Pass by value Pass by reference We’ll look at the following ways to send arguments into a function: 1) pass by value 2) pass by reference.

Pass by Value #include <iostream> using std::cout; using std::endl; int main() { void fun(int x, double y); int a = 5; double b = 9.3; cout << "before "; cout << a << ' ' << b << endl; fun(a, b); cout << “after "; return 0; } void fun(int x, double y) { x = 99; y = 7.23; } Let’s look at this program which is an example of pass by value. The first few lines of main( ) are the declarations and initialization. We use the cout statements to print out the contents of a and b before the call to function fun(). The next slide shows you the output right before the call to function fun().

Output Before Function Call You can see that the computer prints the word “before” and then the contents of a and b which is 5 and 9.3

Function Call #include <iostream> using std::cout; using std::endl; int main() { void fun(int x, double y); int a = 5; double b = 9.3; cout << "before "; cout << a << ' ' << b << endl; fun(a, b); cout << “after "; return 0; } void fun(int x, double y) { x = 99; y = 7.23; } The line: fun (a,b); calls function fun() and sends in a and b as arguments. A copy of what is in a is passed into the local variable x and a copy of b is stored in the local variable y.

Function fun ( ) fun ( a, b); void fun(int x, double y) { x = 99; } 5 x 9.3 y This is a picture of the computer’s memory after the line: fun (a,b); from main() is executed. The local variable x contains 5 and the local variable y contains 9.3 because that is what is in a and b.

Function fun ( ) void fun(int x, double y) { x = 99; y = 7.23; } 99 x After we execute the body of function fun, this is the picture of the computer’s memory. x contains the # 99 and y has 7.23.

Local Variables Destroyed void fun(int x, double y) { x = 99; y = 7.23; } 99 x 7.23 y When we finish with the body of the function, local variables are destroyed and we return back to main.

Function Call #include <iostream> using std::cout; using std::endl; int main() { void fun(int x, double y); int a = 5; double b = 9.3; cout << "before "; cout << a << ' ' << b << endl; fun(a, b); cout << “after "; return 0; } void fun(int x, double y) { x = 99; y = 7.23; } When we get back to main(), the last two cout statements will be executed and you will get the output shown in the next slide.

Output After Call to Function You can see from the output that when we return from the function fun ( ), the contents of a and b are still the same, 5 and 9.3. This program is an example of pass by value. What that means is that no matter what you do to x and y in function fun( ), you will not modify a and b in main. The reason is that x and y are local variables which are destroyed when you exit the function. Sometimes, however, you might want a and b to change when you modify x and y in the function. You do this with a technique called pass by reference.

Pass by Reference #include <iostream> using std::cout; using std::endl; int main() { void fun(int & x, double & y); int a = 5; double b = 9.3; cout << "before "; cout << a << ' ' << b << endl; fun(a, b); cout << “after "; return 0; } void fun(int & x, double &y) { x = 99; y = 7.23; } We have changed the last program so that the arguments are passed by reference rather than pass by value. When you write the program this way, changing x and y in function fun( ) changes a and b in function main().

Output Take a look at the output from the pass by reference program. Before we call the function, a is 5 and b is 9.3. After the call to the function, a is 99 and b is 7.23. This program shows you that you can modify the calling arguments in a function if you pass by reference rather than pass by value.

Syntax of Pass by Reference void fun(int & x, double &y) { x = 99; y = 7.23; } When you want to pass arguments by reference, you put an & between the data type and the argument as in: int & x; C++ is format free which means that it doesn’t matter if you have spaces between the &. The declaration looks like the header except that you have a ; at the end of the statement. Your argument list can be a combination of pass by value and pass by reference.

Mechanics of Pass by Reference int a = 5; double b = 9.3; cout << "before "; cout << a << ' ' << b << endl; fun(a, b); cout << “after "; void fun(int & x, double &y) { x = 99; y = 7.23; } 5 Until we discuss pointers we cannot tell you the exact mechanics of passing by reference. Loosely, here is what happens: before function fun( ) is called, a contains 5 and b contains 9.3. a 9.3 b

Alias int a = 5; double b = 9.3; cout << "before "; cout << a << ' ' << b << endl; fun(a, b); cout << “after "; void fun(int & x, double &y) { x = 99; y = 7.23; } 5 When you enter function fun(), you get this picture. The computer creates an alias for the arguments. In other words, x is another name for a and y is another name for b. In passing by reference the computer does not create local variables like it does in pass by value. a (x) 9.3 b (y)

Execution of Function Body int a = 5; double b = 9.3; cout << "before "; cout << a << ' ' << b << endl; fun(a, b); cout << “after "; void fun(int & x, double &y) { x = 99; y = 7.23; } 99 Now, when the computer executes the line, x = 99; it says, “oh yes, x is another name for a, so I am going to put 99 into a”. After the 2 lines of function fun () are executed, the memory of the computer will look as drawn. a (x) 7.23 b (y)

After Calling Function int a = 5; double b = 9.3; cout << "before "; cout << a << ' ' << b << endl; fun(a, b); cout << “after "; void fun(int & x, double &y) { x = 99; y = 7.23; } 99 When you get back to main( ), the alias are gone because they only exists for the duration of the function. Now, a is 99 and b is 7.23. There you have it, pass by reference. There are several reasons for using pass by reference: 1) it allows you to modify the calling parameters 2) since the return type only allows you to send back 1 item, pass by reference allows you to return more than 1 item indirectly. a 7.23 b

Synopsis fun(a, b); void fun(int x, double y) // pass by value fun(a, b); void fun(int & x, double &y) // pass by reference Let’s put this all together for you so that you can compare both methods of passing arguments. The top line is an example of pass by value. Anything you do to x and y in the function will not affect a and b. The bottom line is how you pass by reference and you indicate this by using an & between the data type and the argument. When you pass by reference, changing x and y will change a and b respectively. Notice that the call to the function is the same whether you pass by value or reference. The computer tells the difference by looking for the &.

Summary Pass by value Pass by reference In this module, we learned about passing arguments by value and passing arguments by reference.