Functions Cont.

More Example Functions

Let's look at some more examples of functions:

A function to return the maximum of two floats:
float max (float a, float b) {
	if (a > b)
		return a;
	else
		return b;
}
Note that we can omit the else in that function, but it looks a little nicer if we don't. How would you write a function to return the minimum?

A predicate function to determine whether a given character is alphabetic (as opposed to numeric or punctuation). It should return 1 if the character parameter is either A through Z or a through z, 0 otherwise:
int is_alpha (char ch) {
	if (c <= 'z' && c >= 'a') return 1;
	if (c <= 'Z' && c >= 'A') return 1;
	return 0;
}

void Functions

Some functions return nothing, they just do something useful like printing something out or do other non-mathematical tasks. These functions are declared as returning the type void, a C type that means "nothing." If you think of int as representing the set of integers, and float as representing the set of real numbers, think of void as representing the empty set. Let's look at an example of a void function that prints a prompt based on two parameter values:
void print_prompt (float a, float b) {
	printf ("Enter a number between %f and %f: ", a, b);
}
So this function prints a prompt asking for numbers in a certain range. It can be followed by a call to scanf, or by a function that does the reading, like this:
float get_number (float a, float b) {
	float	c;

	scanf ("%f", &c);
	while (c > b || c < a) {
		printf ("Number out of range, try again: ");
		scanf ("%f", &c);
	}
	return c;
}
So a main for this would look like:
int main () {
	float	f;

	print_prompt (1, 10);
	f = get_number (1, 10);
	/* do something with f... */
}

A large program split up into functions

Let's write a program that plays a game with the user. The computer will think of a random number between 1 and 100, and the user will try to guess it. When the user types in a number, the computer will tell the user whether the guess is correct, too high, or too low, and keep track of the number of guesses. Each time the user makes a guess, the computer updates the range of numbers for the user to type in. One run of the game would look like this:
Enter a number between 1 and 100: 40
too high!
Enter a number between 1 and 40: 25
too high!
Enter a number between 1 and 25: 3
too low!
Enter a number between 3 and 25: 45
Out of range!
Enter a number between 3 and 25: 16
too low!
Enter a number between 16 and 25: 20
too low!
Enter a number between 20 and 25: 23
too low!
Enter a number between 23 and 25: 24
correct!  you took 7 guesses.
To get the computer to "think" of a random number, we'll call the rand() function from stdlib.h. It returns a "pseudorandom" number in the range 0..32767, which we can make into a random number between 1 and 100 by taking the remainder when dividing by 100, then adding 1. This program will be kind of big, so we'll split it up into smaller functions: It's nice to have main come first, so we'll use function prototypes, i.e., declarations without definitions, to tell the program about the other functions. Prototypes look like function definitions, but instead of having a body between curly braces, they simply have a semicolon, and don't have to give names to the parameters, just the types. Here's the program.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* function prototypes */

int get_random (int, int);
void print_prompt (int, int);
int get_guess (int, int);

/* main function */

int main () {
	int	secret, upper, lower, guess, count;

	/* "seed" random number generator with the current time in number
	 * of seconds since 1970 so it gives a different sequence
	 * of numbers each time the program is run
	 */
	srand (time (NULL));

	/* the lower limit is 1, the upper limit is 100 */

	lower = 1;
	upper = 100;

	/* 1 guess so far... */

	count = 1;

	/* think of the random number */

	secret = get_random (lower, upper);

	/* start the guessing */

	guess = get_guess (lower, upper);

	/* while the user is wrong... */

	while (guess != secret) {

		/* one more guess */

		count++;

		/* if the guess was too low... */

		if (guess < secret) {
			printf ("too low!\n");

			/* update the lower limit */

			lower = guess;
		} else {
			printf ("too high!\n");

			/* update the upper limit */

			upper = guess;
		}

		/* get another guess */

		guess = get_guess (lower, upper);
	}

	/* finally... */

	printf ("correct!  you took %i guesses.\n", count);
	return 0;
}

/* this function returns a random number between a and b 
 * using the rand() function, which returns a random number
 * between 0 and 32767
 */
int get_random (int a, int b) {
	int	sample;

	/* sample is a random number between 0 and 32767 */

	sample = rand ();

	/* sample is a random number between 0 and b-a */

	sample = sample % (b - a);

	/* sample is a random number between a and b */

	sample = sample + a;
	return sample;
}

/* this function prints a prompt, asking for a number in a range
 * of integers
 */
void print_prompt (int x, int y) {
	printf ("Enter a number between %i and %i: ", x, y);
}

/* this function gets a guess from the user, making sure it is
 * in the right range 
 */
int get_guess (int a, int b) {
	int	c;

	/* print the prompt */

	print_prompt (a, b);

	/* read the value */

	scanf ("%i", &c);

	/* if value is out of range, give a warning and do it all over again */

	while (c < a || c > b) {
		printf ("Out of range!\n");
		print_prompt (a, b);
		scanf ("%i", &c);
	}

	/* return the value */

	return c;
}