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>Nested Functions (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Nested Functions (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Nested Functions (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="Advanced-Definitions.html" rel="up" title="Advanced Definitions">
|
---|
33 | <link href="Inline-Function-Definitions.html" rel="next" title="Inline Function Definitions">
|
---|
34 | <link href="Variable-Number-of-Arguments.html" rel="prev" title="Variable Number of Arguments">
|
---|
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="Nested-Functions"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Inline-Function-Definitions.html" accesskey="n" rel="next">Inline Function Definitions</a>, Previous: <a href="Variable-Number-of-Arguments.html" accesskey="p" rel="prev">Variable Number of Arguments</a>, Up: <a href="Advanced-Definitions.html" accesskey="u" rel="up">Advanced 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="Nested-Functions-1"></span><h4 class="subsection">22.7.3 Nested Functions</h4>
|
---|
64 | <span id="index-nested-functions"></span>
|
---|
65 | <span id="index-functions_002c-nested"></span>
|
---|
66 | <span id="index-downward-funargs"></span>
|
---|
67 | <span id="index-thunks"></span>
|
---|
68 |
|
---|
69 | <p>A <em>nested function</em> is a function defined inside another function.
|
---|
70 | The nested function’s name is local to the block where it is defined.
|
---|
71 | For example, here we define a nested function named <code>square</code>, and
|
---|
72 | call it twice:
|
---|
73 | </p>
|
---|
74 | <div class="example">
|
---|
75 | <pre class="example">foo (double a, double b)
|
---|
76 | {
|
---|
77 | double square (double z) { return z * z; }
|
---|
78 |
|
---|
79 | return square (a) + square (b);
|
---|
80 | }
|
---|
81 | </pre></div>
|
---|
82 |
|
---|
83 | <p>The nested function can access all the variables of the containing
|
---|
84 | function that are visible at the point of its definition. This is
|
---|
85 | called <em>lexical scoping</em>. For example, here we show a nested
|
---|
86 | function that uses an inherited variable named <code>offset</code>:
|
---|
87 | </p>
|
---|
88 | <div class="example">
|
---|
89 | <pre class="example">bar (int *array, int offset, int size)
|
---|
90 | {
|
---|
91 | int access (int *array, int index)
|
---|
92 | { return array[index + offset]; }
|
---|
93 | int i;
|
---|
94 | <span class="roman">…</span>
|
---|
95 | for (i = 0; i < size; i++)
|
---|
96 | <span class="roman">…</span> access (array, i) <span class="roman">…</span>
|
---|
97 | }
|
---|
98 | </pre></div>
|
---|
99 |
|
---|
100 | <p>Nested function definitions can appear wherever automatic variable
|
---|
101 | declarations are allowed; that is, in any block, interspersed with the
|
---|
102 | other declarations and statements in the block.
|
---|
103 | </p>
|
---|
104 | <p>The nested function’s name is visible only within the parent block;
|
---|
105 | the name’s scope starts from its definition and continues to the end
|
---|
106 | of the containing block. If the nested function’s name
|
---|
107 | is the same as the parent function’s name, there wil be
|
---|
108 | no way to refer to the parent function inside the scope of the
|
---|
109 | name of the nested function.
|
---|
110 | </p>
|
---|
111 | <p>Using <code>extern</code> or <code>static</code> on a nested function definition
|
---|
112 | is an error.
|
---|
113 | </p>
|
---|
114 | <p>It is possible to call the nested function from outside the scope of its
|
---|
115 | name by storing its address or passing the address to another function.
|
---|
116 | You can do this safely, but you must be careful:
|
---|
117 | </p>
|
---|
118 | <div class="example">
|
---|
119 | <pre class="example">hack (int *array, int size, int addition)
|
---|
120 | {
|
---|
121 | void store (int index, int value)
|
---|
122 | { array[index] = value + addition; }
|
---|
123 |
|
---|
124 | intermediate (store, size);
|
---|
125 | }
|
---|
126 | </pre></div>
|
---|
127 |
|
---|
128 | <p>Here, the function <code>intermediate</code> receives the address of
|
---|
129 | <code>store</code> as an argument. If <code>intermediate</code> calls <code>store</code>,
|
---|
130 | the arguments given to <code>store</code> are used to store into <code>array</code>.
|
---|
131 | <code>store</code> also accesses <code>hack</code>’s local variable <code>addition</code>.
|
---|
132 | </p>
|
---|
133 | <p>It is safe for <code>intermediate</code> to call <code>store</code> because
|
---|
134 | <code>hack</code>’s stack frame, with its arguments and local variables,
|
---|
135 | continues to exist during the call to <code>intermediate</code>.
|
---|
136 | </p>
|
---|
137 | <p>Calling the nested function through its address after the containing
|
---|
138 | function has exited is asking for trouble. If it is called after a
|
---|
139 | containing scope level has exited, and if it refers to some of the
|
---|
140 | variables that are no longer in scope, it will refer to memory
|
---|
141 | containing junk or other data. It’s not wise to take the risk.
|
---|
142 | </p>
|
---|
143 | <p>The GNU C Compiler implements taking the address of a nested function
|
---|
144 | using a technique called <em>trampolines</em>. This technique was
|
---|
145 | described in <cite>Lexical Closures for C<tt>++</tt></cite> (Thomas M. Breuel,
|
---|
146 | USENIX C<tt>++</tt> Conference Proceedings, October 17–21, 1988).
|
---|
147 | </p>
|
---|
148 | <p>A nested function can jump to a label inherited from a containing
|
---|
149 | function, provided the label was explicitly declared in the containing
|
---|
150 | function (see <a href="Local-Labels.html">Local Labels</a>). Such a jump returns instantly to the
|
---|
151 | containing function, exiting the nested function that did the
|
---|
152 | <code>goto</code> and any intermediate function invocations as well. Here
|
---|
153 | is an example:
|
---|
154 | </p>
|
---|
155 | <div class="example">
|
---|
156 | <pre class="example">bar (int *array, int offset, int size)
|
---|
157 | {
|
---|
158 | /* <span class="roman">Explicitly declare the label <code>failure</code>.</span> */
|
---|
159 | __label__ failure;
|
---|
160 | int access (int *array, int index)
|
---|
161 | {
|
---|
162 | if (index > size)
|
---|
163 | /* <span class="roman">Exit this function,</span>
|
---|
164 | <span class="roman">and return to <code>bar</code>.</span> */
|
---|
165 | goto failure;
|
---|
166 | return array[index + offset];
|
---|
167 | }
|
---|
168 | </pre><pre class="example">
|
---|
169 |
|
---|
170 | </pre><pre class="example"> int i;
|
---|
171 | <span class="roman">…</span>
|
---|
172 | for (i = 0; i < size; i++)
|
---|
173 | <span class="roman">…</span> access (array, i) <span class="roman">…</span>
|
---|
174 | <span class="roman">…</span>
|
---|
175 | return 0;
|
---|
176 |
|
---|
177 | /* <span class="roman">Control comes here from <code>access</code>
|
---|
178 | if it does the <code>goto</code>.</span> */
|
---|
179 | failure:
|
---|
180 | return -1;
|
---|
181 | }
|
---|
182 | </pre></div>
|
---|
183 |
|
---|
184 | <p>To declare the nested function before its definition, use
|
---|
185 | <code>auto</code> (which is otherwise meaningless for function declarations;
|
---|
186 | see <a href="auto-and-register.html">auto and register</a>). For example,
|
---|
187 | </p>
|
---|
188 | <div class="example">
|
---|
189 | <pre class="example">bar (int *array, int offset, int size)
|
---|
190 | {
|
---|
191 | auto int access (int *, int);
|
---|
192 | <span class="roman">…</span>
|
---|
193 | <span class="roman">…</span> access (array, i) <span class="roman">…</span>
|
---|
194 | <span class="roman">…</span>
|
---|
195 | int access (int *array, int index)
|
---|
196 | {
|
---|
197 | <span class="roman">…</span>
|
---|
198 | }
|
---|
199 | <span class="roman">…</span>
|
---|
200 | }
|
---|
201 | </pre></div>
|
---|
202 |
|
---|
203 | <hr>
|
---|
204 | <div class="header">
|
---|
205 | <p>
|
---|
206 | Next: <a href="Inline-Function-Definitions.html" accesskey="n" rel="next">Inline Function Definitions</a>, Previous: <a href="Variable-Number-of-Arguments.html" accesskey="p" rel="prev">Variable Number of Arguments</a>, Up: <a href="Advanced-Definitions.html" accesskey="u" rel="up">Advanced 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>
|
---|
207 | </div>
|
---|
208 |
|
---|
209 |
|
---|
210 |
|
---|
211 | </body>
|
---|
212 | </html>
|
---|