C++ void functions are called by using the name of the function,
along with any function parameters enclosed in parentheses as a single
statement:
int main() { Print2Lines();
// Function call cout << " C++ Sucks Less
" << endl; Print4Lines();
// Function call return 0; }
In C++, everything must be declared prior to its being used.
In the example program, the two functions are written after function main
which calls them. Function prototypes
are statements that declare what the function
looks like but do not specify any of the implementation details:
// -----------------------
Function prototypes void Print2Lines(); void Print4Lines();
Every C++ function begins with a function
header that specifies essentially the same
information that the function prototype does:
void Print2Lines() //
Function heading
Note that the function prototype is
a complete statement and ends with a semicolon. The function heading does
not end in a semicolon because it is only the beginning of the function
definition.
Following the function header, the
body of the function is written as a block of C++ statements enclosed in
braces:
Void functions do not return a function
value. Control is returned to the calling function either after the last
statement in the function is executed or when the return statement is executed
(as in the previous example):
Notice that the return statement is
not followed by an expression.
As a matter of style, void function
names should begin with a capital letter. Each additional word in the function
name should also begin with a capital letter.
Void function names that begin with
a verb tend to make a program easier to understand. For example Print4Lines
is a better name than something like Lines4.
Function Parameters
Parameters
are variables and expressions that are used to pass information to and
from functions.
numLines and count
are local to the void function PrintLines.
Value and Reference
Parameters
C++ supports two kinds of formal parameters
- value and reference parameters.
A value
parameter is a formal parameter that receives
a copy of the contents of the corresponding actual parameter.
A reference
parameter is a formal parameter that receives
the location (memory address) of the calling functions actual parameter.
Value parameters are the default for
simple data types such as int, char, float
, etc.
Reference parameters are indicated
by the & character in the formal parameter list:
void Example( int&
refParm,
int valParm,
float &refFloat)
Notice that the ampersand character
can be written immediately after the data type or immediately before the
name of the reference parameter.
Any change to a value parameter by
a function changes only the local copy of the parameter and leaves the
original value in the calling function unchanged.
Any change to reference parameter by
the function results in a change to the actual parameter in the calling
function.
Reference parameters can be used to
pass data back to the calling function from a function.
Example Program
3 illustrates the use of value and reference parameters.
Programming
Example
Programming Problem 3 on page 383 of
the text illustrates the use of functions.
The following is a design for a solution
of the problem:
Pseudocode:
Calendar Program
Ask user for year and day of January 1st
for each month
PrintMonth(month, year, day)
PrintMonth(month, year, startDay)
PrintMonthHeader(month, year)
daysInMonth = MonthDays(month, year)
output empty days for 1st line
for each day in the month
output the day
increment startDay
if startDay is Saturday
output end of line
startDay = Sunday
PrintMonthHeader(month, year)
output month name and year
output days of the week
output seperator line
MonthDays(month, year)
if month is January, March, May, July, August,
October or December