metanohi/site/projects/enigma/doc/enigma-0.1-html/Functions.html

98 lines
4.1 KiB
HTML
Raw Normal View History

<html lang="en">
<head>
<title>Functions - Enigma</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Enigma">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Language.html#Language" title="Language">
<link rel="prev" href="Files.html#Files" title="Files">
<link rel="next" href="Conditionals.html#Conditionals" title="Conditionals">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This manual is for Enigma, version 0.1.
Copyright (C) 2010 Niels Serup
Permission is granted to copy, distribute and/or modify this
document under the terms of the GNU Free Documentation License,
Version 1.3 or any later version published by the Free Software
Foundation; with no Invariant Sections, no Front-Cover Texts, and
no Back-Cover Texts. A copy of the license is included in the
section entitled "GNU Free Documentation License".
This document is also available under the terms of the Creative
Commons Attribution-Share Alike 3.0 (or any later version)
Unported license. A copy of the license is available at
`http://creativecommons.org/licenses/by-sa/3.0/legalcode'.
-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<a name="Functions"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Conditionals.html#Conditionals">Conditionals</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Files.html#Files">Files</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Language.html#Language">Language</a>
<hr>
</div>
<h3 class="section">2.7 Functions</h3>
<p><a name="index-functions-14"></a>Functions are defined and called using the following syntax:
<pre class="verbatim"># Defining them
{stdout "Hello" ! write;} = func;
{/x,y/stdout "x=" x ", y=" y!write;} = coorprint;
{stdout args ! write;} = aprint;
# Calling them
!func; # Outputs "Hello"
52 12 ! coorprint; # Outputs "x=52, y=12"
999 "abc" 21 ! aprint; # Outputs "999 abc 21"
</pre>
<p>Arguments can thus be accessed either by using the <code>/.../</code> syntax
in the beginning of the function or by using the local <code>args</code>
variable. Furthermore, to make <em>all</em> new variables save in the
global space, prepend the function content with a <code>*</code>. This will
have the same effect as replacing all <code>=</code> signs in the function
with the <code>*</code> operator.
<p>Remember that your file is also a function, just without the curly
brackets. This means that when working on the top level of your program,
<code>args</code> actually refer to the arguments specified on the command
line. All arguments are strings, but built-in functions that can convert
them to numbers exist.
<p>The previous example functions did not have any return values. This is
merely because they didn't need such things. To define a return value,
the return value must be sent to the special variable <code>return</code>.
<pre class="verbatim">{/a,b/a b ! multiply = return;} = 2mul;
33 3 ! 2mul = 2mult;
stdout 2mult ! write;
</pre>
The execution of a function does <em>not</em> stop when a value
is assigned to <code>return</code>. This has both advantages and
disadvantages.
<p>Many things are almost impossible to do without the built-in
functions. Some are completely impossible. See <a href="Built_002dIn.html#Built_002dIn">Built-In</a>. As all
objects in Enigma are pointers, several built-in functions actually
modify the objects sent to them as arguments. To solve this problem, one
must use the <code>clone</code> function.
</body></html>