Warning, /education/kturtle/doc/programming-reference.docbook is written in an unsupported language. File is not indexed.

0001 <chapter id="reference">
0002 <title>&turtlescript; Programming Reference</title>
0003 <para>This is the reference for &kturtle;'s &turtlescript;. In the first section of this chapter have a look at some aspects of the <link linkend="grammar">grammar</link> of &turtlescript; programs. The second section deals exclusively with <link linkend="mathematical-operators">mathematical operators</link>, <link linkend="boolean-operators">boolean (true/false) operators</link> and <link linkend="comparing-operators">comparison operators</link>. The third section is basically a giant list of all <link linkend="commands">commands</link> explaining them one-by-one. Section four explains how to <link linkend="assignment-of-variables">assign</link> values to <link linkend="assignment-of-variables">variables</link>. Finally we explain how to arrange the execution of commands with <link linkend="controlling-execution">execution controlling statements</link> in section five and how to create you own commands with <link linkend="learn"><userinput>learn</userinput></link> in section six.</para>
0004 
0005 <sect1 id="grammar">
0006 <title>The Grammar of &turtlescript;</title>
0007 <para>As in any language, &turtlescript; has different types of words and symbols. In English we distinguish verbs (like 'to walk' or 'to sing') and nouns (like 'sister' or 'house'), they are used for different purposes. &turtlescript; is a programming language, it is used to instruct &kturtle; what to do.</para>
0008 <para>In this section some of &turtlescript;'s different types of words and symbols are briefly explained. We explain <link linkend="comment">comments</link>, <link linkend="command">commands</link> and the three different kinds of literals: <link linkend="number">numbers</link>, <link linkend="string">strings</link> and <link linkend="boolean-value">boolean (true/false) values</link>.</para>
0009 
0010 
0011 <sect2 id="comment">
0012 <title>Comments</title>
0013 <para>A program consists instructions that are executed when the program is run and so called comments. Comments are not executed, &kturtle; simply ignores them when executing your program. Comment are there for other programmers to make them understand your program better. Everything that follows on a <userinput>#</userinput> symbol is considered a comment in &turtlescript;. For example this little program that does nothing:
0014 <screen>
0015 # this little program does nothing, it is only a comment!
0016 </screen>
0017 It is a bit useless but it explain the matter well.</para>
0018 <para>Comments get very useful when the program gets a little bit more complex. It can help to give some advice to other programmers. In the following program you see comments being used together with the <link linkend="print">print</link> command.
0019 <screen>
0020 # this program has been made by Cies Breijs.
0021 print "this text will get printed on the canvas"
0022 # the previous line is not a comment, but the next line is:
0023 # print "this text will not get printed!"
0024 </screen>
0025 The first line describes the program. The second line is executed by &kturtle; and prints <userinput>this text will get printed on the canvas</userinput> on the canvas. The third line is a comment. And the forth line is a comment that contains a piece of &turtlescript;, if the <userinput>#</userinput> symbol would be removed on the fourth line the print statement will we executed by &kturtle;. Programmers say: the print statement on the fourth line is 'commented out'.</para>
0026 <para>Commented lines are <glossterm>highlighted</glossterm> with light gray in the <link linkend="the-editor">code editor</link>.</para>
0027 </sect2>
0028 
0029 <sect2 id="command">
0030 <title>Commands</title>
0031 <para>Using commands you tell the turtle or &kturtle; to do something. Some commands need input, some give output.
0032 <screen>
0033 # forward is a command that needs input, in this case the number 100:
0034 forward 100
0035 </screen>
0036 The first line is a <link linkend="comment">comment</link>. The second line contains the <userinput>forward</userinput> command and the <link linkend="number">number</link> <userinput>100</userinput>. The number is not part of command, it is considered 'input' for the command.</para>
0037 <para>Some commands like &eg; <userinput>go</userinput> need more than one input value. Multiple values have to be separated using the <userinput>,</userinput> character (comma).</para>
0038 <para> For a detailed overview of all commands that &kturtle; supports go <link linkend="commands">here</link>. Built-in commands are <glossterm>highlighted</glossterm> in dark blue.</para>
0039 </sect2>
0040 
0041 <sect2 id="number">
0042 <title>Numbers</title>
0043 <para>Most likely you already know quite a bit about numbers. The way numbers are used in &kturtle; is not much different from spoken language, or math.</para>
0044 <para>We have the so called natural numbers: <userinput>0</userinput>, <userinput>1</userinput>, <userinput>2</userinput>, <userinput>3</userinput>, <userinput>4</userinput>, <userinput>5</userinput>, &etc; The negative numbers: <userinput>-1</userinput>, <userinput>-2</userinput>, <userinput>-3</userinput>, &etc; And the numbers with decimals, or dot-numbers, for example: <userinput>0.1</userinput>, <userinput>3.14</userinput>, <userinput>33.3333</userinput>, <userinput>-5.05</userinput>, <userinput>-1.0</userinput>.
0045 The <userinput>.</userinput> character (dot) is used as decimal separator.
0046 </para>
0047 <para>Numbers can be used in <link linkend="mathematical-operators">mathematical operators</link> and <link linkend="comparing-operators">comparison operators</link>. They can also be stored in <link linkend="assignment-of-variables">variables</link>. Numbers are <glossterm>highlighted</glossterm> in dark red.</para>
0048 </sect2>
0049 
0050 <!-- constants like pi? -->
0051 
0052 <sect2 id="string">
0053 <title>Strings</title>
0054 <para>First an example:
0055 <screen>
0056 print "Hello, I'm a string."
0057 </screen>
0058 In this example <userinput>print</userinput> is a command where <userinput>"Hello, I'm a string."</userinput> is a string. Strings start and end with the <userinput>"</userinput> mark, by these marks &kturtle; knows it is a string.</para>
0059 <para>Strings can be put in <link linkend="assignment-of-variables">variables</link>, just like <link linkend="number">numbers</link>. Yet, unlike numbers, strings cannot be used in <link linkend="mathematical-operators">mathematical operators</link> or <link linkend="comparing-operators">comparison operators</link>. Strings are <glossterm>highlighted</glossterm> with red.</para>
0060 </sect2>
0061 
0062 <sect2 id="boolean-value">
0063 <title>Boolean (true/false) values</title>
0064 <para>There are only two boolean values: <userinput>true</userinput><indexterm><primary>true</primary></indexterm> and <userinput>false</userinput><indexterm><primary>false</primary></indexterm>. Sometimes they are also called: <quote><userinput>on</userinput></quote> and <quote><userinput>off</userinput></quote>, <quote><userinput>yes</userinput></quote> and <quote><userinput>no</userinput></quote>, <quote><userinput>one</userinput></quote> and <quote><userinput>zero</userinput></quote>. But in &turtlescript; we call them, always, <userinput>true</userinput> and <userinput>false</userinput>. Have a look at this piece of &turtlescript;:
0065 <screen>
0066 $a = true
0067 </screen>
0068 If you look in the <link linkend="the-inspector">inspector</link> you can see that the <link linkend="assignment-of-variables">variable</link> <userinput>$a</userinput> is set to <userinput>true</userinput>, and has the boolean type.</para>
0069 <para>Often boolean values are the result of a <link linkend="comparing-operators">comparison operator</link>, like in the following piece of &turtlescript;:
0070 <screen>
0071 $answer = 10 &gt; 3
0072 </screen>
0073 The <link linkend="assignment-of-variables">variable</link> <userinput>$answer</userinput> is set to <userinput>true</userinput> because <userinput>10</userinput> is larger than <userinput>3</userinput>.</para>
0074 <para>Boolean values, <userinput>true</userinput> and <userinput>false</userinput>, are <glossterm>highlighted</glossterm> with dark red.</para>
0075 </sect2>
0076 
0077 </sect1>
0078 
0079 
0080 
0081 <sect1 id="operators">
0082 <title>Mathematical, boolean and comparing operators</title>
0083 <para>The title of this section might sound very difficult, yet it is not as difficult as it sound.</para>
0084 
0085 <sect2 id="mathematical-operators">
0086 <title>Mathematical operators</title>
0087 <para>These are the basic math symbols known as: add (<userinput>+</userinput>), subtract (<userinput>-</userinput>), multiply (<userinput>*</userinput>), divide (<userinput>/</userinput>) and power (<userinput>^</userinput>).</para>
0088 
0089 <para>Here a small example of the mathematical operators you can use in &turtlescript;:
0090 <screen>
0091 $add      = 1 + 1
0092 $subtract = 20 - 5
0093 $multiply = 15 * 2
0094 $divide   = 30 / 30
0095 $power    = 2 ^ 2
0096 </screen>
0097 The values resulting from the mathematical operations get <link linkend="assignment-of-variables">assigned</link> to various <link linkend="assignment-of-variables">variables</link>. Using the <link linkend="the-inspector">inspector</link> you can see the values.</para>
0098 <para>If you just want a simple calculation to be done you can do something like this:
0099 <screen>
0100 print 2010-12
0101 </screen></para>
0102 <para>Now an example with parentheses:
0103 <screen>
0104 print ( ( 20 - 5 ) * 2 / 30 ) + 1
0105 </screen>
0106 The expressions inside parentheses will be calculated first. In this example, 20-5 will be calculated, then multiplied by 2, divided by 30, and then 1 is added (giving 2). Parentheses can also be used in other cases.</para>
0107 <para>&kturtle; also has more advanced mathematical features in the form of commands. Have a look at the following commands but be aware that it concerns advanced operations: <link linkend="round">round</link>, <link linkend="random">random</link>, <link linkend="sqrt">sqrt</link>
0108 <!--, <link linkend="exp">exp</link> -->
0109 , <link linkend="pi">pi</link>, <link linkend="sin">sin</link>, <link linkend="cos">cos</link>, <link linkend="tan">tan</link>, <link linkend="arcsin">arcsin</link>, <link linkend="arccos">arccos</link>, <link linkend="arctan">arctan</link>.</para>
0110 </sect2>
0111 
0112 <sect2 id="boolean-operators">
0113 <title>Boolean (true/false) operators</title>
0114 <para>Where <link linkend="mathematical-operators">mathematical operators</link> are mainly for <link linkend="number">numbers</link>, boolean operators are for <link linkend="boolean-value">boolean values</link> (<userinput>true</userinput> and <userinput>false</userinput>). There are only three boolean operators, namely: <userinput>and</userinput><indexterm><primary>and</primary></indexterm>, <userinput>or</userinput><indexterm><primary>or</primary></indexterm>, and <userinput>not</userinput><indexterm><primary>not</primary></indexterm>. The following piece of &turtlescript; shows how to use them:
0115 <screen>
0116 $and_1_1 = true and true    # -> true
0117 $and_1_0 = true and false   # -> false
0118 $and_0_1 = false and true   # -> false
0119 $and_0_0 = false and false  # -> false
0120 
0121 $or_1_1 = true or true    # -> true
0122 $or_1_0 = true or false   # -> true
0123 $or_0_1 = false or true   # -> true
0124 $or_0_0 = false or false  # -> false
0125 
0126 $not_1 = not true   # -> false
0127 $not_0 = not false  # -> true
0128 </screen>
0129 Using the <link linkend="the-inspector">inspector</link> you can see the values, yet we also supply these results as little comments at the end of the lines. <userinput>and</userinput> evaluates <userinput>true</userinput> only if both sides are <userinput>true</userinput>. <userinput>or</userinput> evaluates <userinput>true</userinput> if either side is <userinput>true</userinput>. And <userinput>not</userinput> turns a <userinput>true</userinput> into <userinput>false</userinput> and a <userinput>false</userinput> into <userinput>true</userinput>.</para>
0130 <para>Boolean operators are <glossterm>highlighted</glossterm> with pink.</para>
0131 
0132 <sect3 id="boolean-operators-advanced-examples">
0133 <title>Some more advanced examples</title>
0134 <para>Consider the following example with <userinput>and</userinput>:
0135 <screen>
0136 $a = 1
0137 $b = 5
0138 if (($a &lt; 10) and ($b == 5)) and ($a &lt; $b) {
0139   print "hello"
0140 }
0141 </screen>
0142 In this piece of &turtlescript; the result of three <link linkend="comparing-operators">comparing operators</link> are merged using <userinput>and</userinput> operators. This means that all three have to evaluate <quote>true</quote> in order for the <quote>hello</quote> to be printed.</para>
0143 
0144 <para>An example with <userinput>or</userinput>:
0145 <screen>
0146 $n = 1
0147 if ($n &lt; 10) or ($n == 2) {
0148   print "hello"
0149 }
0150 </screen>
0151 In this piece of &turtlescript; the left side of the <userinput>or</userinput> is evaluating to <quote>true</quote>, the right side to <quote>false</quote>. Since one of the two sides of the <userinput>or</userinput> operator is <quote>true</quote>, the <userinput>or</userinput> operator evaluates <quote>true</quote>. That means <quote>hello</quote> gets printed.</para>
0152 
0153 <para>And finally an example with <userinput>not</userinput> which changes <quote>true</quote> into <quote>false</quote> and <quote>false</quote> into <quote>true</quote>. Have a look:
0154 <screen>
0155 $n = 1
0156 if not ($n == 3) {
0157   print "hello"
0158 } else {
0159   print "not hello ;-)"
0160 }
0161 </screen></para>
0162 </sect3>
0163 </sect2>
0164 
0165 <sect2 id="comparing-operators">
0166 <title>Comparing operators</title>
0167 <para>Consider this simple comparison:
0168 <screen>
0169 $answer = 10 &gt; 3
0170 </screen>
0171 Here <userinput>10</userinput> is compared to <userinput>3</userinput> with the <quote>greater than</quote> operator. The result of this comparison, the <link linkend="boolean-value">boolean value</link> <userinput>true</userinput> is stored in the <link linkend="assignment-of-variables">variable</link> <userinput>$answer</userinput>.</para>
0172 <para>All <link linkend="number">numbers</link> and <link linkend="assignment-of-variables">variables</link> (that contain numbers) can be compared to each other with comparing operators.</para>
0173 <para>
0174 Here are all possible comparing operators:
0175 <table>
0176 <title>Types of questions</title>
0177 <tgroup cols="3">
0178 <tbody>
0179 <row>
0180 <entry><userinput>$A == $B</userinput></entry>
0181 <entry>equals</entry>
0182 <entry>answer is <quote>true</quote> if <userinput>$A</userinput> equals <userinput>$B</userinput></entry>
0183 </row>
0184 <row>
0185 <entry><userinput>$A != $B</userinput></entry>
0186 <entry>not-equals</entry>
0187 <entry>answer is <quote>true</quote> if <userinput>$A</userinput> does not equal <userinput>$B</userinput></entry>
0188 </row>
0189 <row>
0190 <entry><userinput>$A &gt; $B</userinput></entry>
0191 <entry>greater than</entry>
0192 <entry>answer is <quote>true</quote> if <userinput>$A</userinput> is greater than <userinput>$B</userinput></entry>
0193 </row>
0194 <row>
0195 <entry><userinput>$A &lt; $B</userinput></entry>
0196 <entry>smaller than</entry>
0197 <entry>answer is <quote>true</quote> if <userinput>$A</userinput> is smaller than <userinput>$B</userinput></entry>
0198 </row>
0199 <row>
0200 <entry><userinput>$A &gt;= $B</userinput></entry>
0201 <entry>greater than or equals</entry>
0202 <entry>answer is <quote>true</quote> if <userinput>$A</userinput> is greater than or equals <userinput>$B</userinput></entry>
0203 </row>
0204 <row>
0205 <entry><userinput>$A &lt;= $B</userinput></entry>
0206 <entry>smaller than or equals</entry>
0207 <entry>answer is <quote>true</quote> if <userinput>$A</userinput> is smaller than or equals <userinput>$B</userinput></entry>
0208 </row>
0209 </tbody>
0210 </tgroup>
0211 </table>
0212 Please note that <userinput>$A</userinput> and <userinput>$B</userinput> have to be <link linkend="number">numbers</link> or <link linkend="assignment-of-variables">variables</link> that contain numbers.</para>
0213 </sect2>
0214 
0215 
0216 </sect1>
0217 
0218 
0219 
0220 <sect1 id="commands">
0221 <title>Commands</title>
0222 <para>Using commands you tell the turtle or &kturtle; to do something. Some commands need input, some give output. In this section we explain all the built-in commands of &kturtle;. Alternatively, using <link linkend="learn"><userinput>learn</userinput></link>, you can create your own commands. Built-in commands we discuss here are <glossterm>highlighted</glossterm> with dark blue.</para>
0223 
0224 <sect2 id="moving-the-turtle">
0225 <title>Moving the turtle</title>
0226 <para>There are several commands to move the turtle over the screen.</para>
0227 
0228   <variablelist>
0229     <anchor id="forward" />
0230     <varlistentry> 
0231       <term>forward (fw)<indexterm><primary>forward (fw)</primary></indexterm></term>
0232       <listitem><para><screen>forward X</screen>
0233       <userinput>forward</userinput> moves the turtle forward by the amount of X pixels. When the pen is down the turtle will leave a trail. <userinput>forward</userinput> can be abbreviated to <userinput>fw</userinput>.</para></listitem>
0234     </varlistentry>
0235   </variablelist>
0236   <variablelist>
0237     <anchor id="backward" />
0238     <varlistentry>  
0239       <term>backward (bw)<indexterm><primary>backward (bw)</primary></indexterm></term>
0240       <listitem><para><screen>backward X</screen>
0241       <userinput>backward</userinput> moves the turtle backward by the amount of X pixels. When the pen is down the turtle will leave a trail. <userinput>backward</userinput> can be abbreviated to <userinput>bw</userinput>.</para></listitem>
0242     </varlistentry>
0243   </variablelist>
0244   <variablelist>
0245     <anchor id="turnleft" />
0246     <varlistentry> 
0247       <term>turnleft (tl)<indexterm><primary>turnleft (tl)</primary></indexterm></term>
0248       <listitem><para><screen>turnleft X</screen>
0249       <userinput>turnleft</userinput> commands the turtle to turn an amount of X degrees to the left. <userinput>turnleft</userinput> can be abbreviated to <userinput>tl</userinput>.</para></listitem>
0250     </varlistentry>
0251   </variablelist>
0252   <variablelist>
0253     <anchor id="turnright" />
0254     <varlistentry> 
0255       <term>turnright (tr)<indexterm><primary>turnright (tr)</primary></indexterm></term>
0256       <listitem><para><screen>turnright X</screen>
0257       <userinput>turnright</userinput> the turtle to turn an amount of X degrees to the right. <userinput>turnright</userinput> can be abbreviated to <userinput>tr</userinput>.</para></listitem>
0258     </varlistentry>
0259   </variablelist>
0260   <variablelist>
0261     <anchor id="direction" />
0262     <varlistentry> 
0263       <term>direction (dir)<indexterm><primary>direction (dir)</primary></indexterm></term>
0264       <listitem><para><screen>direction X</screen>
0265       <userinput>direction</userinput> set the turtle's direction to an amount of X degrees counting from zero, and thus is not relative to the turtle's previous direction. <userinput>direction</userinput> can be abbreviated to <userinput>dir</userinput>.</para></listitem>
0266     </varlistentry>
0267   </variablelist>
0268   <variablelist>
0269     <anchor id="getdirection" />
0270     <varlistentry> 
0271       <term>getdirection<indexterm><primary>getdirection</primary></indexterm></term>
0272       <listitem><para><screen>getdirection</screen>
0273       <userinput>getdirection</userinput> returns the turtle's direction as an amount of degrees counting from zero, where zero is the direction when the turtle is pointing upwards.</para></listitem>
0274     </varlistentry>
0275   </variablelist>
0276   <variablelist>
0277     <anchor id="center" />
0278     <varlistentry> 
0279       <term>center<indexterm><primary>center</primary></indexterm></term>
0280       <listitem><para><screen>center</screen>
0281       <userinput>center</userinput> moves the turtle to the center on the canvas.</para></listitem>
0282     </varlistentry>
0283   </variablelist>
0284   <variablelist>
0285     <anchor id="go" />
0286     <varlistentry> 
0287       <term>go<indexterm><primary>go</primary></indexterm></term>
0288       <listitem><para><screen>go X,Y</screen>
0289       <userinput>go</userinput> commands the turtle to go to a certain place on the canvas. This place is X <glossterm linkend="pixels">pixels</glossterm> from the left of the canvas, and Y <glossterm linkend="pixels">pixels</glossterm> from the top of the canvas.</para></listitem>
0290     </varlistentry>
0291   </variablelist>
0292   <variablelist>
0293     <anchor id="gox" />
0294     <varlistentry> 
0295       <term>gox<indexterm><primary>gox (gx)</primary></indexterm></term>
0296       <listitem><para><screen>gox X</screen>
0297       <userinput>gox</userinput> using this command the turtle will move to X <glossterm linkend="pixels">pixels</glossterm> from the left of the canvas whilst staying at the same height. <userinput>gox</userinput> can be abbreviated to <userinput>gx</userinput>.</para></listitem>
0298     </varlistentry>
0299   </variablelist>
0300   <variablelist>
0301     <anchor id="goy" />
0302     <varlistentry> 
0303       <term>goy<indexterm><primary>goy (gy)</primary></indexterm></term>
0304       <listitem><para><screen>goy Y</screen>
0305       <userinput>goy</userinput> using this command the turtle will move to Y <glossterm linkend="pixels">pixels</glossterm> from the top of the canvas whilst staying at the same distance from the left border of the canvas. <userinput>goy</userinput> can be abbreviated to <userinput>gy</userinput>.</para></listitem>
0306     </varlistentry>
0307   </variablelist>
0308   <note><para>Using the commands <userinput>go</userinput>, <userinput>gox</userinput>, <userinput>goy</userinput> and <userinput>center</userinput> the turtle will not draw a line, no matter if the pen is up or down.</para>
0309   </note>
0310 </sect2>
0311 
0312 <sect2 id="locate-the-turtle">
0313 <title>Where is the turtle?</title>
0314 <para>There are two commands which return the position of the turtle on the screen.</para>
0315 
0316   <variablelist>
0317     <anchor id="getx" />
0318     <varlistentry> 
0319       <term>getx<indexterm><primary>getx</primary></indexterm></term>
0320       <listitem><para>
0321       <userinput>getx</userinput> returns the number of pixels from the left of the canvas to the current position of the turtle.</para></listitem>
0322     </varlistentry>
0323   </variablelist>
0324   <variablelist>
0325     <anchor id="gety" />
0326     <varlistentry> 
0327       <term>gety<indexterm><primary>gety</primary></indexterm></term>
0328       <listitem><para>
0329       <userinput>gety</userinput> returns the number of pixels from the top of the canvas to the current position of the turtle.</para></listitem>
0330     </varlistentry>
0331   </variablelist>
0332 </sect2>
0333 
0334 <sect2 id="pen">
0335 <title>The turtle has a pen</title>
0336 <para>The turtle has a pen that draws a line when the turtle moves. There are a few commands to control the pen. In this section we explain these commands.</para>
0337   <variablelist>
0338     <anchor id="penup" />
0339     <varlistentry> 
0340       <term>penup (pu)<indexterm><primary>penup (pu)</primary></indexterm></term>
0341       <listitem><para><screen>penup</screen>
0342       <userinput>penup</userinput> lifts the pen from the canvas. When the pen is <quote>up</quote> no line will be drawn when the turtle moves. See also <userinput>pendown</userinput>. <userinput>penup</userinput> can be abbreviated to <userinput>pu</userinput>.</para></listitem>
0343     </varlistentry>
0344   </variablelist>
0345   <variablelist>
0346     <anchor id="pendown" />
0347     <varlistentry> 
0348       <term>pendown (pd)<indexterm><primary>pendown (pd)</primary></indexterm></term>
0349       <listitem><para><screen>pendown</screen>
0350       <userinput>pendown</userinput> presses the pen down on the canvas. When the pen is press <quote>down</quote> on the canvas a line will be drawn when the turtle moves. See also <userinput>penup</userinput>. <userinput>pendown</userinput> can be abbreviated to <userinput>pd</userinput>.</para></listitem>
0351     </varlistentry>
0352   </variablelist>
0353   <variablelist>
0354     <anchor id="setpenwidth" />
0355     <varlistentry> 
0356       <term>penwidth (pw)<indexterm><primary>penwidth (pw)</primary></indexterm></term>
0357       <listitem><para><screen>penwidth X</screen>
0358       <userinput>penwidth</userinput> sets the width of the pen (the line width) to an amount of X <glossterm linkend="pixels">pixels</glossterm>. <userinput>penwidth</userinput> can be abbreviated to <userinput>pw</userinput>.</para></listitem>
0359     </varlistentry>
0360   </variablelist>
0361   <variablelist>
0362     <anchor id="setfgcolor" />
0363     <varlistentry> 
0364       <term>pencolor (pc)<indexterm><primary>pencolor (pc)</primary></indexterm></term>
0365       <listitem><para><screen>pencolor R,G,B</screen>
0366       <userinput>pencolor</userinput> sets the color of the pen. <userinput>pencolor</userinput> takes an <glossterm linkend="rgb">RGB combination</glossterm> as input. <userinput>pencolor</userinput> can be abbreviated to <userinput>pc</userinput>.</para></listitem>
0367     </varlistentry>
0368   </variablelist>
0369 </sect2>
0370 
0371 <sect2 id="canvas">
0372 <title>Commands to control the canvas</title>
0373 <para>There are several commands to control the canvas.</para>
0374   <variablelist>
0375     <anchor id="resizecanvas" />
0376     <varlistentry>
0377       <term>canvassize (cs)<indexterm><primary>canvassize (cs)</primary></indexterm></term>
0378       <listitem><para><screen>canvassize X,Y</screen>
0379       With the <userinput>canvassize</userinput> command you can set the size of the canvas. It takes X and Y as input, where X is the new canvas width in <glossterm linkend="pixels">pixels</glossterm>, and Y is the new height of the canvas in <glossterm linkend="pixels">pixels</glossterm>. <userinput>canvassize</userinput> can be abbreviated to <userinput>cs</userinput>.</para></listitem>
0380     </varlistentry>
0381   </variablelist>
0382   <variablelist>
0383     <anchor id="setbgcolor" />
0384     <varlistentry> 
0385       <term>canvascolor (cc)<indexterm><primary>canvascolor (cc)</primary></indexterm></term>
0386       <listitem><para><screen>canvascolor R,G,B</screen>
0387       <userinput>canvascolor</userinput> set the color of the canvas. <userinput>canvascolor</userinput> takes an <glossterm linkend="rgb">RGB combination</glossterm> as input. <userinput>canvascolor</userinput> can be abbreviated to <userinput>cc</userinput>.</para></listitem>
0388     </varlistentry>
0389   </variablelist>
0390 </sect2>
0391 
0392 <sect2 id="clean">
0393 <title>Commands to clean up</title>
0394 <para>There are two commands to clean up the canvas after you have made a mess.</para>
0395   <variablelist>
0396     <anchor id="clear" />
0397     <varlistentry> 
0398       <term>clear (ccl)<indexterm><primary>clear (ccl)</primary></indexterm></term>
0399       <listitem><para><screen>clear</screen>
0400       With <userinput>clear</userinput> you can clean all drawings from the canvas. All other things remain: the position and angle of the turtle, the canvascolor, the visibility of the turtle, and the canvas size.</para></listitem>
0401     </varlistentry>
0402   </variablelist>
0403   <variablelist>
0404     <anchor id="reset" />
0405     <varlistentry> 
0406       <term>reset<indexterm><primary>reset</primary></indexterm></term>
0407       <listitem><para><screen>reset</screen>
0408       <userinput>reset</userinput> cleans much more thoroughly than the <userinput>clear</userinput> command. After a <userinput>reset</userinput> command everything is like is was when you had just started &kturtle;. The turtle is positioned at the middle of the screen, the canvas color is white, the turtle draws a black line on the canvas and the canvassize is set to 400 x 400 pixels.</para></listitem>
0409     </varlistentry>
0410   </variablelist>
0411 </sect2>
0412 
0413 <sect2 id="sprites">
0414 <title>The turtle is a sprite</title>
0415 <para>First a brief explanation of what sprites are: sprites are small pictures that can be moved around the screen, like we often see in computer games. Our turtle is also a sprite. For more info see the glossary on <glossterm linkend="sprites">sprites</glossterm>.</para>
0416 <para>Next you will find a full overview on all commands to work with sprites.</para>
0417 <note><para>The current version of &kturtle; does not yet support the use of sprites other than the turtle. With future versions you will be able to change the turtle into something of your own design.</para></note>
0418   <variablelist>
0419     <anchor id="spriteshow" />
0420     <varlistentry> 
0421       <term>spriteshow (ss)<indexterm><primary>spriteshow (ss)</primary></indexterm></term>
0422       <listitem><para><screen>spriteshow</screen>
0423       <userinput>spriteshow</userinput> makes the turtle visible again after it has been hidden. <userinput>spriteshow</userinput> can be abbreviated to <userinput>ss</userinput>.</para></listitem>
0424     </varlistentry>
0425   </variablelist>
0426   <variablelist>
0427     <anchor id="spritehide" />
0428     <varlistentry> 
0429       <term>spritehide (sh)<indexterm><primary>spritehide (sh)</primary></indexterm></term>
0430       <listitem><para><screen>spritehide</screen>
0431       <userinput>spritehide</userinput> hides the turtle. This can be used if the turtle does not fit in your drawing. <userinput>spritehide</userinput> can be abbreviated to <userinput>sh</userinput>.</para></listitem>
0432     </varlistentry>
0433   </variablelist>
0434 </sect2>
0435 
0436 <sect2 id="writing">
0437 <title>Can the turtle write?</title>
0438 <para>The answer is: <quote>yes</quote>. The turtle can write: it writes just about everything you command it to.</para>
0439   <variablelist>
0440     <anchor id="print" />
0441     <varlistentry> 
0442       <term>print<indexterm><primary>print</primary></indexterm></term>
0443       <listitem><para><screen>print X</screen>
0444       The <userinput>print</userinput> command is used to command the turtle to write something on the canvas. <userinput>print</userinput> takes numbers and strings as input. You can <userinput>print</userinput> various numbers and strings using the <quote>+</quote> symbol. See here a small example:
0445 <screen>
0446 $year = 2003
0447 $author = "Cies"
0448 print $author + " started the KTurtle project in " + $year + " and still enjoys working on it!"
0449 </screen>
0450       </para></listitem>
0451     </varlistentry>
0452   </variablelist>
0453   <variablelist>
0454     <anchor id="fontsize" />
0455     <varlistentry> 
0456       <term>fontsize<indexterm><primary>fontsize</primary></indexterm></term>
0457       <listitem><para><screen>fontsize X</screen>
0458       <userinput>fontsize</userinput> sets the size of the font that is used by <userinput>print</userinput>. <userinput>fontsize</userinput> takes one input which should be a number. The size is set in <glossterm linkend="pixels">pixels</glossterm>.</para></listitem>
0459     </varlistentry>
0460   </variablelist>
0461 </sect2>
0462 
0463 <sect2 id="math-commands">
0464 <title>Mathematical commands</title>
0465 <para>The following commands are &kturtle;'s more advanced mathematical commands.</para>
0466   <variablelist>
0467     <anchor id="round" />
0468     <varlistentry>
0469       <term>round<indexterm><primary>round</primary></indexterm></term>
0470       <listitem><para><screen>round(x)</screen>
0471       <userinput>round</userinput> the given number to the nearest integer.
0472 <screen>
0473 print round(10.8)
0474 forward 20
0475 print round(10.3)
0476 </screen>
0477       With this code the turtle will print the numbers 11 and 10.</para></listitem>
0478     </varlistentry>
0479   </variablelist>
0480   <variablelist>
0481     <anchor id="random" />
0482     <varlistentry> 
0483       <term>random (rnd)<indexterm><primary>random (rnd)</primary></indexterm></term>
0484       <listitem><para><screen>random X,Y</screen>
0485       <userinput>random</userinput> is a command that takes input and gives output. As input are required two numbers, the first (X) sets the minimum output, the second (Y) sets the maximum. The output is a randomly chosen number that is equal or greater than the minimum and equal or smaller than the maximum. Here a small example:
0486       <screen>
0487 repeat 500 {
0488   $x = random 1,20
0489   forward $x
0490   turnleft 10 - $x
0491 }
0492 </screen>
0493       Using the <userinput>random</userinput> command you can add a bit of chaos to your program.</para></listitem>
0494     </varlistentry>
0495   </variablelist>
0496   <variablelist>
0497     <anchor id="mod" />
0498     <varlistentry> 
0499       <term>mod<indexterm><primary>mod</primary></indexterm></term>
0500       <listitem><para><screen>mod X,Y</screen>
0501       The <userinput>mod</userinput> returns remainder of the division of first number by the second number.</para></listitem>
0502     </varlistentry>
0503   </variablelist>
0504   <variablelist>
0505     <anchor id="sqrt" />
0506     <varlistentry> 
0507       <term>sqrt<indexterm><primary>sqrt</primary></indexterm></term>
0508       <listitem><para><screen>sqrt X</screen>
0509       The <userinput>sqrt</userinput> command is sued to find the square root of a number, X.</para></listitem>
0510     </varlistentry>
0511   </variablelist>
0512 <!--
0513   <variablelist>
0514     <anchor id="exp" />
0515     <varlistentry> 
0516       <term>exp<indexterm><primary>exp</primary></indexterm></term>
0517       <listitem><para><screen>sqrt X</screen>
0518       </para></listitem>
0519     </varlistentry>
0520   </variablelist>
0521 -->
0522   <variablelist>
0523     <anchor id="pi" />
0524     <varlistentry> 
0525       <term>pi<indexterm><primary>pi</primary></indexterm></term>
0526       <listitem><para><screen>pi</screen>
0527       This command returns the constant Pi, <userinput>3.14159</userinput>.</para></listitem>
0528     </varlistentry>
0529   </variablelist>
0530   <variablelist>
0531     <anchor id="sin" />
0532     <anchor id="cos" />
0533     <anchor id="tan" />
0534     <varlistentry>
0535       <term>sin<indexterm><primary>sin</primary></indexterm>, cos<indexterm><primary>cos</primary></indexterm>, tan<indexterm><primary>tan</primary></indexterm></term>
0536       <listitem><para>
0537 <screen>
0538 sin X
0539 cos X
0540 tan X
0541 </screen>
0542       These three commands represent the world famous trigoniometrical functions <userinput>sin</userinput>, <userinput>cos</userinput> and <userinput>tan</userinput>. The input argument of these commands, X, is a <link linkend="number">number</link>.</para></listitem>
0543     </varlistentry>
0544   </variablelist>
0545   <variablelist>
0546     <anchor id="arcsin" />
0547     <anchor id="arccos" />
0548     <anchor id="arctan" />
0549     <varlistentry>
0550       <term>arcsin<indexterm><primary>arcsin</primary></indexterm>, arccos<indexterm><primary>arccos</primary></indexterm>, arctan<indexterm><primary>arctan</primary></indexterm></term>
0551       <listitem><para>
0552 <screen>
0553 arcsin X
0554 arccos X
0555 arctan X
0556 </screen>
0557       These commands are the inverse functions of <link linkend="sin">sin</link>, <link linkend="cos">cos</link> and <link linkend="tan">tan</link>. The input argument of these commands, X, is a <link linkend="number">number</link>.</para></listitem>
0558     </varlistentry>
0559   </variablelist>
0560 </sect2>
0561 
0562 <sect2 id="dialogs">
0563 <title>Input and feedback through dialogs</title>
0564 <para>A dialog is a small pop-up window that provides some feedback or asks for some input. &kturtle; has two commands for dialogs, namely: <userinput>message</userinput> and <userinput>ask</userinput></para>
0565   <variablelist>
0566     <anchor id="message" />
0567     <varlistentry> 
0568       <term>message<indexterm><primary>message</primary></indexterm></term>
0569       <listitem><para><screen>message X</screen>
0570       The <userinput>message</userinput> command takes a <link linkend="string">string</link> as input. It shows a pop-up dialog containing the text from the <link linkend="string">string</link>.
0571 <screen>
0572 message "Cies started KTurtle in 2003 and still enjoys working on it!"
0573 </screen>
0574       </para></listitem>
0575     </varlistentry>
0576   </variablelist>
0577   <variablelist>
0578     <anchor id="ask" />
0579     <varlistentry> 
0580       <term>ask<indexterm><primary>ask</primary></indexterm></term>
0581       <listitem><para><screen>ask X</screen>
0582       <userinput>ask</userinput> takes a <link linkend="string">string</link> as input.  It shows this string in a pop-up dialog (similar to <link linkend="message">message</link>), along with an input field.  After the user has entered a <link linkend="number">number</link> or a <link linkend="string">string</link> into this, the result can be stored in a <link linkend="assignment-of-variables">variable</link> or passed as an argument to a <link linkend="commands">command</link>. For example:
0583 <screen>
0584 $in = ask "What is your year of birth?"
0585 $out = 2003 - $in
0586 print "In 2003 you were " + $out + " years old at some point."
0587 </screen>
0588       If the user cancels the input dialog, or does not enter anything at all, the <link linkend="assignment-of-variables">variable</link> is empty.</para></listitem>
0589     </varlistentry>
0590   </variablelist>
0591 </sect2>
0592 
0593 </sect1>
0594 
0595 
0596 
0597 <sect1 id="assignment-of-variables">
0598 <title>Assignment of variables</title>
0599 <para>First we have a look at variables, then we look at assigning values to those variables.
0600 <!-- The final part of this section considers <link linkend="scoping">scoping of variables</link>. -->
0601 </para>
0602 
0603 <para>Variables are words that start with a <quote>$</quote>, in the <link linkend="the-editor">editor</link> they are <glossterm>highlighted</glossterm> with purple.</para>
0604 
0605 <para>Variables can contain any <link linkend="number">number</link>, <link linkend="string">string</link> or <link linkend="boolean-value">boolean (true/false) value</link>. Using the assignment, <userinput>=</userinput>, a variable is given its content. It will keep that content until the program finishes executing or until the variable is reassigned to something else.</para>
0606 
0607 <para>You can use variables, once assigned, just as if they are their content. For instance in the following piece of &turtlescript;:
0608 <screen>
0609 $x = 10
0610 $x = $x / 3
0611 print $x
0612 </screen>
0613 First the variable <userinput>$x</userinput> is assigned to <userinput>10</userinput>. Then <userinput>$x</userinput> is reassigned to itself divided by <userinput>3</userinput> &mdash; this effectively means <userinput>$x</userinput> is reassigned to product of <userinput>10 / 3</userinput>. Finally <userinput>$x</userinput> is printed. In line two and three you see that <userinput>$x</userinput> is used as if it is its contents.</para>
0614 
0615 <para>Variables have to be assigned in order to be used. For example:
0616 <screen>
0617 print $n
0618 </screen>
0619 Will result in an error message.</para>
0620 
0621 <para>Please consider the following piece of &turtlescript;:
0622 <screen>
0623 $a = 2004
0624 $b = 25
0625 
0626 # the next command prints "2029"
0627 print $a + $b
0628 backward 30
0629 # the next command prints "2004 plus 25 equals 2029"
0630 print $a + " plus " + $b + " equals " + ($a + $b)
0631 </screen>
0632 In the first two lines the variables <userinput>$a</userinput> and <userinput>$b</userinput> are set to 2004 and 25. Then in two <userinput>print</userinput> commands with a <userinput>backward 30</userinput> in between are executed. The comments before the <userinput>print</userinput> commands explain what they are doing. The command <userinput>backward 30</userinput> is there to make 
0633 sure every output is on a new line.
0634 As you see variables can be used just as if their where what they contain, you can use them with any kind of <link linkend="operators">operators</link> or give them as input when invoking <link linkend="commands">commands</link>.</para>
0635 
0636 <para>One more example:
0637 <screen>
0638 $name = ask "What is your name?"
0639 print "Hi " + $name + "! Good luck while learning the art of programming..."
0640 </screen>
0641 Pretty straight forward. Again you can see that the variable <userinput>$name</userinput>, treated just like a string.</para>
0642 
0643 <para>When using variables the <link linkend="the-inspector">inspector</link> is very helpful. It shows you the contents of all variables that are currently in use.</para>
0644 </sect1>
0645 
0646 
0647 
0648 <sect1 id="controlling-execution">
0649 <title>Controlling execution</title>
0650 <para>The execution controllers enable you &mdash; as their name implies &mdash; to control execution.</para>
0651 <para>Execution controlling commands are <glossterm>highlighted</glossterm> with dark green in a bold font type. The brackets are mostly used together with execution controllers and they are <glossterm>highlighted</glossterm> with black.</para>
0652 
0653 <sect2 id="wait">
0654 <title>Have the turtle wait</title>
0655 <para>If you have done some programming in &kturtle; you have might noticed that the turtle can be very quick at drawing. This command makes the turtle wait for a given amount of time.</para>
0656   <variablelist>
0657     <varlistentry>
0658       <term>wait<indexterm><primary>wait</primary></indexterm></term>
0659       <listitem><para><screen>wait X</screen>
0660       <userinput>wait</userinput> makes the turtle wait for X seconds.
0661 <screen>
0662 repeat 36 {
0663   forward 5
0664   turnright 10
0665   wait 0.5
0666 }
0667 </screen>
0668       This code draws a circle, but the turtle will wait half a second
0669       after each step. This gives the impression of a slow-moving turtle.</para></listitem>
0670     </varlistentry>
0671   </variablelist>
0672 </sect2>
0673 
0674 <sect2 id="if">
0675 <title>Execute <quote>if</quote></title>
0676   <variablelist>
0677     <varlistentry>
0678       <term>if<indexterm><primary>if</primary></indexterm></term>
0679       <listitem><para><screen>if <link linkend="boolean-value">boolean</link> { ... }</screen>
0680       The code that is placed between the brackets will only be executed <userinput>if</userinput> the <link linkend="boolean-value">boolean value</link> evaluates <quote>true</quote>.
0681       <screen>
0682 $x = 6
0683 if $x &gt; 5 {
0684   print "$x is greater than five!"
0685 }
0686 </screen>
0687       On the first line <userinput>$x</userinput> is set to 6. On the second line a <link linkend="comparing-operators">comparing operator</link> is used to evaluate <userinput>$x &gt; 5</userinput>. Since this evaluates <quote>true</quote>, 6 is larger than 5, the execution controller <userinput>if</userinput> will allow the code between the brackets to be executed.</para></listitem>
0688     </varlistentry>
0689   </variablelist>
0690 </sect2>
0691 
0692 <sect2 id="else">
0693 <title>If not, in other words: <quote>else</quote></title>
0694   <variablelist>
0695     <varlistentry>
0696       <term>else<indexterm><primary>else</primary></indexterm></term>
0697       <listitem><para><screen>if <link linkend="boolean-value">boolean</link> { ... } else { ... }</screen>
0698       <userinput>else</userinput> can be used in addition to the execution controller <link linkend="if"><userinput>if</userinput></link>. The code between the brackets after <userinput>else</userinput> is only executed if the <link linkend="boolean-value">boolean</link> evaluates <quote>false</quote>.
0699       <screen>
0700 reset
0701 $x = 4
0702 if $x &gt; 5 {
0703   print "$x is greater than five!"
0704 } else {
0705   print "$x is smaller than six!"
0706 }
0707 </screen>
0708       The <link linkend="comparing-operators">comparing operator</link> evaluates the expression <userinput>$x &gt; 5</userinput>. Since 4 is not greater than 5 the expression evaluates <quote>false</quote>. This means the code between the brackets after <userinput>else</userinput> gets executed.</para></listitem>
0709     </varlistentry>
0710   </variablelist>
0711 </sect2>
0712 
0713 <sect2 id="while">
0714 <title>The <quote>while</quote> loop</title>
0715   <variablelist>
0716     <varlistentry>
0717       <term>while<indexterm><primary>while</primary></indexterm></term>
0718       <listitem><para><screen>while <link linkend="boolean-value">boolean</link> { ... }</screen>
0719       The execution controller <userinput>while</userinput> is a lot like <link linkend="if"><userinput>if</userinput></link>. The difference is that <userinput>while</userinput> keeps repeating (looping) the code between the brackets until the <link linkend="boolean-value">boolean</link> evaluates <quote>false</quote>.
0720       <screen>
0721 $x = 1
0722 while $x &lt; 5 {
0723   forward 10
0724   wait 1
0725   $x = $x + 1
0726 }
0727 </screen>
0728       On the first line <userinput>$x</userinput> is set to 1. On the second line <userinput>$x &lt; 5</userinput> is evaluated. Since the answer to this question is <quote>true</quote> the execution controller <userinput>while</userinput> starts executing the code between the brackets until the <userinput>$x &lt; 5</userinput> evaluates <quote>false</quote>. In this case the code between the brackets will be executed 4 times, because every time the fifth line is executed <userinput>$x</userinput> increases by 1.</para></listitem>
0729     </varlistentry>
0730   </variablelist>
0731 </sect2>
0732 
0733 <sect2 id="repeat">
0734 <title>The <quote>repeat</quote> loop</title>
0735   <variablelist>
0736     <varlistentry>
0737       <term>repeat<indexterm><primary>repeat</primary></indexterm></term>
0738       <listitem><para><screen>repeat <link linkend="number">number</link> { ... }</screen>
0739       The execution controller <userinput>repeat</userinput> is a lot like <link linkend="while"><userinput>while</userinput></link>. The difference is that <userinput>repeat</userinput> keeps repeating (looping) the code between the brackets for as many times as the given number.</para></listitem>
0740     </varlistentry>
0741   </variablelist>
0742 </sect2>
0743 
0744 <sect2 id="for">
0745 <title>The <quote>for</quote> loop, a counting loop</title>
0746   <variablelist>
0747     <varlistentry>
0748       <term>for<indexterm><primary>for</primary></indexterm><indexterm><primary>to</primary></indexterm><indexterm><primary>step</primary></indexterm></term>
0749       <listitem><para><screen>for <link linkend="assignment-of-variables">variable</link> = <link linkend="number">number</link> to <link linkend="number">number</link> { ... }</screen>
0750       The <userinput>for</userinput> loop is a <quote>counting loop</quote>, &ie; it keeps count for you. The first number sets the variable to the value in the first loop. Every loop the number is increased until the second number is reached.
0751       <screen>
0752 for $x = 1 to 10 {
0753   print $x * 7
0754   forward 15
0755 }
0756 </screen>
0757      Every time the code between the brackets is executed the <userinput>$x</userinput> is increased by 1, until <userinput>$x</userinput> reaches the value of 10. The code between the brackets prints the <userinput>$x</userinput> multiplied by 7. After this program finishes its execution you will see the times table of 7 on the canvas.
0758      </para>
0759      <para>
0760      The default step size of a loop is 1, you can use an other value with
0761      <screen>for <link linkend="assignment-of-variables">variable</link> = <link linkend="number">number</link> to <link linkend="number">number</link> step <link linkend="number">number</link> { ... }</screen></para></listitem>
0762     </varlistentry>
0763   </variablelist>
0764 </sect2>
0765 
0766 <sect2 id="break">
0767 <title>Leave a loop</title>
0768   <variablelist>
0769     <varlistentry>
0770       <term>break<indexterm><primary>break</primary></indexterm></term>
0771       <listitem><para><screen>break</screen>
0772       Terminates the current loop immediately and transfers control to the statement immediately following that loop.</para></listitem>
0773     </varlistentry>
0774   </variablelist>
0775 </sect2>
0776 
0777 <sect2 id="exit">
0778 <title>Stop executing your program</title>
0779   <variablelist>
0780     <varlistentry>
0781       <term>exit<indexterm><primary>exit</primary></indexterm></term>
0782       <listitem><para><screen>exit</screen>
0783       Finishes the execution of your program.</para></listitem>
0784     </varlistentry>
0785   </variablelist>
0786 </sect2>
0787 
0788 <sect2 id="assert">
0789 <title>Checking assertions at runtime</title>
0790   <variablelist>
0791     <varlistentry>
0792       <term>assert<indexterm><primary>assert</primary></indexterm></term>
0793       <listitem><para><screen>assert <link linkend="boolean-value">boolean</link></screen>
0794       Can be used to reason about program or input correctness.
0795       <screen>
0796 $in = ask "What is your year of birth?"
0797 # the year must be positive
0798 assert $in > 0
0799 </screen></para></listitem>
0800     </varlistentry>
0801   </variablelist>
0802 </sect2>
0803 </sect1>
0804 
0805 
0806 <sect1 id="learn">
0807 
0808 <title>Create your own commands with <quote>learn</quote></title>
0809 <para><userinput>learn</userinput><indexterm><primary>learn</primary></indexterm> is special as it is used to create your own commands. The commands you create can take <glossterm linkend="input-output">input</glossterm> and return <glossterm linkend="input-output">output</glossterm>. Let us take a look at how a new command is created:
0810 <screen>
0811 learn circle $x {
0812   repeat 36 {
0813     forward $x
0814     turnleft 10
0815   }
0816 }
0817 </screen>
0818 The new command is called <userinput>circle</userinput>. <userinput>circle</userinput> takes one <glossterm linkend="input-output">input</glossterm> argument, to set the size of the circle. <userinput>circle</userinput> returns no <glossterm linkend="input-output">output</glossterm>. The <userinput>circle</userinput> command can now be used like a normal command in the rest of the code. See this example:
0819 <screen>
0820 learn circle $X {
0821   repeat 36 {
0822     forward $X 
0823     turnleft 10 
0824   }
0825 }
0826 
0827 go 200,200 
0828 circle 20
0829 
0830 go 300,200 
0831 circle 40  
0832 </screen>
0833 </para>
0834 <para>In the next example, a command with a return<indexterm><primary>return</primary></indexterm> value is created.
0835 <screen>
0836 learn faculty $x {
0837   $r = 1
0838   for $i = 1 to $x {
0839     $r = $r * $i
0840   }
0841   return $r
0842 }
0843 
0844 print faculty 5
0845 </screen>
0846 In this example a new command called <userinput>faculty</userinput> is created. If the input of this command is <userinput>5</userinput> then the output is <userinput>5*4*3*2*1</userinput>. By using <userinput>return</userinput> the <glossterm linkend="input-output">output</glossterm> value is specified and the execution is returned.</para>
0847 <para>Commands can have more than one <glossterm linkend="input-output">input</glossterm>. In the next example, a command that draws a rectangle is created:
0848 <screen>
0849 learn box $x, $y {
0850   forward $y
0851   turnright 90
0852   forward $x
0853   turnright 90
0854   forward $y
0855   turnright 90
0856   forward $x
0857   turnright 90
0858 }
0859 </screen>
0860 Now you can run <userinput>box 50, 100</userinput> and the turtle will draw a rectangle on the canvas. 
0861 </para>
0862   
0863 </sect1>
0864 
0865 </chapter>