4.
Functions in C++
Main( ) in C does not return any value. In C++, the main( )
returns a value of type int to the operating system. C++,
therefore, explicitly defines main( ) as matching one of the
following prototypes:
int main( );
int main(int argc, char * argv[]);
Provision of the reference variables in C++ permits us to
pass parameters to the functions by reference. When we pass
arguments by reference, the ‘formal’ arguments in the called
function become aliases to the ‘actual’ arguments in the
calling function. This means that when the function is working
with its own arguments, it is actually working on the original
data.
One of the objectives of using functions in a program is to
save some memory space, which becomes appreciable when
a function is likely to be called many times. However, every
time a function is called, it takes a lot of extra time in
executing a series of instructions for tasks such as jumping to
the function, saving registers, pushing arguments into the
stack, and returning to the calling function. When a function
is small, a substantial percentage of execution time may be
spent in such overheads. One solution to this problem is to
use macro definitions, popularly known as macros.
Preprocessor macros are popular in C. The major drawback
with macros is that they are not really functions and
therefore, the usual error checking does not occur during
compilation.
To eliminate the cost of calls to small functions, C++
proposes a new feature called inline function. An inline
function is a function that is expanded in line when it is
invoked. That is, the compiler replaces the function call with
the corresponding function code (something similar to
macros expansion). The inline functions are defined as
follows:
inline function-header
{
function body
}
The speed benefits of inline functions diminish as the function
grows in size. At some point the overhead of the function call
becomes small compared to the execution of the function,
and the benefits of inline functions may be lost. In such
cases, the use of normal functions will be more meaningful.
Usually, the functions are made inline when they are small
enough to be defined in one or two lines.
Remember that the inline keyword merely sends a request,
not a command, to the compiler. The compiler may ignore
this request if the function definition is too long or too
complicated and compile the function as a normal function.
Some of the situations where inline expansion may not work
are:
1. For functions returning values, if a loop, a switch, or a goto
exists.
2. For functions not returning values, if a return statement
exists.
3. If functions contain static variables.
4. If inline functions are recursive.
C++ allows us to call a function without specifying all its
arguments. In such cases, the function assigns a default
value to the parameter which does not have a matching
argument in the function call. Default values are specified
when the function is declared. Here is an example of a
prototype (i.e., function declaration) with default values:
float amount(float principal, int period, float rate=0.15);
A default argument is checked for type at the time of
declaration and evaluated at the time of call. One important
point to note is that only the trailing arguments can have
default values and therefore we must add defaults from right
to left.
Default arguments are useful in situations where some
arguments always have the same value. For instance, bank
interest may remain the same for all customers for a
particular period of deposit.
A function call first matches the prototype having the same
number and type of arguments and then calls the appropriate
function for execution. A best match must be unique. The
function selection involves the following steps:
1. The compiler first tries to find an exact match in which the
types of actual arguments are the same, and use that
function.
2. If an exact match is not found, the compiler uses the
integral promotions to the actual arguments, such as,
char to int
float to double
to find a match.
3. When either of them fails, the compiler tries to use the
built-in conversions (the implicit assignment conversions) to
the actual arguments and then uses the function whose
match is unique.
If the conversion is possible to have multiple matches, then
the compiler will generate an error message.
In C++ the main( ) returns a value of type int to the
operating system. Since the return type of functions is int by
default, the keyword int in the main( ) header is optional.