Programming and Graphics

(Kiana) #1

4.5 Functions with scalar arguments 105


The reference declarator & has been appended to the variable type definition
“double” both in the functionmelonprototype and in the function implemen-
tation. Running the executable prints on the screen:


437;237

In contrast, if the reference declarators were omitted, the output would have
been:


437;231

The reference declarator must be used when a function returns one variable or
a group of scalar variables through the function arguments.


Maximum integer with given bits


For example, we consider a code that computes the maximum integer
that can be described with a specified number of bits,n. According to our
discussion in Section 1.2, this is equal to


20 +2^1 +... 2 n=2n+1− 1

It is convenient to split the code into two files, one file containing the
main program and the second file containing a functionipowthat computes
the integral power of an integer. The content of the main file namedbits.ccis:


/* --------------------------------------------------------------
Greatest integer that can be described a specified number of bits
----------------------------------------------------------------*/

#include <iostream>
#include <iomanip>
#include "ipow.h"
using namespace std;

int main()
{
int n, i;
const int two = 2;
cout << " Will compute the greatest integer" << endl;
cout << " that can be described with n bits" << endl;

while(n!=0)
{
cout << endl;
cout << " Please enter the maximum number of bits" << endl;
Free download pdf