Defining and Calling a Function


Implementing any function in addition to main involves two steps:

  1. Defining the function

  2. Calling the function

The explanation of these steps uses terminology we have not discussed before, so that terminology is reviewed first.

Terminology of a Function

Let s look at a simple program with one function, main:

 #include <iostream> using namespace std; int main () {  cout << "Hello world!";  return 0; } 

The first line, int main (), is the function header. Unlike a statement, the function header is not followed by a semicolon.

The function header consists of a return type, a function name , and an argument list. The data type int preceding main is the return type, main is the function name, and the parentheses, empty in this example but not always, contains the argument list.

A function header always is followed by an open curly brace , which begins the function body. The function body ends with a close curly brace. There may be other open and curly braces between the open curly brace that begins the function body and the close curly brace that ends it, such as to enclose multiple statements that belong to an if statement or a loop.

The function body consists of one or more statements. In this example, the function body consists of two statements. The last statement, return 0, is a return statement. The function body must contain a return statement unless the return type is void, in which case the return statement is optional.

The function header and body together are referred to as the function definition . A function cannot execute until it is first defined. Once defined, a function executes when it is called.

Normally, a function is called through code. The main function is the exception. The main function is called automatically when your program begins to run.

The next sections will explain how to define your own function and then call it.

Defining a Function

Let s take our Hello World example and divide the code into two functions, main and a printMessage function that outputs Hello world! The comments (beginning with //) indicate the beginning and end of the definition of the printMessage function and where that function is called.

 #include <iostream> using namespace std; // begins definition of printMessage function void printMessage (void) {  cout << "Hello world!"; } // ends definition of printMessage function int main () {  printMessage(); // calls printMessage function  return 0; } 

The printMessage function is defined first. The void keyword preceding the function name printMessage means that this function does not return a value. The void keyword in parentheses following the function name means this function has no arguments. The parentheses also could be left empty, such as after main; empty parentheses following the function name in a function header is the same as placing the void keyword within the parentheses. Which syntax you choose is a matter of taste; one is no better or worse than the other.

The body of the printMessage function has one statement, which outputs Hello world! The function body does not need to contain an explicit return statement because, since the return type is void, the return statement is implied . However, you may include an explicit return statement. If you did, then the printMessage function would read

 void printMessage (void) {  cout << "Hello world!";  return; } 

Calling a Function

Unless the printMessage function is called, it is the programming equivalent of the tree that falls in the forest without anyone seeing or hearing it; it is there in the program, but it doesn t do anything. The printMessage function is called in main with the line:

 printMessage(); 

In this example, printMessage is the called function, since it is the function being called from main. The empty parentheses indicate that no arguments are being passed to this function. I will show you later in this chapter how to pass arguments, as well as how to use return values.

The order of execution is as follows :

  1. Execution always starts with the main function.

  2. The first statement in main , printMessage(), is executed.

  3. Execution next shifts to the printMessage function, and begins with the first statement in that function, which outputs Hello world!

  4. After the printMessage function completes executing, execution returns to the main function with the next unexecuted statement, return 0, which completes the main function.

Figure 9-1 shows the order of execution graphically.

click to expand
Figure 9-1: Order of execution of the Hello World Program

Prototyping

Since execution always starts with main, it seems more logical to place the main function first, ahead of the printMessage function, such as in the following example:

 #include <iostream> using namespace std; int main () {  printMessage();   return 0; } void printMessage (void) {  cout << "Hello world!"; } 

However, this code will not compile. The call in main to printMessage() will be highlighted, with the compiler error message being undeclared identifier.

The reason for this compiler error is that when the compiler, going from top to bottom in your code, encounters a function call, it must already know of the function s name, return type, and arguments. This was not a problem when the printMessage function was defined above the main function. However, when the printMessage function was defined below the main function, when the compiler encounters the call in main to printMessage(), it does not yet know of the printMessage function s name, return type, and arguments.

One solution to this problem is to define all functions above main. However, this may make your code difficult to read. A program s execution always starts with main, regardless of the order in which functions are defined. In a program with many functions, the main function often acts as a switchboard, calling one function after another. Therefore, viewing the main function can provide an excellent overview of the order of events. Burying the main function beneath numerous other functions requires someone reviewing your code to hunt for main to obtain that overview. Additionally, in complex programs in which one function calls another function which calls still another function, the order in which to define these functions to avoid a compiler error can be confusing.

The solution of preference is to prototype each function, except main, which does not have to be prototyped since it is required by every program. The following program shows how to prototype the printMessage function in the Hello World program:

 #include <iostream> using namespace std; void printMessage(void); // this is the prototype! int main () {  printMessage();   return 0; } void printMessage (void) {  cout << "Hello world!"; } 

The prototype is above all function definitions. This ensures that the compiler, compiling the code from top to bottom, will encounter the prototype before any function.

The prototype is similar to a function header. The primary difference is that it has a semicolon at the end because it is a statement. By contrast, a function header must not be followed by a semicolon.

Note  

There are other differences between the prototype and the function header when, unlike here, the parentheses following the function name includes one or more arguments. Those differences will be discussed in the section Sending Information to a Function later in this chapter.




C++ Demystified(c) A Self-Teaching Guide
C++ Demystified(c) A Self-Teaching Guide
ISBN: 72253703
EAN: N/A
Year: 2006
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net