• Aucun résultat trouvé

Passing Arguments by Reference

Dans le document Click to visit (Page 173-177)

void printMessage (string theName, int theAge) {

cout << "Your name is " << theName

<< " and your age is " << theAge << endl;

}

Here is some sample input and output (fortunately the program has no way to verify my age):

Enter first name: Jeff Kent Enter age: 21

Your name is Jeff Kent and your age is 21 The function call and the function header here are printMessage(name, age);

void printMessage (string theName, int theName)

The first argument of the printMessage function expects a string, so it is critical that the first argument in the function call is a string. Similarly, the second argument of the printMessage function expects an integer, so it is critical that the second argument in the function call is an integer. If the arguments in the function call were reversed, as in:

printMessage(age, name);

then the consequence would not be illogical output as in the prior example, but instead a compiler error “cannot convert parameter 1 from ‘int’ to ‘string’.” This is because the compiler was expecting from the function prototype that the first argument (or parameter) would be a string, not an int.

Passing Arguments by Reference

Passing arguments by value is fine when you don’t want to change their value in the called function. The printMessage function did not change the value of its arguments; it simply outputted them.

However, sometimes the intent of a function is to change the value of the argument passed to it. Consider the following example, in which the doubleIt function is supposed to double the value of the argument passed to it:

#include <iostream>

Sending Information to a Function

cin >> num;

doubleIt(num);

cout << "The number doubled in main is " << num << endl;

return 0;

cout << "The number doubled in doubleIt is "

<< x << endl;

}

Here is some sample input and output:

Enter number: 3

The number to be doubled is 3

The number doubled in doubleIt is 6 The number doubled in main is 3

As the sample input and output reflects, the value of num was not changed by the doubling of its counterpart argument in the doubleIt function.

The reason the value of num was not changed in main is that a copy of it was passed to doubleIt. The change was made to the copy, but the original, the variable num in main, was not affected by the doubling of the copy. The logic is the same as if I gave you a copy of this page, which you then proceeded to rip up. The original I kept would be unaffected.

In order for the called function to change the value in main of a variable passed to it, the variable must be passed by reference. The variable in the called function is called a reference variable. The reference variable is not a copy of the variable in main. Instead, the reference variable is an alias for the variable in main. You may recall from television that an alias is another name a person may use, such as James Bond’s alias of 007.

However, whether you refer to him as James Bond or 007, you are still referring to the same person.

In order to pass a variable by reference, the data type in the argument, both in the function header and in the prototype, is followed by an ampersand. Yes, this is the same ampersand that is used as the address operator. Here, however, the ampersand is used in a different context.

The following program passes the variable to be doubled by reference:

#include <iostream>

cout << "The number doubled in main is " << num << endl;

return 0;

}

void doubleIt (int& x) {

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0060.html (6 of 8)06.11.2004 22:52:05

Sending Information to a Function

cout << "The number to be doubled is " << x << endl;

x *= 2;

cout << "The number doubled in doubleIt is " << x << endl;

}

Here is some sample input and output:

Enter number: 3

The number to be doubled is 3

The number doubled in doubleIt is 6 The number doubled in main is 6

There were only two changes. The prototype and function header for doubleIt when the argument is passed by value is

void doubleIt(int);

void doubleIt (int x)

By contrast, the prototype and function header for doubleIt when the argument is passed by reference each includes the ampersand following the data types:

void doubleIt(int&);

void doubleIt (int& x)

However, the function call is the same whether the variable is passed by value or by reference; there is no ampersand in either case. Whether the program passes an

argument in a function call by value or by reference is dictated by the function’s prototype.

You can pass multiple values by reference as well as by value. Indeed, you can pass some values by reference and others by value. You pass by reference those values you need to change, and you pass by value those values you are not changing.

Note There is another difference between passing by value and passing by reference.

You can pass by value expressions and constants (constants are covered in Chapter 10) as well as variables. However, you can only pass variables by reference.

For example, in the following program the function addNumbers has three arguments.

The first two arguments are the numbers to be added, and are passed by value. The third argument will be the sum of the two numbers and will be passed by reference, since its value is being changed in the called function:

#include <iostream>

using namespace std;

void addNumbers(int, int, int&);

int main () {

int firstNum, secondNum, sum = 0;

cout << "Enter first number: ";

cin >> firstNum;

cout << "Enter second number: ";

cin >> secondNum;

addNumbers (firstNum, secondNum, sum);

cout << firstNum << " + " << secondNum << " = " << sum;

return 0;

}

void addNumbers (int x, int y, int& z)

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0060.html (7 of 8)06.11.2004 22:52:05

Sending Information to a Function

{

z = x + y;

}

Here is some sample input and output:

Enter first number: 3 Enter first number: 6 3 + 6 = 9

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0060.html (8 of 8)06.11.2004 22:52:05

Dans le document Click to visit (Page 173-177)