Declaring and Defining Functions

 < Day Day Up > 



Now that you know what a function is it is time to learn how to create them. To write and use functions you will need to know how to do four things:

  1. How to name the function

  2. How to declare the function,

  3. How to define the function, and

  4. How to call the function in a program

Naming Functions

A function’s name should reveal its purpose. A program written with well-named functions is easy to read and easy to maintain. If you are writing programs for the first time you will be tempted to write short, cryptic function names in the interest of completing a project on time. The problem with this shortcut approach is that if you have problems getting your program to run, your instructors have to decipher your code before they can help you. Some so-called professional programmers do not follow this advice because they think they’re so good they do not need to follow any rules. These cowboy programmers produce code that’s hard if not impossible to maintain when they finally do leave the project. Develop good naming habits early. Doing so will pay off in the long run.

A function name can be any valid identifier, but since functions invoke some sort of processing they should be named using action words. The following examples show good function naming form:

getClassCount

setTemperatureValue

computeSum

addArrayElements

The naming convention used here is to lowercase the first letter of the first word and then use uppercase letters for the first letter of each word thereafter.

Function Declaration

Before a function can be defined or called from another function it must be declared. A function declaration takes the following form:

click to expand

Here are a few examples of function declarations:

int getClassCount();

void setTemperatureValue(float temp_val);

float computeSum(float a, float b = 0);

double addArrayElements(double the_array[]);

The getClassCount() function is declared to return an integer type and take no arguments. The setTemperature() function is declared to return no value and take one floating point argument. The third function, computeSum(), takes two floating point arguments and returns a float value. The parameter b is set to a default value of zero. In the absence of a second argument, computeSum() will set the value of the parameter b to zero for use in the body of the function. This means that computeSum() can be called with either one or two arguments. The addArrayElements() function takes an array of doubles as an argument and returns a double value.

Function Definition

Once a function has been declared it can be defined. Any programming statements required to give a function its behavior go into the body of the function definition. You will need a couple of things handy before you write a function definition. First, you will need access to the declaration of the function you are defining. If you have placed the declaration of the function in a separate header file you will need to include that file in the function implementation file. Second, you will need to include the header files for any other functions you are using to define your function.

Before studying a complete example take a look at the form of a function definition:

click to expand

Function Calling

A function is invoked via a function call. To call a function simply use the name of the function, supplying to it any arguments it requires. If you’re calling functions written by other programmers, like third party libraries, then you will need to include the header file that contains the function declaration, and the library code so your development environment can link to the function’s object code. If you’re writing the function, you will need to add the function’s implementation file to your project before you compile. Let us take a look at a complete example.

A Complete Example

In this example I will step you through the declaration, definition, and use of a simple function called testFunctionOne() that prints a short message to the screen. First, create the header file and to it add the function declaration. I am naming the file testfunctionone.h:

Listing 9.1: testfunctionone.h

start example
1 #ifndef TEST_FUNCTION_ONE_H 2 #define TEST_FUNCTION_ONE_H 3 4 void testFunctionOne(); 5 6 #endif
end example

Next, create the definition for testFunctionOne(). The definition should go in its own .cpp file. I am naming the file testfunctionone.cpp:

Listing 9.2: testfunctionone.cpp

start example
1  #include "testfunctionone.h" 2  #include <iostream> 3 4  using namespace std; 5 6  void testFunctionOne(){ 7     cout<<"Function Called: testFunctionOne()"<<endl; 8  }
end example

Notice on line 1 I’ve included the header file testfunctionone.h. This will make the testFunctionOne() function declaration accessible to the testfunctionone.cpp file. If you fail to declare a function before you define it you will receive a compiler error stating something to the effect, “...function does not have a prototype.” The exact message you receive will depend on your development environment.

On line 2 I’ve included the iostream header file. I need to do this because I’m using the cout object which is declared in that header file. The function definition for testFunctionOne() appears on lines 6 through 8. Since it is a void function no return statement is required. All that’s left now is to use testFunctionOne(). I will use it in the main() function as is shown in the following example:

Listing 9.3: main.cpp

start example
1 #include "testfunctionone.h" 2 3 int main(){ 4     testFunctionOne(); 5     return 0; 6 }
end example

Contents of main.cpp

The main() function is in a file to itself called main.cpp. Notice on line 1 that testfunctionone.h is included. This allows access to the declaration of the function so it can be called in the main() function. testFunctionOne() is then called on line 4, followed by a return statement, which is required by the main() function.

The two files, testfunctionone.cpp and main.cpp can now be compiled. Figure 9-1 is a CodeWarrior project screen shot showing the two files as part of the project:

click to expand
Figure 9-1: TestFunctionOne Project Screen Shot

The testfunctionone.h file is not explicitly added to the project but will be brought into the compilation process via the #include directive. It simply needs to reside in the same directory as the other project files. If you’re using an IDE other than CodeWarrior your process may differ somewhat but the basic steps are the same. (see chapter 2)

When all files are ready to go the project can be compiled and run. Figure 9-2 shows the results of running the program and the message printed to the screen by testFunctionOne():

click to expand
Figure 9-2: Results of Calling testFunctionOne()

Quick Review

Before a function can be defined or called it must first be declared. Start your function creation process by declaring the function in its own header file. That way you can use the #include directive to provide access to the function declaration to any file that needs it.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

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