File: default.yo

package info (click to toggle)
c%2B%2B-annotations 12.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,044 kB
  • sloc: cpp: 24,337; makefile: 1,517; ansic: 165; sh: 121; perl: 90
file content (54 lines) | stat: -rw-r--r-- 2,172 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
    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 both function definitions and
function declarations. When applicable default arguments should be provided in
function declarations: when the function is used by other sources the compiler
commonly reads the header file rather than the function definition
itself. Consequently the compiler has no way to determine the values of
default arguments if they are provided in the function definition.