1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Nested Functions (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Nested Functions (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Nested Functions (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Function-Files.html" rel="up" title="Function Files">
<link href="Overloading-and-Autoloading.html" rel="next" title="Overloading and Autoloading">
<link href="Private-Functions.html" rel="prev" title="Private Functions">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Nested-Functions">
<div class="nav-panel">
<p>
Next: <a href="Overloading-and-Autoloading.html" accesskey="n" rel="next">Overloading and Autoloading</a>, Previous: <a href="Private-Functions.html" accesskey="p" rel="prev">Private Functions</a>, Up: <a href="Function-Files.html" accesskey="u" rel="up">Function Files</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Nested-Functions-1"><span>11.10.4 Nested Functions<a class="copiable-link" href="#Nested-Functions-1"> ¶</a></span></h4>
<p>Nested functions are similar to subfunctions in that only the main function is
visible outside the file. However, they also allow for child functions to
access the local variables in their parent function. This shared access mimics
using a global variable to share information — but a global variable which is
not visible to the rest of Octave. As a programming strategy, sharing data
this way can create code which is difficult to maintain. It is recommended to
use subfunctions in place of nested functions when possible.
</p>
<p>As a simple example, consider a parent function <code class="code">foo</code>, that calls a nested
child function <code class="code">bar</code>, with a shared variable <var class="var">x</var>.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">function y = foo ()
x = 10;
bar ();
y = x;
function bar ()
x = 20;
endfunction
endfunction
foo ()
⇒ 20
</pre></div></div>
<p>Notice that there is no special syntax for sharing <var class="var">x</var>. This can lead to
problems with accidental variable sharing between a parent function and its
child. While normally variables are inherited, child function parameters and
return values are local to the child function.
</p>
<p>Now consider the function <code class="code">foobar</code> that uses variables <var class="var">x</var> and
<var class="var">y</var>. <code class="code">foobar</code> calls a nested function <code class="code">foo</code> which takes
<var class="var">x</var> as a parameter and returns <var class="var">y</var>. <code class="code">foo</code> then calls <code class="code">bat</code>
which does some computation.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">function z = foobar ()
x = 0;
y = 0;
z = foo (5);
z += x + y;
function y = foo (x)
y = x + bat ();
function z = bat ()
z = x;
endfunction
endfunction
endfunction
foobar ()
⇒ 10
</pre></div></div>
<p>It is important to note that the <var class="var">x</var> and <var class="var">y</var> in <code class="code">foobar</code> remain
zero, as in <code class="code">foo</code> they are a return value and parameter respectively. The
<var class="var">x</var> in <code class="code">bat</code> refers to the <var class="var">x</var> in <code class="code">foo</code>.
</p>
<p>Variable inheritance leads to a problem for <code class="code">eval</code> and scripts. If a
new variable is created in a parent function, it is not clear what should
happen in nested child functions. For example, consider a parent function
<code class="code">foo</code> with a nested child function <code class="code">bar</code>:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">function y = foo (to_eval)
bar ();
eval (to_eval);
function bar ()
eval ("x = 100;");
eval ("y = x;");
endfunction
endfunction
foo ("x = 5;")
⇒ error: can not add variable "x" to a static workspace
foo ("y = 10;")
⇒ 10
foo ("")
⇒ 100
</pre></div></div>
<p>The parent function <code class="code">foo</code> is unable to create a new variable
<var class="var">x</var>, but the child function <code class="code">bar</code> was successful. Furthermore, even
in an <code class="code">eval</code> statement <var class="var">y</var> in <code class="code">bar</code> is the same <var class="var">y</var> as in its
parent function <code class="code">foo</code>. The use of <code class="code">eval</code> in conjunction with nested
functions is best avoided.
</p>
<p>As with subfunctions, only the first nested function in a file may be called
from the outside. Inside a function the rules are more complicated. In
general a nested function may call:
</p>
<ol class="enumerate" start="0">
<li> Globally visible functions
</li><li> Any function that the nested function’s parent can call
</li><li> Sibling functions (functions that have the same parents)
</li><li> Direct children
</li></ol>
<p>As a complex example consider a parent function <code class="code">ex_top</code> with two
child functions, <code class="code">ex_a</code> and <code class="code">ex_b</code>. In addition, <code class="code">ex_a</code> has two
more child functions, <code class="code">ex_aa</code> and <code class="code">ex_ab</code>. For example:
</p>
<div class="example">
<pre class="example-preformatted">function ex_top ()
## Can call: ex_top, ex_a, and ex_b
## Can NOT call: ex_aa and ex_ab
function ex_a ()
## Can call everything
function ex_aa ()
## Can call everything
endfunction
function ex_ab ()
## Can call everything
endfunction
endfunction
function ex_b ()
## Can call: ex_top, ex_a, and ex_b
## Can NOT call: ex_aa and ex_ab
endfunction
endfunction
</pre></div>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Overloading-and-Autoloading.html">Overloading and Autoloading</a>, Previous: <a href="Private-Functions.html">Private Functions</a>, Up: <a href="Function-Files.html">Function Files</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|