Download presentation
Presentation is loading. Please wait.
1
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.
2
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.
3
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().
4
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
5
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.
6
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.
7
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.
8
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.
9
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.
10
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 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.
11
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().
12
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 This program shows you that you can modify the calling arguments in a function if you pass by reference rather than pass by value.
13
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.
14
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
15
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)
16
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)
17
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 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
18
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 &.
19
Summary Pass by value Pass by reference
In this module, we learned about passing arguments by value and passing arguments by reference.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.