source: public/doc/gnu-c/Nested-Functions.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: 8.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>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<!--
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="Nested-Functions"></span><div class="header">
59<p>
60Next: <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> &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="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.
70The nested function&rsquo;s name is local to the block where it is defined.
71For example, here we define a nested function named <code>square</code>, and
72call 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
84function that are visible at the point of its definition. This is
85called <em>lexical scoping</em>. For example, here we show a nested
86function 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">&hellip;</span>
95 for (i = 0; i &lt; size; i++)
96 <span class="roman">&hellip;</span> access (array, i) <span class="roman">&hellip;</span>
97}
98</pre></div>
99
100<p>Nested function definitions can appear wherever automatic variable
101declarations are allowed; that is, in any block, interspersed with the
102other declarations and statements in the block.
103</p>
104<p>The nested function&rsquo;s name is visible only within the parent block;
105the name&rsquo;s scope starts from its definition and continues to the end
106of the containing block. If the nested function&rsquo;s name
107is the same as the parent function&rsquo;s name, there wil be
108no way to refer to the parent function inside the scope of the
109name of the nested function.
110</p>
111<p>Using <code>extern</code> or <code>static</code> on a nested function definition
112is an error.
113</p>
114<p>It is possible to call the nested function from outside the scope of its
115name by storing its address or passing the address to another function.
116You 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>,
130the arguments given to <code>store</code> are used to store into <code>array</code>.
131<code>store</code> also accesses <code>hack</code>&rsquo;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>&rsquo;s stack frame, with its arguments and local variables,
135continues to exist during the call to <code>intermediate</code>.
136</p>
137<p>Calling the nested function through its address after the containing
138function has exited is asking for trouble. If it is called after a
139containing scope level has exited, and if it refers to some of the
140variables that are no longer in scope, it will refer to memory
141containing junk or other data. It&rsquo;s not wise to take the risk.
142</p>
143<p>The GNU C Compiler implements taking the address of a nested function
144using a technique called <em>trampolines</em>. This technique was
145described in <cite>Lexical Closures for C<tt>++</tt></cite> (Thomas M. Breuel,
146USENIX C<tt>++</tt> Conference Proceedings, October 17&ndash;21, 1988).
147</p>
148<p>A nested function can jump to a label inherited from a containing
149function, provided the label was explicitly declared in the containing
150function (see <a href="Local-Labels.html">Local Labels</a>). Such a jump returns instantly to the
151containing function, exiting the nested function that did the
152<code>goto</code> and any intermediate function invocations as well. Here
153is 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 &gt; 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">&hellip;</span>
172 for (i = 0; i &lt; size; i++)
173 <span class="roman">&hellip;</span> access (array, i) <span class="roman">&hellip;</span>
174 <span class="roman">&hellip;</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;
186see <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">&hellip;</span>
193 <span class="roman">&hellip;</span> access (array, i) <span class="roman">&hellip;</span>
194 <span class="roman">&hellip;</span>
195 int access (int *array, int index)
196 {
197 <span class="roman">&hellip;</span>
198 }
199 <span class="roman">&hellip;</span>
200}
201</pre></div>
202
203<hr>
204<div class="header">
205<p>
206Next: <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> &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>
207</div>
208
209
210
211</body>
212</html>
Note: See TracBrowser for help on using the repository browser.