/usr/share/doc/octave/octave.html/Nested-Functions.html is in octave-doc 4.2.2-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Nested Functions (GNU Octave)</title>
<meta name="description" content="Nested Functions (GNU Octave)">
<meta name="keywords" content="Nested Functions (GNU Octave)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Function-Files.html#Function-Files" rel="up" title="Function Files">
<link href="Overloading-and-Autoloading.html#Overloading-and-Autoloading" rel="next" title="Overloading and Autoloading">
<link href="Private-Functions.html#Private-Functions" rel="prev" title="Private Functions">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<a name="Nested-Functions"></a>
<div class="header">
<p>
Next: <a href="Overloading-and-Autoloading.html#Overloading-and-Autoloading" accesskey="n" rel="next">Overloading and Autoloading</a>, Previous: <a href="Private-Functions.html#Private-Functions" accesskey="p" rel="prev">Private Functions</a>, Up: <a href="Function-Files.html#Function-Files" 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#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Nested-Functions-1"></a>
<h4 class="subsection">11.9.4 Nested Functions</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>foo</code>, that calls a nested
child function <code>bar</code>, with a shared variable <var>x</var>.
</p>
<div class="example">
<pre class="example">function y = foo ()
x = 10;
bar ();
y = x;
function bar ()
x = 20;
endfunction
endfunction
foo ()
⇒ 20
</pre></div>
<p>Notice that there is no special syntax for sharing <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>foobar</code> that uses variables <var>x</var> and
<var>y</var>. <code>foobar</code> calls a nested function <code>foo</code> which takes
<var>x</var> as a parameter and returns <var>y</var>. <code>foo</code> then calls <code>bat</code>
which does some computation.
</p>
<div class="example">
<pre class="example">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>
<p>It is important to note that the <var>x</var> and <var>y</var> in <code>foobar</code> remain
zero, as in <code>foo</code> they are a return value and parameter respectively. The
<var>x</var> in <code>bat</code> refers to the <var>x</var> in <code>foo</code>.
</p>
<p>Variable inheritance leads to a problem for <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>foo</code> with a nested child function <code>bar</code>:
</p>
<div class="example">
<pre class="example">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>
<p>The parent function <code>foo</code> is unable to create a new variable
<var>x</var>, but the child function <code>bar</code> was successful. Furthermore, even
in an <code>eval</code> statement <var>y</var> in <code>bar</code> is the same <var>y</var> as in its
parent function <code>foo</code>. The use of <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 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>ex_top</code> with two
child functions, <code>ex_a</code> and <code>ex_b</code>. In addition, <code>ex_a</code> has two
more child functions, <code>ex_aa</code> and <code>ex_ab</code>. For example:
</p>
<div class="example">
<pre class="example">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>
<hr>
<div class="header">
<p>
Next: <a href="Overloading-and-Autoloading.html#Overloading-and-Autoloading" accesskey="n" rel="next">Overloading and Autoloading</a>, Previous: <a href="Private-Functions.html#Private-Functions" accesskey="p" rel="prev">Private Functions</a>, Up: <a href="Function-Files.html#Function-Files" 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#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|