<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.sternwarte.uni-erlangen.de/wiki/index.php?action=history&amp;feed=atom&amp;title=Function_nesting</id>
	<title>Function nesting - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.sternwarte.uni-erlangen.de/wiki/index.php?action=history&amp;feed=atom&amp;title=Function_nesting"/>
	<link rel="alternate" type="text/html" href="https://www.sternwarte.uni-erlangen.de/wiki/index.php?title=Function_nesting&amp;action=history"/>
	<updated>2026-05-18T19:56:07Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.7</generator>
	<entry>
		<id>https://www.sternwarte.uni-erlangen.de/wiki/index.php?title=Function_nesting&amp;diff=704&amp;oldid=prev</id>
		<title>Stierhof: copy from old wiki</title>
		<link rel="alternate" type="text/html" href="https://www.sternwarte.uni-erlangen.de/wiki/index.php?title=Function_nesting&amp;diff=704&amp;oldid=prev"/>
		<updated>2018-04-11T12:23:59Z</updated>

		<summary type="html">&lt;p&gt;copy from old wiki&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Function nesting (or functions define functions) is, in principle, not possible:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
define fun() {&lt;br /&gt;
  define fun2() { return 5; };&lt;br /&gt;
  return fun2();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This approach results in an error. But one can may use &amp;quot;eval&amp;quot; to define a nested function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
define fun() {&lt;br /&gt;
  eval(&amp;quot;define fun2() { return 5; };&amp;quot;);&lt;br /&gt;
  return eval(&amp;quot;fun2()&amp;quot;);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nested function can also be made ''private''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In that way, functions can be defined dynamically during runtime.&lt;br /&gt;
'''However''', there are some '''disadvantages''' one should be aware of:&lt;br /&gt;
* The &amp;quot;eval&amp;quot;uated string is parsed by the S-lang interpreter only during the call of eval. That means syntax errors are '''not''' detected beforehand, which could unnecessarily crash your script!&lt;br /&gt;
* Never ever pass external input into eval-statements -- at least not directly, but best not at all! If not handled properly, malicious code can be '''injected''' into your process or script, which is a known computer '''attack''' (see also this [http://en.wikipedia.org/wiki/Code_injection article])&lt;br /&gt;
&lt;br /&gt;
Note that the function defined in the eval-Statement is by no means &amp;quot;nested&amp;quot; (in the sense of: &amp;quot;internal to the function&amp;quot;), as the following example illustrates:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
define f(x)&lt;br /&gt;
{&lt;br /&gt;
  if(x == 0)&lt;br /&gt;
  {&lt;br /&gt;
    eval(&amp;quot;define nested() { return 42; }&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  return eval(&amp;quot;nested()&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
define g()&lt;br /&gt;
{&lt;br /&gt;
  eval(&amp;quot;define nested() { return `f's 'nested' function was hijacked by g`; }&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
variable x;&lt;br /&gt;
_for x (0, 2) vmessage(&amp;quot;f(%d) = %S&amp;quot;, x, f(x));&lt;br /&gt;
g();&lt;br /&gt;
_for x (3, 5) vmessage(&amp;quot;f(%d) = %S&amp;quot;, x, f(x));&lt;br /&gt;
define nested() { throw DataError; }&lt;br /&gt;
_for x (6, 9) vmessage(&amp;quot;f(%d) = %S&amp;quot;, x, f(x));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Isis / Slang]]&lt;/div&gt;</summary>
		<author><name>Stierhof</name></author>
	</entry>
</feed>