mirror of
https://gitlab.com/fbb-git/cppannotations
synced 2024-11-18 10:06:54 +01:00
56569e9174
git-svn-id: https://cppannotations.svn.sourceforge.net/svnroot/cppannotations/trunk@251 f6dd340e-d3f9-0310-b409-bdd246841980
56 lines
2.1 KiB
Text
56 lines
2.1 KiB
Text
In bf(C++) it is possible to provide `i(default arguments)' when defining a
|
|
function. These arguments are supplied by the compiler when they are not
|
|
specified by the programmer. For example:
|
|
verb(
|
|
#include <stdio.h>
|
|
|
|
void showstring(char *str = "Hello World!\n");
|
|
|
|
int main()
|
|
{
|
|
showstring("Here's an explicit argument.\n");
|
|
|
|
showstring(); // in fact this says:
|
|
// showstring("Hello World!\n");
|
|
}
|
|
)
|
|
The possibility to omit arguments in situations where default arguments
|
|
are defined is just a nice touch: it is the compiler who supplies the lacking
|
|
argument unless it is explicitly specified at the call. The code of the
|
|
program will neither be shorter nor more efficient when default arguments are
|
|
used.
|
|
|
|
Functions may be defined with more than one default argument:
|
|
verb(
|
|
void two_ints(int a = 1, int b = 4);
|
|
|
|
int main()
|
|
{
|
|
two_ints(); // arguments: 1, 4
|
|
two_ints(20); // arguments: 20, 4
|
|
two_ints(20, 5); // arguments: 20, 5
|
|
}
|
|
)
|
|
When the function tt(two_ints) is called, the compiler supplies one or two
|
|
arguments whenever necessary. A statement like tt(two_ints(,6)) is, however,
|
|
not allowed: when arguments are omitted they must be on the right-hand side.
|
|
|
|
Default arguments must be known at i(compile-time) since at that moment
|
|
arguments are supplied to functions. Therefore, the default arguments must be
|
|
mentioned at the function's em(declaration), rather than at its
|
|
em(implementation):
|
|
verb(
|
|
// sample header file
|
|
extern void two_ints(int a = 1, int b = 4);
|
|
|
|
// code of function in, say, two.cc
|
|
void two_ints(int a, int b)
|
|
{
|
|
...
|
|
}
|
|
)
|
|
It is an error to supply default arguments in function definitions. When
|
|
the function is used by other sources the compiler reads the header file
|
|
rather than the function definition. Consequently the compiler has no way to
|
|
determine the values of default function arguments. Current compilers generate
|
|
compile-time errors when detecting default arguments in function definitions.
|