1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Function Parameter Variables (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Function Parameter Variables (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Function Parameter Variables (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Function-Definitions.html" rel="up" title="Function Definitions">
|
---|
33 | <link href="Forward-Function-Declarations.html" rel="next" title="Forward Function Declarations">
|
---|
34 | <link href="Function-Definitions.html" rel="prev" title="Function Definitions">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Function-Parameter-Variables"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Forward-Function-Declarations.html" accesskey="n" rel="next">Forward Function Declarations</a>, Up: <a href="Function-Definitions.html" accesskey="u" rel="up">Function Definitions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Function-Parameter-Variables-1"></span><h4 class="subsection">22.1.1 Function Parameter Variables</h4>
|
---|
64 | <span id="index-function-parameter-variables"></span>
|
---|
65 | <span id="index-parameter-variables-in-functions"></span>
|
---|
66 | <span id="index-parameter-list"></span>
|
---|
67 |
|
---|
68 | <p>A function parameter variable is a local variable (see <a href="Local-Variables.html">Local Variables</a>) used within the function to store the value passed as an
|
---|
69 | argument in a call to the function. Usually we say “function
|
---|
70 | parameter” or “parameter” for short, not mentioning the fact that
|
---|
71 | it’s a variable.
|
---|
72 | </p>
|
---|
73 | <p>We declare these variables in the beginning of the function
|
---|
74 | definition, in the <em>parameter list</em>. For example,
|
---|
75 | </p>
|
---|
76 | <div class="example">
|
---|
77 | <pre class="example">fib (int n)
|
---|
78 | </pre></div>
|
---|
79 |
|
---|
80 | <p>has a parameter list with one function parameter <code>n</code>, which has
|
---|
81 | type <code>int</code>.
|
---|
82 | </p>
|
---|
83 | <p>Function parameter declarations differ from ordinary variable
|
---|
84 | declarations in several ways:
|
---|
85 | </p>
|
---|
86 | <ul>
|
---|
87 | <li> Inside the function definition header, commas separate parameter
|
---|
88 | declarations, and each parameter needs a complete declaration
|
---|
89 | including the type. For instance, if a function <code>foo</code> has two
|
---|
90 | <code>int</code> parameters, write this:
|
---|
91 |
|
---|
92 | <div class="example">
|
---|
93 | <pre class="example">foo (int a, int b)
|
---|
94 | </pre></div>
|
---|
95 |
|
---|
96 | <p>You can’t share the common <code>int</code> between the two declarations:
|
---|
97 | </p>
|
---|
98 | <div class="example">
|
---|
99 | <pre class="example">foo (int a, b) /* <span class="roman">Invalid!</span> */
|
---|
100 | </pre></div>
|
---|
101 |
|
---|
102 | </li><li> A function parameter variable is initialized to whatever value is
|
---|
103 | passed in the function call, so its declaration cannot specify an
|
---|
104 | initial value.
|
---|
105 |
|
---|
106 | </li><li> Writing an array type in a function parameter declaration has the
|
---|
107 | effect of declaring it as a pointer. The size specified for the array
|
---|
108 | has no effect at all, and we normally omit the size. Thus,
|
---|
109 |
|
---|
110 | <div class="example">
|
---|
111 | <pre class="example">foo (int a[5])
|
---|
112 | foo (int a[])
|
---|
113 | foo (int *a)
|
---|
114 | </pre></div>
|
---|
115 |
|
---|
116 | <p>are equivalent.
|
---|
117 | </p>
|
---|
118 | </li><li> The scope of the parameter variables is the entire function body,
|
---|
119 | notwithstanding the fact that they are written in the function header,
|
---|
120 | which is just outside the function body.
|
---|
121 | </li></ul>
|
---|
122 |
|
---|
123 | <p>If a function has no parameters, it would be most natural for the
|
---|
124 | list of parameters in its definition to be empty. But that, in C, has
|
---|
125 | a special meaning for historical reasons: “Do not check that calls to
|
---|
126 | this function have the right number of arguments.” Thus,
|
---|
127 | </p>
|
---|
128 | <div class="example">
|
---|
129 | <pre class="example">int
|
---|
130 | foo ()
|
---|
131 | {
|
---|
132 | return 5;
|
---|
133 | }
|
---|
134 |
|
---|
135 | int
|
---|
136 | bar (int x)
|
---|
137 | {
|
---|
138 | return foo (x);
|
---|
139 | }
|
---|
140 | </pre></div>
|
---|
141 |
|
---|
142 | <p>would not report a compilation error in passing <code>x</code> as an
|
---|
143 | argument to <code>foo</code>. By contrast,
|
---|
144 | </p>
|
---|
145 | <div class="example">
|
---|
146 | <pre class="example">int
|
---|
147 | foo (void)
|
---|
148 | {
|
---|
149 | return 5;
|
---|
150 | }
|
---|
151 |
|
---|
152 | int
|
---|
153 | bar (int x)
|
---|
154 | {
|
---|
155 | return foo (x);
|
---|
156 | }
|
---|
157 | </pre></div>
|
---|
158 |
|
---|
159 | <p>would report an error because <code>foo</code> is supposed to receive
|
---|
160 | no arguments.
|
---|
161 | </p>
|
---|
162 | <hr>
|
---|
163 | <div class="header">
|
---|
164 | <p>
|
---|
165 | Next: <a href="Forward-Function-Declarations.html" accesskey="n" rel="next">Forward Function Declarations</a>, Up: <a href="Function-Definitions.html" accesskey="u" rel="up">Function Definitions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
166 | </div>
|
---|
167 |
|
---|
168 |
|
---|
169 |
|
---|
170 | </body>
|
---|
171 | </html>
|
---|