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 Declarations (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Function Declarations (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Function Declarations (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="Functions.html" rel="up" title="Functions">
|
---|
33 | <link href="Function-Calls.html" rel="next" title="Function Calls">
|
---|
34 | <link href="Structs-as-Parameters.html" rel="prev" title="Structs as Parameters">
|
---|
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-Declarations"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Function-Calls.html" accesskey="n" rel="next">Function Calls</a>, Previous: <a href="Function-Definitions.html" accesskey="p" rel="prev">Function Definitions</a>, Up: <a href="Functions.html" accesskey="u" rel="up">Functions</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-Declarations-1"></span><h3 class="section">22.2 Function Declarations</h3>
|
---|
64 | <span id="index-function-declarations"></span>
|
---|
65 | <span id="index-declararing-functions"></span>
|
---|
66 |
|
---|
67 | <p>To call a function, or use its name as a pointer, a <em>function
|
---|
68 | declaration</em> for the function name must be in effect at that point in
|
---|
69 | the code. The function’s definition serves as a declaration of that
|
---|
70 | function for the rest of the containing scope, but to use the function
|
---|
71 | in code before the definition, or from another compilation module, a
|
---|
72 | separate function declaration must precede the use.
|
---|
73 | </p>
|
---|
74 | <p>A function declaration looks like the start of a function definition.
|
---|
75 | It begins with the return value type (<code>void</code> if none) and the
|
---|
76 | function name, followed by argument declarations in parentheses
|
---|
77 | (though these can sometimes be omitted). But that’s as far as the
|
---|
78 | similarity goes: instead of the function body, the declaration uses a
|
---|
79 | semicolon.
|
---|
80 | </p>
|
---|
81 | <span id="index-function-prototype"></span>
|
---|
82 | <span id="index-prototype-of-a-function"></span>
|
---|
83 | <p>A declaration that specifies argument types is called a <em>function
|
---|
84 | prototype</em>. You can include the argument names or omit them. The
|
---|
85 | names, if included in the declaration, have no effect, but they may
|
---|
86 | serve as documentation.
|
---|
87 | </p>
|
---|
88 | <p>This form of prototype specifies fixed argument types:
|
---|
89 | </p>
|
---|
90 | <div class="example">
|
---|
91 | <pre class="example"><var>rettype</var> <var>function</var> (<var>argtypes</var><span class="roman">…</span>);
|
---|
92 | </pre></div>
|
---|
93 |
|
---|
94 | <p>This form says the function takes no arguments:
|
---|
95 | </p>
|
---|
96 | <div class="example">
|
---|
97 | <pre class="example"><var>rettype</var> <var>function</var> (void);
|
---|
98 | </pre></div>
|
---|
99 |
|
---|
100 | <p>This form declares types for some arguments, and allows additional
|
---|
101 | arguments whose types are not specified:
|
---|
102 | </p>
|
---|
103 | <div class="example">
|
---|
104 | <pre class="example"><var>rettype</var> <var>function</var> (<var>argtypes</var><span class="roman">…</span>, ...);
|
---|
105 | </pre></div>
|
---|
106 |
|
---|
107 | <p>For a parameter that’s an array of variable length, you can write
|
---|
108 | its declaration with ‘<samp>*</samp>’ where the “length” of the array would
|
---|
109 | normally go; for example, these are all equivalent.
|
---|
110 | </p>
|
---|
111 | <div class="example">
|
---|
112 | <pre class="example">double maximum (int n, int m, double a[n][m]);
|
---|
113 | double maximum (int n, int m, double a[*][*]);
|
---|
114 | double maximum (int n, int m, double a[ ][*]);
|
---|
115 | double maximum (int n, int m, double a[ ][m]);
|
---|
116 | </pre></div>
|
---|
117 |
|
---|
118 | <p>The old-fashioned form of declaration, which is not a prototype, says
|
---|
119 | nothing about the types of arguments or how many they should be:
|
---|
120 | </p>
|
---|
121 | <div class="example">
|
---|
122 | <pre class="example"><var>rettype</var> <var>function</var> ();
|
---|
123 | </pre></div>
|
---|
124 |
|
---|
125 | <p><strong>Warning:</strong> Arguments passed to a function declared without a
|
---|
126 | prototype are converted with the default argument promotions
|
---|
127 | (see <a href="Argument-Promotions.html">Argument Promotions</a>. Likewise for additional arguments whose
|
---|
128 | types are unspecified.
|
---|
129 | </p>
|
---|
130 | <p>Function declarations are usually written at the top level in a source file,
|
---|
131 | but you can also put them inside code blocks. Then the function name
|
---|
132 | is visible for the rest of the containing scope. For example:
|
---|
133 | </p>
|
---|
134 | <div class="example">
|
---|
135 | <pre class="example">void
|
---|
136 | foo (char *file_name)
|
---|
137 | {
|
---|
138 | void save_file (char *);
|
---|
139 | save_file (file_name);
|
---|
140 | }
|
---|
141 | </pre></div>
|
---|
142 |
|
---|
143 | <p>If another part of the code tries to call the function
|
---|
144 | <code>save_file</code>, this declaration won’t be in effect there. So the
|
---|
145 | function will get an implicit declaration of the form <code>extern int
|
---|
146 | save_file ();</code>. That conflicts with the explicit declaration
|
---|
147 | here, and the discrepancy generates a warning.
|
---|
148 | </p>
|
---|
149 | <p>The syntax of C traditionally allows omitting the data type in a
|
---|
150 | function declaration if it specifies a storage class or a qualifier.
|
---|
151 | Then the type defaults to <code>int</code>. For example:
|
---|
152 | </p>
|
---|
153 | <div class="example">
|
---|
154 | <pre class="example">static foo (double x);
|
---|
155 | </pre></div>
|
---|
156 |
|
---|
157 | <p>defaults the return type to <code>int</code>.
|
---|
158 | This is bad practice; if you see it, fix it.
|
---|
159 | </p>
|
---|
160 | <p>Calling a function that is undeclared has the effect of an creating
|
---|
161 | <em>implicit</em> declaration in the innermost containing scope,
|
---|
162 | equivalent to this:
|
---|
163 | </p>
|
---|
164 | <div class="example">
|
---|
165 | <pre class="example">extern int <em>function</em> ();
|
---|
166 | </pre></div>
|
---|
167 |
|
---|
168 | <p>This declaration says that the function returns <code>int</code> but leaves
|
---|
169 | its argument types unspecified. If that does not accurately fit the
|
---|
170 | function, then the program <strong>needs</strong> an explicit declaration of
|
---|
171 | the function with argument types in order to call it correctly.
|
---|
172 | </p>
|
---|
173 | <p>Implicit declarations are deprecated, and a function call that creates one
|
---|
174 | causes a warning.
|
---|
175 | </p>
|
---|
176 | <hr>
|
---|
177 | <div class="header">
|
---|
178 | <p>
|
---|
179 | Next: <a href="Function-Calls.html" accesskey="n" rel="next">Function Calls</a>, Previous: <a href="Function-Definitions.html" accesskey="p" rel="prev">Function Definitions</a>, Up: <a href="Functions.html" accesskey="u" rel="up">Functions</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>
|
---|
180 | </div>
|
---|
181 |
|
---|
182 |
|
---|
183 |
|
---|
184 | </body>
|
---|
185 | </html>
|
---|