summaryrefslogtreecommitdiff
path: root/reference/CPLUSPLUS/CONCEPT/fundefault.html
blob: 80cd0aa6284ea092f0c56431eda162d666d9ccd1 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<html>
<head>
<title>Default Parameters.</title>
</head>
<body bgcolor="#dddddd">
<font color=brown>
<hr>
<center><h1>Default Parameters.</h1></center>
<hr>
<p>
Within C you could not provide default values for function
parameters. Well, C++ has come to your rescue. 
<p>

<center>
<table border="1" width="80%" bgcolor="ivory">
<tr>
<td>
<pre>

    #include &lt;iostream.h&gt;
    
    void Func( int one, <b>int two=2, int three=3</b>);
    
    main ()
    {
        Func(10, 20, 30);
        Func<b>(10, 20);   // Let the last parm default</b>
        Func<b>(10);       // Just provide the required parm.</b>
    }
    

    void Func( int one, int two, int three)
    {
        cout << "One   = " << one   << endl;
        cout << "Two   = " << two   << endl;
        cout << "Three = " << three << endl << endl;
    }
</pre>
</td>
</tr>
</table>
</center>

<p>
From this example, you can see that the prototype/function declaration
gives default values for the second and third parameters. It is now 
down to the programmer calling the function to decide how many
s/he wants to provide.
<p>
There are some basic rules that should be applied when using 
default parameter values.
<ul>
<li>When a default is provided, all remaining parameters must be
    given defaults. There can not be any holes.
<li>Default values must be of the correct type.
<li>Defaults can be placed on the function prototype or the function
    definition. They can also be placed in both locations at the
    same time. I have been told its good practice to place them 
    in the prototype only.
</ul>
<p>
<hr>
<h2>Examples:</h2>
<!-- 
<img src="../../GRAPHICS/computer.gif" alt="o">
<a href="../EXAMPLES/xxxx.cc">Example program.</a>
<p>
-->

<hr>
<h2>See Also:</h2>

<img src="../../GRAPHICS/whiteball.gif" alt="o">
<a href="funcoverload.html">Function Name Overloading</a>.
</font>
<hr>
<font color=black>
<h2>C References</h2>
<p>
<img src="../../GRAPHICS/whiteball.gif" alt="o">
<a href="../../C/SYNTAX/functions.html">Function basics</a>.

</font>

<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html">Top</a>
</td><td width="25%">
<a href="../../C/master_index.html">Master Index</a>
</td><td width="25%">
<a href="../SYNTAX/keywords.html">Keywords</a>
</td><td width="25%">
<a href="../../C/FUNCTIONS/funcref.htm">Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<font color=brown>
<address>Martin Leslie 
<script language="JavaScript">
<!--  //
document.write(document.lastModified);
// -->
</script>
</address><p>
</font>
</body>
</html>