Functions

Functions are useful in breaking up a large program into smaller, more manageable segments.

Syntax


The syntax for a typical function is:

ftype function_name (type1 param1, type2 param2, ... , typeX paramX) {

// Statement block;

}

Notes

· ftype is the type of value to be returned. Common type variables are: int (integer), char (character), float (decimal)and bool (boolean).

· function_name is the name of the function.

· typeX and paramX are the types and names of the parameters to be passed in, respectively.

· If no value is to be returned by the function, a void type is specified.

· ADVANCED USERS ONLY:

· Function Overloading

· When passing large parameters which take up a lot of memory (e.g. objects), passing their memory address (pass by reference) is preferred over sending the entire object. If all the values of the object are sent to a function, a second copy is made by the function; whereas if it were passed by a pointer, the original data would be modified.

· If you are using both global and local variables, the tutorial on the scope of variables in a C++ program is highly recommended reading.


Function Overloading

Description

A function name with more than one declaration is said to be an overloaded function. Each declaration of the function name must have a different number or type of parameters so that the C++ compiler will be able to determine which function the user wishes to call. When a call to an overloaded function is made, the one that matches the type of arguments being passed will be chosen.

Create the functions as you would normally and ensure that the type and order of arguments will be distinguishable by the compiler.


Examples

// Definition 1

int overloadFunc (float x) {

cout << "I accepted a float." << endl;

}

// Definition 2

int overloadFunc (int x) {

cout << "I accepted an integer." << endl;

}

// Definition 3

int overloadFunc (int x, int y) {

cout << "I accepted two integers." << endl;

}

int main () {

overloadFunc (4); // calls definition 2

overloadFunc (3.14); // calls definition 1

overloadFunc (4, 10); // calls definition 3

}


· When we make a call to overloadFunc(), the C++ compiler will compare the parameters being passed with what the defined functions will accept. For example, the call overloadFunc(4, 10) will cause the compiler to begin a search through all the overloadFunc() definitions in hopes of finding one whose arguments match the types/number of parameters being passed. When one is found, (in this example, definition 3) that procedure is executed; otherwise, an error will be sent to standard output.

· When using default parameters, be careful not to accidentally over-step other defined functions. For example,

// Functions

int overloadFunc (int x) { /* definition A */ }

int overloadFunc (int x, int y = 0); { /* definition B */ }

// Call

overloadFunc (5);

In this particular case, the C++ compiler will be unable to determine which function is being called. Definition A accepts an integer as a parameter, so this is a valid choice. Likewise, Definition B can also accept one integer (and makes y equal to 0), so this too is a valid choice. The executed function will be system/compiler dependent.

· If you are having difficulty determining which function is being called, add debugging messages to each version of the function so you can see which version is being called, as in the example.

· Include only one version of the function for all possible combinations of parameter types. This will make your program "cleaner" (less redundant code) and easier to modify.