Intro To C++/Functions Deprecated

From Wikibooks, open books for an open world
Jump to: navigation, search

Once you begin programming seriously, you are likely to find yourself writing the same (or very similar) lines of code over and over again. For instance, suppose you keep needing to collect 4 numbers from the program's user, and display both the sum and the product of these numbers. (Note: this is not meant to be a representative example. It is just an example meant to be simple enough for beginning programmers to understand). It is easy enough to do this once or twice, with code like this:

int a, b, c, d;
a = b = c = d = 0;
cin >> a >> b >> c >> d;
cout << "Sum: " << (a + b + c + d) << endl;
cout << "Product: " << (a * b * c * d) << endl;

However, typing all of those commands every time you wish to do this is a bad idea. For a sequence of commands this small, you might believe that the only potential problem could be your hands getting tired from typing too much. This is indeed a problem, but it could arguably be solved by copying and pasting code from one file to another instead of typing it directly. However, for larger sequences of commands, especially complex ones, even this strategy causes problems:

  • Modifications: If the requirements for a program change, affecting everywhere the sequence of commands is used, you may forget to update it in one or two of the places. Or you may remember to update it everywhere, but introduce potentially fatal typos into the program as you do so.
  • Bloat: The program's source code gets much longer than you, or anyone else reading the code, would like it to be.
  • Difficulty Implementing Certain Functionality: Some algorithms (ordered lists of instructions to follow to complete a task) are very difficult to implement using sequential lines of code, even though they are not necessarily complex tasks.

Fortunately, there is a way in C++ (and almost every other noteworthy programming language in existence) to define a sequence of commands that you can run anywhere in your program. This is called defining a function.

As an example, we can define a function that will print out our sum and product as shown above.

void sumAndProd(void)
{
    int a, b, c, d;
    a = b = c = d = 0;
    cin >> a >> b >> c >> d;
    cout << "Sum: " << (a + b + c + d) << endl;
    cout << "Product: " << (a * b * c * d) << endl;
}

Once this function has been defined, you can execute (or call) it by typing its name followed by "()".

sumAndProd();

(The semicolon terminating the statement is not part of the function call.)

Now you can copy the above code into any file where you might need its functionality. You can then call the function using the above syntax as many times as you want. Here's a complete program incorporating this function:

#include <iostream>
using namespace std;
 
void sumAndProd(void)
{
    int a, b, c, d;
    a = b = c = d = 0;
    cin >> a >> b >> c >> d;
    cout << "Sum: " << (a + b + c + d) << endl;
    cout << "Product: " << (a * b * c * d) << endl;
}

Note that you must place the definition of sumAndProd above where it is first used. Otherwise, your compiler will likely complain about a "reference to an undeclared identifier" or the like. However, there is an alternate method to tell your program that a function exists without defining it right away; this technique will be explained in more detail below.