Function is a very important concept in C++. Every C++ program must have
a main()
function which is what you
have been using so far. The C++ compiler looks for the main function
when it compiles the program. If the main function is not found, a
compiler error will occur. You already know the following format that is
used for the main function.
int main()
{
.
.
.
return 0;
}
This function returns a single value 0. Thus, it is a
value-returning function. The return data type is
int
. The statement return 0;
is used to complete the function
and the value 0 is returned to the operating system if the function has
been processed correctly. The return
indicates the end of the function.
Some functions we have used, such as .open()
in InData.open()
and get()
in cin.get()
, do not return any values.
It is only the name of an action. These kind of functions are called
void
functions.
The C++ system includes a standard library - a large collection of
prewritten functions, data types, and other items that any C++
programmer may use. To use library functions, you have to place an #include
directive near the top of
the program, specifying the header file. For example, to use cin
and cout
, you have to have #include<iostream>
in your
program; to use sqrt(x)
and pow(x, y)
functions, you have to have #include<cmath>
in your
program.
In this lab, we focus on user defined functions. User defined functions are often declared before they are actually used/called. This forward declaration is also known as a function prototype. The general syntax of a function prototype is:
returnDataType functionName(Parameter_DataType_List);
If a function is a value-returning function, the returnDataType will be
something like int
, float
, or char
and so on. If the function does not return any value, we use the word void
for
the return data type. Once a function
is declared, it must be defined somewhere in the program. The definition of
a function has the following general format:
returnDataType functionName(ParameterList)
{
// Statements
.
.
.
}
Now, let's look at void
functions.
Here is the syntax template of a void function prototype:
void functionName(Parameter_DataType_List);
Here is the syntax template of a void function definition:
void functionName(ParameterList)
{
//Statement
.
.
.
}
A void function has a heading that names the function followed by a pair of parentheses. The function identifier/name is preceded by the word void. The parameter list with the corresponding data type is within the parentheses. The body of the function is between the pair of braces.
// Program Stars prints num_stars on the screen.
#include <iostream>
using namespace std;
const int num_stars = 10;
// Prints num_stars stars on the screen.
void printStars(); //Function prototype, no parameter
int main ()
{
cout << "The next lines contain " << num_stars
<< " stars. " << endl;
printStars(); //Function call
printStars(); //Function call
return 0;
}
//********************************************************
//Function definition, no parameter
//********************************************************
// Post: num_stars asterisks are sent to cout.
void printStars()
{
cout << "**********" << endl;
return;
}
In the above example, void printStars()
is
the function header. There is no parameter list, but the parentheses that
enclose the parameter list must be present. The braces enclose the body of
the function, the action part of the function. The output statement prints
10 stars. The return statement ends the function, but it is not required in
void functions.
In the main function, printStars()
is
invoked/called and used as a statement to print out the 10 stars. Now,
let's look at Value-returning functions.
© Department of Computer Science, University of Regina.