C++ programs are composed of some number of functions
representing groups of instructions. They are the building blocks upon
which programs are built. All programs must have a function called 'main'.
The first instruction in 'main' becomes the first instruction in the
program. A. Function Declarations
In C++, functions must be declared before they can be used. Except for
'main' which is always declared and defined at the same time, most C++
functions are declared at the beginning of a file (as prototypes)
and defined later in the same file. (The most common exceptions to this
are functions that are members of a class - see Section XI.) A function declaration (prototype) starts with a return type followed by the function's name and a list of types of values to be received and/or returned by the function. The list of values to be received and/or returned is enclosed in parentheses and is called the parameter list. The general form is as follows:
and a specific example would be: Except for the function name and the parentheses themselves the
elements are optional. Functions can return any type except an array or a
function type. If no type is included, the default is 'int'. Functions
that return nothing have a return type of void. One can consider the declaration of the return type as a description of
one of the mechanisms a function has to communicate with the rest of the
program. In C++, one function calls another to get the 'called'
function to execute its instructions and, if declared to do so, to return
some value(s). The parameter list represents a second, more complex
communication path (actually a set of paths – one for each parameter)
between the function doing the calling and the called function. If no
communication is needed between two functions (other than to simply have
the called function begin executing and, possibly, return one value), the
parameter list is empty - the parentheses exist but there is nothing
inside them. When the function to be called requires information from the calling
function or when it is desired that the 'communication paths' (other than
the one provided by the return mechanism) be used to return information, a
parameter list is included in the parentheses. Each element of the
parameter list is declared using a format similar to that used in variable
and constant declarations. The type of the parameter (what type of value
will be passed along the path) is written first, followed by the name for
of the identifier to be used both as the variable receiving and/or
returning the information being passed and in the code for the function.
(Technically, the identifier name is not required in the declaration, but
in this text it is considered good style to include it.) Commas separate
the elements of the list and a semicolon appears after the closing
parenthesis. To indicate that a parameter is to be used both to return and possible
receive a value, the '&' symbol is placed after the type and before
the identifier name. Such parameters are called reference
parameters . Arrays types do not need this symbol. (A more complex
mechanism for returning values via the parameter list involves the use of
pointer declarations. See the discusion on pointers in Section X.) It is possible to provide default values for parameters by including an
equal sign (=) and a value after the identifier. Such default declarations
must come at the end of the parameter list. One can also declare that a
function will receive a variable number of values by placing an ellipsis
(…) at the end of the list. To implement this capability, requires the use
of the "stdarg.h" header file and some special coding. If interested, read
the discussion of the "stdarg.h" header file in your C++ manual. Here are a few examples of function declarations: double function3(Type1 value1, int value2 = 0); void function4(int & value1 char& str, double
value2) int function5(char& str, double value1, ...) B. Calling Functions There are two mechanisms used to pass parameters. When a reference
parameter is passed, the memory address of the actual parameter
is passed to the function and the formal argument identifier becomes a
second symbolic name for the same memory location. This mechanism has the
obvious name pass by reference. The second mechanism is called pass by value. When a parameter
(also called an argument) is passed by value, a copy of the
information being passed is sent to the calling function and placed
in a new memory location set aside for the formal argumment. Thus, any
changes to this new memory location have no effect on the value in the
memory for the actual parameter in the calling function. The actual parameter list can include named or unnamed constants (for
example, strings or numeric values) and variables. One can also pass other
elements such as expressions and pointers to functions - see your manual
for more details. Rvalues such as
named and unnamed constants as well as complex expressions can passed by
reference but nothing will be returned. In the case of constants, this is
because the value of a constant cannot be modified. In the case of
expressions, there is no specific memory location to modify. C. Function Definitions If the function has been declared with a return
type, the return instruction should be included in every path
through the function. If not, one or more warning messages will be
generated. The 'return' takes the form: where 'some expression' is a constant, variable, or some more complex
expression representing the value to be returned by the function. When a function is called, the parameters are passed, the statements in
the function body are executed and the function terminates when it either
reaches the end of the function body or a return statement is executed.
Upon terminination, the memory for all identifiers with local duration is deallocated and the
identifiers with local scope become
unusable. If the calling function does not include a way for the value
returned (if there is one) to be used, that value is ignored. More on the basics of functions in Chapter 4 of the Story of C++ D. Function Overloading Function calls take time. The computer must save where it left off in
the calling function, allocate memory for any parameters being passed by
value, pass all the parameter values or addresses, and jump to the
instructions for the function being called. Upon returning, it must
de-allocate memory, perform the built-in return procedure, and jump back
to where it left off in the calling function. For reasons that are related
to software engineering principles, programs are designed to use function
calls even when the calling functions are very small and the overhead
takes more time than the actual function body execution. To meet the goals
of software engineering while improving efficiency, C++ provides inline
functions. For such functions, the calls look just the same but at
compile time, the compiler does not generate the code for a function call.
Instead, the function body is inserted into the calling function in place
of the function call. The programmer sees a function call, the computer at
run-time simply sees a few lines of code. There are two ways to declare a function as 'inline'. In the first way one adds the keyword 'inline' as the first word of a function definition. The second way is used in conjunction with class member functions and is discussed in Section XI. Links to 'The Story of C++" and other documents
|