Function Prototypes
#include <iostream> using namespace std; // Announce the upcoming function. These declarations // allow the function to be used before it is defined. void print_it(int n1, int n2, int n3, double av); double average(int a, int b, int); int main(void) { // Prompt for, and read, three integers. cout << "Please enter three integer values: "; int n1, n2, n3; cin >> n1 >> n2 >> n3; print_it(n1, n2, n3, average(n1, n2, n3)); } /* This function simply prints the numbers averaged, and the result. */ void print_it(int a1, int a2, int a3, double av) { cout << "The average of " << a1 << " " << a2 << " " << a3 << " is " << av << endl; } /* This function simply prints the numbers averaged, and the result. */ double average(int n1, int n2, int n3) { double sum; sum = n1 + n2 + n3; return sum / 3.0; }

Generally, C++ requires functions to be defined before they can be called. But it does allow for a function to be declared first, used, then defined later. The sytax is as shown: give the return type, name, and parameters, then a semicolon instead of a body. This sort of declaration is called a prototype in C++.

We will have more use for these later.