Programming and Graphics

(Kiana) #1

4.6 Functions with array arguments 109


return a;
}

Now consider the main program:


int main()
{
int a=5, b=10;
int&c=maxalias(a, b);
c = 20;
cout << a <<""<<b<<endl;
return 0;
}

The output is:


520

We can circumvent introducing the aliascby replacing the second and third
lines with the single line:


maxal(a, b) = 20;

We see that returning an alias allows us to bypass temporary variables
and thus save memory space.


Problems


4.5.1.Write a function that returns the product of two real numbers through
an argument, and the ratio through the function return.


4.5.2.Write a function that returns the sum and the difference of two real
numbers through its arguments.


4.5.3.Explain why the results of thebitsprogram discussed in the text are
consistent with the data given in Table 2.3.1.


4.6 Functions with array arguments...................


Unlike scalar variables, array variables communicated to a function are referred
to the memory address allocated in the calling program. Thus, array variables
are called byreferenceor byaddress. By default, user-defined functions are
able to change the values of the elements of a communicated array.

Free download pdf