Function Arguments

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. For example, if you have a function declared as

int MyFunction(int theIntegerParam, bool theBoolean);

you can legally invoke this function with any of the following function calls:

int z, x = 3, y = 5; // declare the variables
z = MyFunction(x,y); // pass in an int and a bool variable
z = MyFunction(32,true); // pass in two constants
z = MyFunction(23+9, 100>5); // expressions which equate to 32, true

The last function call is identical in its effect to the second function call. Finally, if you declare the following two functions

int MyIntFunction(int x, int y);
bool MyBoolFunction(int x, int y);

you can invoke MyFunction as follows:

z = MyFunction(MyIntFunction(3,5), MyBoolFunction(2,4));

In this final invocation, you are effectively passing in as parameters the integer value returned by calling MyIntFunction(3,5) and the bool value returned by calling MyBoolFunction(2,4).

Using Functions as Parameters to Functions

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 doubler(), tripler(), square(), and cube()returns a value. You could write

Answer = (doubler(tripler(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 tripler(), and that return value is passed to doubler(). 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 = tripler(squared);       // tripled = 192
unsigned long Answer =  doubler(tripled);       // Answer = 384

Now each intermediate result can be examined, and the order of execution is explicit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.217.55