Sunday, January 22, 2012

Introduction to C++ Functions


This blog assumes that the reader understand how to create a C++ program containing a single statement that displays the phrase "Hello World!" Therefore this blog focuses solely on functions and does not explain the main function nor the class libraries.

A function is a group of C++ statements that collectively perform a specific well defined task. Functions can be executed repeatedly throughout a program. The advantage of a function is that the programmer does not have to repeat the C++ statements each time the task needs to be performed.

First, I will explain each statement in the example, then I will present the complete tested and debugged example.

/* The first statement lists the iostream class. */

#include <iostream>

/* The second statement invokes namespace. */

using namespace std;

/* For more details on iostream and namespace see my blog titled
An Introduction to C++ Programming. */

/* Now we can insert our function header. You can name your function any name you like. I recommend that the function name describes what the function does. This will make it easier to understand the program when you come back to modify it. The word void at the far left of the function name means that the function does not return anything to the calling function. In this example, the calling function is the main function. The word void in the parenthesis means nothing is passed to the function from the main function. */

void Display_Hello_World(void)

/* The beginning of the function is defined by an opening curly bracket. */

{
/* The function only has one statement. That statement displays the words "Hello World" on the monitor. The cout function is defined in the iostream class library. */

cout << "Hello World!" << endl;

/* The end of the function is defined by a closing curly bracket. */

}

/* The main function invokes the function name Display_Hello_World. */

int main (void)
{
Display_Hello_World();
}

/* A function can return a value. The following function declares an integer, assigns a value to that integer and returns the integer value to the calling function. */

int FReturn_A_Value (void)
{
int a_value = 5;
return a_value;
}

/* The main function calls the FReturn_A_Value funtion and  and stores the value returned in my_value. */

int main (void)
{
int my_value;
my_value = FReturn_A_Value();
}

/* A variable can be passed to a function. The function FPass_A_Variable accepts an integer from the main function and displays it. */

void FPass_A_Variable(int my_value)
{
cout << my_value << endl;
}

/* The main funtion declares an integer, assigns the integer the value of 5 and passes the value to the function FPass_A_Variable. */

int main (void)
{
int value = 5;
FPass_A_Variable(value);
}

We can implement each defined task in a function. The function makes the program easier to read and easier to troubleshoot. The name of the function should describe what the function does. Likewise, the name of the variables should describe those variables. We know that the variable is an integer. The variable name should describe what the variable is.

An example is:
int temperature;

Here are examplies of function names
Read_Temperature().
Test_temperature_range().
Display_temperature().
Display_warning_message().

Note how easy it is to understand each function does.
The more clearly one describes variables and functions, the easier it is to read and understand the program.
The function should execute a single well defined task. The inputs and outputs of each function could be defined in the comments before the actual function.

/*****************************************************************
Function name
Inputs: Description of each input
Outputs: Describe the each output
Description: Describes what the function does
*********************************************************************/
/* Place function here */

The advantage of these comments is the time it saves if the programmer should have to modify the program months or years after the program was designed.

No comments: