1<html lang="en"> 2<head> 3<title>Nested Functions - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions"> 9<link rel="prev" href="Labels-as-Values.html#Labels-as-Values" title="Labels as Values"> 10<link rel="next" href="Constructing-Calls.html#Constructing-Calls" title="Constructing Calls"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Nested-Functions"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Constructing-Calls.html#Constructing-Calls">Constructing Calls</a>, 53Previous: <a rel="previous" accesskey="p" href="Labels-as-Values.html#Labels-as-Values">Labels as Values</a>, 54Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a> 55<hr> 56</div> 57 58<h3 class="section">6.4 Nested Functions</h3> 59 60<p><a name="index-nested-functions-2250"></a><a name="index-downward-funargs-2251"></a><a name="index-thunks-2252"></a> 61A <dfn>nested function</dfn> is a function defined inside another function. 62(Nested functions are not supported for GNU C++.) The nested function's 63name is local to the block where it is defined. For example, here we 64define a nested function named <code>square</code>, and call it twice: 65 66<pre class="smallexample"> foo (double a, double b) 67 { 68 double square (double z) { return z * z; } 69 70 return square (a) + square (b); 71 } 72</pre> 73 <p>The nested function can access all the variables of the containing 74function that are visible at the point of its definition. This is 75called <dfn>lexical scoping</dfn>. For example, here we show a nested 76function which uses an inherited variable named <code>offset</code>: 77 78<pre class="smallexample"> bar (int *array, int offset, int size) 79 { 80 int access (int *array, int index) 81 { return array[index + offset]; } 82 int i; 83 /* <span class="roman">...</span> */ 84 for (i = 0; i < size; i++) 85 /* <span class="roman">...</span> */ access (array, i) /* <span class="roman">...</span> */ 86 } 87</pre> 88 <p>Nested function definitions are permitted within functions in the places 89where variable definitions are allowed; that is, in any block, mixed 90with the other declarations and statements in the block. 91 92 <p>It is possible to call the nested function from outside the scope of its 93name by storing its address or passing the address to another function: 94 95<pre class="smallexample"> hack (int *array, int size) 96 { 97 void store (int index, int value) 98 { array[index] = value; } 99 100 intermediate (store, size); 101 } 102</pre> 103 <p>Here, the function <code>intermediate</code> receives the address of 104<code>store</code> as an argument. If <code>intermediate</code> calls <code>store</code>, 105the arguments given to <code>store</code> are used to store into <code>array</code>. 106But this technique works only so long as the containing function 107(<code>hack</code>, in this example) does not exit. 108 109 <p>If you try to call the nested function through its address after the 110containing function has exited, all hell will break loose. If you try 111to call it after a containing scope level has exited, and if it refers 112to some of the variables that are no longer in scope, you may be lucky, 113but it's not wise to take the risk. If, however, the nested function 114does not refer to anything that has gone out of scope, you should be 115safe. 116 117 <p>GCC implements taking the address of a nested function using a technique 118called <dfn>trampolines</dfn>. This technique was described in 119<cite>Lexical Closures for C++</cite> (Thomas M. Breuel, USENIX 120C++ Conference Proceedings, October 17-21, 1988). 121 122 <p>A nested function can jump to a label inherited from a containing 123function, provided the label was explicitly declared in the containing 124function (see <a href="Local-Labels.html#Local-Labels">Local Labels</a>). Such a jump returns instantly to the 125containing function, exiting the nested function which did the 126<code>goto</code> and any intermediate functions as well. Here is an example: 127 128<pre class="smallexample"> bar (int *array, int offset, int size) 129 { 130 __label__ failure; 131 int access (int *array, int index) 132 { 133 if (index > size) 134 goto failure; 135 return array[index + offset]; 136 } 137 int i; 138 /* <span class="roman">...</span> */ 139 for (i = 0; i < size; i++) 140 /* <span class="roman">...</span> */ access (array, i) /* <span class="roman">...</span> */ 141 /* <span class="roman">...</span> */ 142 return 0; 143 144 /* <span class="roman">Control comes here from </span><code>access</code><span class="roman"> 145 if it detects an error.</span> */ 146 failure: 147 return -1; 148 } 149</pre> 150 <p>A nested function always has no linkage. Declaring one with 151<code>extern</code> or <code>static</code> is erroneous. If you need to declare the nested function 152before its definition, use <code>auto</code> (which is otherwise meaningless 153for function declarations). 154 155<pre class="smallexample"> bar (int *array, int offset, int size) 156 { 157 __label__ failure; 158 auto int access (int *, int); 159 /* <span class="roman">...</span> */ 160 int access (int *array, int index) 161 { 162 if (index > size) 163 goto failure; 164 return array[index + offset]; 165 } 166 /* <span class="roman">...</span> */ 167 } 168</pre> 169 </body></html> 170 171