Functions

Additional Examples


There are three additional worked examples:-
Example 1
Example 2 - using reference variables
Example 3 - Functions and arrays


Example 1

PROBLEM:

Write a function that will display a line of n asterisks on the screen. n will be passed as a argument to the function. After writing the line of n asterisks a new line should be printed.

Write a program to call this function, using it to display a grid of m times n asterisks. The user should be prompted to enters the values for m and n.


SOLUTION:

Considering initailly the function, the first thing to consider is

What arguments are to be sent to the function?

Obviously, it need to be told how many asterisks to write, in this case n which will be integer?

The next thing to consider is what will be returned by the function?

Obviously, the function only prints to screen and therefore does not need to return anything to the main calling program. The return type is therefore void

We can now continue and write the function.


void line (int n)
{
        int i;

        for (i=0; i < n;i++)
                cout << "*";
        cout << "\n";
}

The complete program, has to call this function m times, and so a loop will be used to call the program. The complete solution is then.

#include <iostream.h>

// function declaration */
void line( int n);

main()
{
        int     i;
        int     m,n;

        cout << "Enter  values for m and n \n";
	cin >> m >> n ;

	// Loop to call function m times 
        for (i=0; i < m; i++)
                line(n);
}


// function definition 
void line (int n)
{
        int i;

        for (i=0; i < n;i++)
                cout << "*";
        cout << "\n";
}


ADDITIONAL EXERCISE

Amend the main program written above, so that the following type of pattern is displayed. This is shown for the case of m = 5.

*
**
***
****
***** 


SOLUTION

Note that no changes are required to the function, we simply need to amend the main calling program.


#include <iostream.h>

// function declaration 
void line( int n);

main()
{
        int     i;
        int     m;

        cout << "Enter  a value for the number of rows\n";
	cin  >> m ;

	//Call to function in loop 
        for (i=1; i <= m; i++)
                line(i);
}


Example 2

PROBLEM:

Consider the calculation of the roots of the quadratic equation ax²+bx+c=0, assuming that a is non-zero:-

Write a function in C++ which will take a, b and c as arguments and returns the roots as function arguments root1 and root2 (as appropriate). The number of roots will be returned through the type specifier.

Write a main program which prompts the user to enter the values of a, b and c. Then after using a call to the function written above, the program will display the number of roots and their values as appropriate.


SOLUTION:

The function has to be sent the arguments a, b and c, it also requires two arguments for the roots. These values will be altered by the program so must be refernec variables. The function returns an integer number for the number of roots. The function declaration will therefore be:-


int root (float a, float b, flot c, float &root1, float &root2);

The rest of the function can now be written.



int root (float a, float b, float c, float &root1, float &root2)
{
        float   tmp;

        tmp = b*b - 4*a*c;

        if (tmp > 0)
        {
                root1 = (-b + sqrt(tmp))/(2*a);
                root2 = (-b - sqrt(tmp))/(2*a);
                return 2;
        }
        else if (tmp == 0)
        {
                root1 = -b/(2*a);
                return 1;
        }
        else
                return 0;
}
The main program can now be written. Remember to include the function declaration and math.h as the sqrt function was used.

#include <iostream.h>
#include <math.h>

int root (float a, float b, float c, float &root1, float &root2);

main()
{
        float   a,b,c,root1,root2;
        int     num_roots;

        cout << "Enter values for a, b and c\n";

        cin >> a >> b >> c;

        num_roots = root(a,b,c,root1, root2);

        if (num_roots == 2)
                cout << "There are 2 real roots with values  " << root1 
                     << " and " << root2 << " \n";
        else if (num_roots == 1)
                 cout << "There is 1  real root with value " << root1 <<"
\n";
        else
                cout << "There are no real roots\n";
}


Example 3

Arrays can also be sent to functions. Arrays are also special in that the function can change the contents of the array and the main calling program will know about the changes after the function is called.

PROBLEM: Write a function which when sent an array will fill each element of the array with an integer number which is equal to the element number. The function will also be sent the size of the array.

Write a main program to call this function, and then print out the contents of the array.


SOLUTION:

The function will not return anything - hnece the type specifier is void. The function will be sent an array of integers and an integer for the size. In the function prototype, teh size of the array is not enetered in the square brackets for the array - this is left blank. The empty square brackets simply indictate that an array will be sent to the function.


void fill_array(int array[], int size)
{
	int i;
	
	for (i=0; i < size; i++)
		array[i] = i;

	return;
}

The main program will then take the form:-


#include <iostream.h>

// function declaration
void fill_array(int array[], int size);

main()
{
	int size = 100;
	int array[size];

	// call function
	fill_array(array, size);

	// write out contents of array
	for (i=0; i < size ; i++)
		cout << i << "\t" << array[i] << endl;
}

Note: That to call the function, only the name of the array to sent to the function needs to be given in the function call. (In the notes on points it mentions how the name of an array is a synonym for a pointer to the beginning of the array - hence only the mname needs to be given.


Back to Main Page for Lecture 6