| Previous | Table of Contents | Next |
In C++, global variables are avoided because they can create very confusing code that is hard to maintain. You wont see global variables in any code in this book, nor in any programs I write.
There is virtually no limit to the number or types of statements that can be in a function body.
On the other hand, well-designed functions tend to be small. The vast majority of functions will be just a handful of lines of code.
Function arguments do not have to all be of the same type. It is perfectly reasonable to write a function that takes an integer, two longs, and a character as its arguments.
Any valid C++ expression can be a function argument, including constants, mathematical and logical expressions, and other functions that return a value.
Although it is legal to use a function that returns a value as a parameter to another function, it can create code that is hard to read and hard to debug.
As an example, each of the functions double(), triple(), square(), and cube() returns a value. You could write
Answer = (double(triple(square(cube(myValue)))));
This statement takes a variable, myValue, and passes it as an argument to the function cube(), whose return value is passed as an argument to the function square(), whose return value is in turn passed to triple(), and that return value is passed to double(). The return value of this doubled, tripled, squared, and cubed number is now passed to Answer.
It is difficult to be certain what this code does (was the value tripled before or after it was squared?), and if the answer is wrong it will be hard to figure out which function failed.
An alternative is to assign each step to its own intermediate variable:
unsigned long myValue = 2; unsigned long cubed = cube(myValue); // cubed = 8 unsigned long squared = square(cubed); // squared = 64 unsigned long tripled = triple(squared); // tripled = 192 unsigned long Answer = double(tripled); // Answer = 384
Now each intermediate result can be examined, and the order of execution is explicit.
The arguments passed in to the function are local to the function. Changes made to the arguments do not affect the values in the calling function. This is known as passing by value, which means a local copy of each argument is made in the function. These local copies are treated just as any other local variables. Listing 5.3 illustrates this point.
Listing 5.3 Demonstrates Passing by Value
1: // Listing 5.3 - demonstrates passing by value
2:
3: #include <iostream.h>
4:
5: void swap(int x, int y);
6:
7: int main()
8: {
9: int x = 5, y = 10;
10:
11: cout << Main. Before swap, x: << x << y: << y << \n;
12: swap(x,y);
13: cout << Main. After swap, x: << x << y: << y << \n;
14: return 0;
15: }
16:
17: void swap (int x, int y)
18: {
19: int temp;
20:
21: cout << Swap. Before swap, x: << x << y: << y << \n;
22:
23: temp = x;
24: x = y;
25: y = temp;
26:
27: cout << Swap. After swap, x: << x << y: << y << \n;
28:
29: }
![]() | Main. Before swap. x: 5 y: 10 Swap. Before swap. x: 5 y: 10 Swap. After swap. x: 10 y: 5 Main. After swap. x: 5 y: 10 |
![]() | This program initializes two variables in main() and then passes them to the swap() function, which appears to swap them. When they are examined again in main(), however, they are unchanged! |
The variables are initialized on line 9, and their values are displayed on line 11. swap() is called, and the variables are passed in.
Execution of the program switches to the swap() function, where on line 21 the values are printed again. They are in the same order as they were in main(), as expected. On lines 23 to 25 the values are swapped, and this action is confirmed by the printout on line 27. Indeed, while in the swap() function, the values are swapped.
Execution then returns to line 13, back in main(), where the values are no longer swapped.
As youve figured out, the values passed in to the swap() function are passed by value, meaning that copies of the values are made that are local to swap(). These local variables are swapped in lines 23 to 25, but the variables back in main() are unaffected.
Later in the book, youll see alternatives to passing by value that will allow the values in main() to be changed.
Functions return a value or return void. void is a signal to the compiler that no value will be returned.
To return a value from a function, write the keyword return followed by the value you want to return. The value might itself be an expression that returns a value. For example:
return 5; return (x > 5); return (MyFunction());
These are all legal return statements, assuming that the function MyFunction() itself returns a value. The value in the second statement return (x > 5), will be zero if x is not greater than 5, or it will be 1. What is returned is the value of the expression, false or true, not the value of x.
When the return keyword is encountered, the expression following return is returned as the value of the function. Program execution returns immediately to the calling function, and any statements following the return are not executed.
It is legal to have more than one return statement in a single function. However, keep in mind that as soon as a return statement is executed, the function ends. Listing 5.4 illustrates this idea.
| Previous | Table of Contents | Next |