Functions.....cont(1)

Declaring Vs Defining a function

The syntax of a function declaration is identical to the header of a function definistion, except that:

    1. It does not include a statement for the function. Which means that it does not include the statement body with all the instructions that are usually enclosed within key brackets { }.

    2. It ends with a semicolon(;).

    3. In the argument enumeration, it is enough to write the type of each argument. The inclusion of a name for each argument is recommended, though optional as in the definition of a standard function.

Calling a function

In order for a function to be used, it must be called by another function. To call a function, one enters the name of the function followed by a matching pair of parentheses inside which is the list of parameter values to be passed to the function. The list is in the same order as in the declaration. The parameters included in function declarations are called formal parameters while the parameters listed in a function call are called the actual parameters.

For example, in order to invoke a function whose prototype is of the following format:

flaot volume(float, float,float);

the function call statement can be used:

volume(l,b,h);

where l,bh are float variables.

Arguments passed by value and by reference

In all the functions we have seen, the parameters passed to the functions have been passed by value. This means that when calling a function with parameters, what we have passed to the function were values but never the specified variables themselves.

For example, suppose that we called our first function addition using the following code :

int x=5, y=3, z;

z = addition ( x , y );

In this case was to call function addition passing the values of x and y, that means 5 and 3 respectively, not the variables themselves. The function addition is declared thus:

int addition(int a, int b);

In this way, when function addition is being called the value of its variables a and b become 5 and 3 respectively. But any modification of a or b within the function addition will not affect the values of x and y outside it. This is because variables x and y were not passed themselves to the the function, only their values were. But there might be some cases where you need to manipulate from inside a function the value of an external variable. For that purpose we have to use arguments passed by reference, as in the function duplicate of the following example:

// passing parameters by reference Output

#include <iostream.h> x=2, y=6, z=14

void duplicate (int& a, int& b, int& c)

{

a*=2;

b*=2;

c*=2;

}

int main ()

{

int x=1, y=3, z=7;

duplicate (x, y, z);

cout << "x=" << x << ", y=" << y << ", z=" << z;

return 0;

}

The first thing that should call your attention is that in the declaration of duplicate the type of each argument is followed by an ampersand sign (&), that serves to specify that the variable has to be passed by reference instead of by value.

When passing a variable by reference we are passing the variable itself and any modification,that we make to that parameter within the function will have effect in the passed variable outside it.

In the example above, we have associated a, b and c with the parameters used when calling the function

(x, y and z) and any change that we do on a within the function will affect the value of x outside.

Any change that we do on b will affect y, and the same with c and z.

That is why our program's output, that shows the values stored in x, y and z after the call to duplicate, shows the values of the three variables of main doubled.

When declaring the following function:

void duplicate (int& a, int& b, int& c)

we had declared it thus:

void duplicate (int a, int b, int c)

that is, without the ampersand (&) signs.

We would have not passed the variables by reference, but their values, and therefore, the output on screen

for our program would have been the values of x, y and z without having been modified.

Passing by reference is an effective way to allow a function to return more than one single value. For example, here is a function that returns the previous and next numbers of the first parameter passed.

// more than one returning value output #include <iostream.h> Previous=99, Next=101 void prevnext (int x, int& prev, int& next)

{ prev = x-1; next = x+1; } int main () { int x=100, y, z; prevnext (x, y, z); cout << "Previous=" << y << ", Next=" << z; return 0; } HOME LEARN C++ PREVIOUS NEXT