You are here

Specifc Task Function

5 February, 2015 - 12:19

We often have the need to perform a specific task that might be used in many programs. In the Compile Test.cpp source code above we have such a task that is used to stop the execution of the code until the user hits the enter key. The functions name is: pause. This function is not communicating any information between the calling function and itself, thus the use of the data type void.

Example: general layout of a function

<return value data type> function identifier name(<data type><identifier name for input value>) { lines of code; 
return <value>; } 

There is no semi-colon after the first line. Semi-colons are used at the end of a statement in C++, but not on the first line when defning a function. Functions have a set of braces {} used for identifying a group or block of statements or lines of code. There are normally several lines of code within a function. Lines of code containing the instructions end in a semi-colon. Can you identify the definition of the pause function in the above program example? The pause function definition is after the function main. Though not technically required, most programs list all functions (program control or specific task) after the function main.

Let's identify the location where the function pause is called. The calling function is the function main and it towards the end of the function. The line looks like:


When you call a function you use its identifier name and a set of parentheses. You place any data items you are passing inside the parentheses, and in our example there are none. A semi-colon ends the statement or line of code. After our program is compiled and running, the lines of code in the function main are executed and when it gets to the calling of the pause function, the control of the program moves to the pause function and starts executing the lines of code in the pause function. When it's done with the lines of code, it will return to the place in the program that called it (in our example the function main) and continue with the code in that function.

Once we know how to define a function and how to call a function, we usually will need to know how to declare a function to the compiler (called a prototype). Because of normal computer programming industry standards, programmers usually list the function main first with other functions defined after it. Then somewhere in the function main, we will call a function. When we convert our source code program to an executable version for running on our computer, the first step of the process is compiling. The compiler program demands to know what the communication will be between two functions when a function is called. It will know the communication (what going in and out as parameters) if the function being called has been defined. But, we have not defined that function yet; it is defined after the function main. To solve this problem, we show the compiler a prototype of what the function will look like (at least the communication features of the function) when we define it.

void pause(void);

This line of code looks exactly like the first line in our function definition with one important addition of a semi-colon. Prototypes (or declarations to the compiler of the communications of a function not yet defined) are placed near the top of the program before the function main. Summary concept: If you call a function before it is defined you must prototype it before it is called. Looking at our list of the three things you do in conjunction with a function in the order that they normally appear in a program, there is a formal way to:

  1. declare a function (a prototype is a communications declaration to a complier)
  2. call a function
  3. define a function