source: public/doc/gnu-c/Function-Parameter-Variables.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 6.2 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
69argument in a call to the function. Usually we say &ldquo;function
70parameter&rdquo; or &ldquo;parameter&rdquo; for short, not mentioning the fact that
71it&rsquo;s a variable.
72</p>
73<p>We declare these variables in the beginning of the function
74definition, 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
81type <code>int</code>.
82</p>
83<p>Function parameter declarations differ from ordinary variable
84declarations in several ways:
85</p>
86<ul>
87<li> Inside the function definition header, commas separate parameter
88declarations, and each parameter needs a complete declaration
89including 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&rsquo;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
103passed in the function call, so its declaration cannot specify an
104initial value.
105
106</li><li> Writing an array type in a function parameter declaration has the
107effect of declaring it as a pointer. The size specified for the array
108has no effect at all, and we normally omit the size. Thus,
109
110<div class="example">
111<pre class="example">foo (int a[5])
112foo (int a[])
113foo (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,
119notwithstanding the fact that they are written in the function header,
120which 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
124list of parameters in its definition to be empty. But that, in C, has
125a special meaning for historical reasons: &ldquo;Do not check that calls to
126this function have the right number of arguments.&rdquo; Thus,
127</p>
128<div class="example">
129<pre class="example">int
130foo ()
131{
132 return 5;
133}
134
135int
136bar (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
143argument to <code>foo</code>. By contrast,
144</p>
145<div class="example">
146<pre class="example">int
147foo (void)
148{
149 return 5;
150}
151
152int
153bar (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
160no arguments.
161</p>
162<hr>
163<div class="header">
164<p>
165Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.