CIS Logo SVC Logo

   Computing & Information Systems
   Department

 

Schoology Facebook        Search CIS Site      Tutorials

Software Design Using C++



Simple C++ Functions



Introduction


This section shows how to subdivide a program into functions. A function packages up a useful section of code so that it can be easily called upon whenever we need it. Here we consider only functions without parameters and without return values. (See the section on Complex Functions for information on these. One exception is the main function itself, which we always set up to return an integer to the operating system.)

Example


The best place to start is with an example. Take a look at area2.cpp, another program that finds the area of a rectangle. Here various parts of the program are carried out by functions. There is a function that prints an explanation of what the program does. There is a function that handles the area computation (including getting the length and width from the user and the printing of the answer). Of course there is a main function; there always has to be a main function.

Divide and Conquer


Dividing a problem up into several functions, each of which does one key portion of the problem, is a common design approach. The idea is sometimes called "divide and conquer", since one large problem is divided up into several smaller ones (each of which is carried out by a function). These smaller problems can themselves be subdivided, etc.

Flow of Control


Execution begins with the main function. It calls the Explanation function, so control passes to that function. The code in the Explanation function is then executed in order. When the function is finished, control passes back to the main function, immediately after the call of this function. The main function then calls the DoArea function. This function's code is then executed. When it completes, control is passed back to the main function, right after the call of this function. Since all that remains in the main function is the return command, the program then ends.

Syntax


The void fn_name(void) pattern is used for all of the functions (other than the main function) in this example. Later you will see other variations on this pattern. Note, too, that all of the functions have their code inside of a block delimited by curly braces, and that the block is indented so that it is clearly visible.

Note that each function call (here found inside of the main function) consists of the function name followed by parentheses. In later programs we will put parameters inside of the parentheses, but here our functions have no parameters.

Function Prototypes


Also, notice the use of "function prototypes", which look like the following and appear at the top of the source code file. They instruct the compiler that we will have functions with these names, parameter lists, etc. That way, you can place the three functions in any order down the page. Without function prototypes you have to be sure that the code for a function is given above any other function which calls the first function. Since that can be confusing to keep track of, function prototypes are highly recommended.


void Explanation(void);
void DoArea(void);

One small item of syntax that is sometimes confusing should be mentioned here. Note that there is a semicolon at the end of each function prototype. When you write the code for a function (its definition) you repeat the same prototype, but without the semicolon, and then follow that by the code inside of curly braces.

Commenting a Function


Each function that you write should have a comment section just before it that uses the "Given, Task, Return" style as shown. The "Given" section describes any data that is passed into the function (none in these example functions). The "Task" section describes the overall task of this function. The "Return" section describes any data passed back out of the function (nothing in these examples). We will later see more interesting functions where values are passed in and out. There are other styles of commenting functions, but the "Given, Task, Return" style will be used consistently throughout these Web pages.

Related Items

Back to the main page for Software Design Using C++

Author: Br. David Carlson with contributions by Br. Isidore Minerd
Last updated: August 27, 2009
Disclaimer