by John
Kopp
Welcome to About.com's free tutorial on C++ programming.
This lesson covers functions. Functions are used to encapsulate
a set of operations and return information to the main program
or calling routine. Encapsulation is data, information or
detail hiding. Once a function is written, we need only be
concerned with what the function does. That is, what data it
requires and what outputs it produces. The details, "how" the
function works, need not be known. We have also seen
encapsulation with objects. Objects in C++, which encapsulate both data and methods, will
be discussed more in later lessons.
The use of functions provides several benefits. First, it
makes programs significantly easier to understand and
maintain. The main program can consist of a series of function
calls rather than countless lines of code. A second benefit is
that well written functions may be reused in multiple
programs. The C standard library is an example of the reuse of
functions. A third benefit of using functions is that
different programmers working on one large project can divide
the workload by writing different functions.
Defining and Declaring Functions A
function is declared with a prototype. A function prototype consists of
the return type, a function name and a parameter list. The
function prototype is also called the function declaration. Here are some examples of
prototypes.
return_type function_name(list of
parameters);
int max(int n1, int
n2); int printResults(string
buffer, int status);
| The function definition consist of the prototype and a
function body, which is a block of code enclosed in
parenthesis. A declaration or prototype is a way of telling
the compiler the data types of the any return value and of any
parameters, so it can perform error checking. The definition
creates the actual function in memory. Here are some examples
of functions.
int FindMax(int n1, int
n2) { if (n1 >
n2) { return
n1; } else { return
n2; } }
void
PrintMax(int
someNumber) { cout
<< "The max is " << someNumber <<
endl; }
void
PrintHW() { cout <<
"Hello World" << endl; }
float
FtoC(float faren) { float
factor = 5./9.; float
freezing = 32.0; float
celsius;
celsius = factor
* (faren -
freezing);
return
celsius; } | There are a few
significant things to notice in these examples. The parameter
list of a function may have parameters of any data type and
may have from no to many parameters. The return statement can be used to return a
single value from a function. The return statement is optional. For instance, the
function PrintHW does not return a value. Techniques for
returning multiple values from a function will be covered
later in the lesson. Finally, observe that variables can be
declared within a function. These variables are local
variables and can only be used within the function. They have
local scope. Scope refers to the section of code
where a variable name is valid and may be used. We'll see more
on scope in a later lesson. Next
page > Using
Functions > Page 1,
2,
3,
4,
5,
6
|