By Stephen R. Davis

C++ functions that do nothing but return a value are of limited value because the communication is one-way — from the function to the caller. Two-way communication requires function arguments.

Function with arguments

A function argument is a variable whose value is passed to the function during the call. The following FactorialFunction converts the previous factorial operation into a function:

//
//  FactorialFunction - rewrite the factorial code as
//                a separate function.
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
//
// factorial - return the factorial of the argument
//             provided. Returns a 1 for invalid arguments
//             such as negative numbers.
int factorial(int nTarget)
{
    // start with an accumulator that's initialized to 1
    int nAccumulator = 1;
    for (int nValue = 1; nValue <= nTarget; nValue++)
    {
        nAccumulator *= nValue;
    }
    return nAccumulator;
}
int main(int nNumberofArgs, char* pszArgs[])
{
    cout << "This program calculates factorials"
         << " of user input.n"
         << "Enter a negative number to exit" << endl;
    // stay in a loop getting input from the user
    // until he enters a negative number
    for (;;)
    {
        // enter the number to calculate the factorial of
        int nValue;
        cout << "Enter number: ";
        cin  >> nValue;
        // exit if the number is negative
        if (nValue < 0)
        {
            break;
        }
        // display the result
        int nFactorial = factorial(nValue);
        cout << nValue << " factorial is "
             << nFactorial << endl;
    }
    // wait until user is ready before terminating program
    // to allow the user to see the program results
    cout << "Press Enter to continue..." << endl;
    cin.ignore(10, 'n');
    cin.get();
    return 0;
}

The declaration of factorial() includes an argument nTarget of int. Looking ahead, you can see that this is intended to be the value whose factorial the program calculates. The return value of the function is the calculated factorial.

In main(), the program prompts the user for a value, which it stores in nValue. If the value is negative, the program terminates. If not, it calls factorial() and passes the value of nValue. The program stores the returned value in nFactorial. It then outputs both values before returning to prompt the user for a new value.

Functions with multiple arguments

A function can have multiple arguments by separating them by commas. Thus the following function returns the product of two integer arguments:

int product(int nValue1, int nValue2)
{
    return nValue1 * nValue2;
}

Exposing main()

Now the truth can be told: The “keyword” main() from the standard template is nothing more than a function — albeit a function with strange arguments, but a function nonetheless.

When a program is built, C++ adds some boilerplate code that executes before your program ever gains control. This code sets up the environment in which your program will operate. For example, this boilerplate code opens the default input and output channels and attaches them to cin and cout.

After the environment has been established, the C++ boilerplate code calls the function main(), thereby beginning execution of your code. When your program finishes, it returns from main(). This enables the C++ boilerplate to clean up a few things before terminating the program and handing control back to the operating system.