source: public/doc/gnu-c/Function-Declarations.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: 7.9 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 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<!--
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-Declarations"></span><div class="header">
59<p>
60Next: <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> &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-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
68declaration</em> for the function name must be in effect at that point in
69the code. The function&rsquo;s definition serves as a declaration of that
70function for the rest of the containing scope, but to use the function
71in code before the definition, or from another compilation module, a
72separate function declaration must precede the use.
73</p>
74<p>A function declaration looks like the start of a function definition.
75It begins with the return value type (<code>void</code> if none) and the
76function name, followed by argument declarations in parentheses
77(though these can sometimes be omitted). But that&rsquo;s as far as the
78similarity goes: instead of the function body, the declaration uses a
79semicolon.
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
84prototype</em>. You can include the argument names or omit them. The
85names, if included in the declaration, have no effect, but they may
86serve 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">&hellip;</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
101arguments 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">&hellip;</span>, ...);
105</pre></div>
106
107<p>For a parameter that&rsquo;s an array of variable length, you can write
108its declaration with &lsquo;<samp>*</samp>&rsquo; where the &ldquo;length&rdquo; of the array would
109normally 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]);
113double maximum (int n, int m, double a[*][*]);
114double maximum (int n, int m, double a[ ][*]);
115double 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
119nothing 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
126prototype are converted with the default argument promotions
127(see <a href="Argument-Promotions.html">Argument Promotions</a>. Likewise for additional arguments whose
128types are unspecified.
129</p>
130<p>Function declarations are usually written at the top level in a source file,
131but you can also put them inside code blocks. Then the function name
132is visible for the rest of the containing scope. For example:
133</p>
134<div class="example">
135<pre class="example">void
136foo (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&rsquo;t be in effect there. So the
145function will get an implicit declaration of the form <code>extern int
146save_file ();</code>. That conflicts with the explicit declaration
147here, and the discrepancy generates a warning.
148</p>
149<p>The syntax of C traditionally allows omitting the data type in a
150function declaration if it specifies a storage class or a qualifier.
151Then 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>.
158This 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,
162equivalent 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
169its argument types unspecified. If that does not accurately fit the
170function, then the program <strong>needs</strong> an explicit declaration of
171the 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
174causes a warning.
175</p>
176<hr>
177<div class="header">
178<p>
179Next: <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> &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>
180</div>
181
182
183
184</body>
185</html>
Note: See TracBrowser for help on using the repository browser.