C Programming

Practise Exercise 8: Functions

1. The function called menu which prints the text string "Menu choices", and does not pass any data back, and does not accept any data as parameters, looks like

function 1

	void menu( void ) {
		printf("Menu choices");
	}
function 2

	int menu( void ) {
		printf("Menu choices");
	}
function 3

	int menu( char string[] ) {
		printf("%s", string);
	}

2. A function prototype for the above function looks like

int menu( char [] );
void menu( char [] );
void menu( void );
int menu( void );

3. A function called print which prints a text string passed to it as a parameter (ie, a character based array), looks like

function 1

	int print( char string[] ) {
		printf("%s", string);
	}
function 2

	void print( char string[] ) {
		printf("Menu choices");
	}
function 3

	void print( char string[] ) {
		printf("%s", string);
	}

4. A function prototype for the above function print looks like

int print( char [] );
void print( char [] );
void print( void );
int print( void );

5. A function called total, totals the sum of an integer array passed to it (as the first parameter) and returns the total of all the elements as an integer. Let the second parameter to the function be an integer which contains the number of elements of the array.

function 1

	int total( int numbers[], int elements ) {
		int total = 0, loop;
		for( loop = 0; loop < elements; loop++ )
			total = total + numbers[loop];
		return total;
	}
function 2

	int total( int numbers[], int elements ) {
		int total = 0, loop;
		for( loop = 0; loop <= elements; loop++ )
			total = total + numbers[loop];
		return total;
	}
function 3

	int total( int numbers[], int elements ) {
		int total, loop;
		for( loop = 0; loop > elements; loop++ )
			total = total + numbers[loop];
		return total;
	}

6. A function prototype for the above function looks like

int total( char [] );
int total( int [], int );
void total( char [], int );
int total( void );

©Copyright B Brown. 1984-1999. All rights reserved.