Getting the work done

Functions



The basic format of all functions found in classes is:
    returnType className::functionName(argument list)
    {
        functionBody;
    }
returnType
className::
functionName
argumentList
functionBody

Function Prototypes

The purpose of a function prototype is to tell the compiler the correct syntax required to call the function. That is, the number and type of arguments to be passed to the function and the function return type. For functions not included in classes this is usually done at the head of the source code file (see example on the left below). Functions included in the header file defining a class are the prototypes for all functions found in the class definition file (.cpp).

While many people use the terms declaration and definition interchangably they have distinct meanings in C++.

Declaration - Introduces the name of the function. This is the function prototype given at the beginning of a file or in the header file. You can have many declarations, i.e. each file in a procedure program needs to prototype the functions it will call or in C++ each file that will use a class will need to include the header file to get the prototypes.

Definition - where the function code is actually found (in the implementation .cpp file for a class). You can have only one definition. (This is why you can’t #include a .cpp file.)

Default Arguments to Functions

Suppose you have some function...
    void someFunction(int x, double d, char c, long l)
    {
         // function body here
    }
In C++ you can specify default values for each of the arguments by modifying this function's prototype to be...
    void someFunction(int x=0, double d=1.5, char c='a', long l=1)
Now all of the following calls to someFunction are legal. Any variables that are not specified in a call to the function are automatically assigned the default value.

	someFunction(1, 2.5, ‘z’, 32);   // normal calling format
	someFunction(1, 2.5, ‘z’);       // 3 args (arg l takes default value)
	someFunction(1, 2.5);            // 2 args (args c and l take default values)
	someFunction(1);                 // 1 arg (args d, c, l take default values)
	someFunction();                  // 0 args (all take default values)
But, you can not skip any of the arguments in a call to the function. For example, the following is illegal because the char argument is omitted.
	someFunction(1, 2.5, 32);   // left out a char argument


Variable Argument Lists

If you declare a function, int function(); with no arguments, in C++ that is exactly what you get. In ANSI standard C this means a function with any number of arguments. To get the same thing in C you would have to declare this function as int function(void);. In C++, you can declare a function that takes any number of arguments with int function(...);. The example below illustrates how this is done:



Function Pointers

Functions exist in memory, therefore you can actually locate the address in memory where a function is and create a pointer to that function. The function pointer can then be used to call the function. Suppose you have a function:
    void myFunction(int x)
    {
        cout << "x = " << x << endl;
    }
You can now declare a pointer to a function with one int argument and returning void, then set the pointer to point to myFunction, and then call the function using the pointer:
    void(*funcPtr)(int);     // Declare a function pointer called funcPtr
    funcPtr = myFunction;    // Store the address of myFunction in the pointer
    funcPtr(255);            // Call myFunction using the pointer
Some of these function pointer definitions can become quite complex as shown below, but in the majority of programming instances they are never more complicated than the above example.

To decipher any of these start from the pointer name and work your way out to the argument types of the function, the return type, and in some of these examples to the pointer array type.

Virtual Functions

Recall the CShape class with the function virtual void draw() and the sub-classes that implemented their own version of that function.