<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://wiki.compsci.ca/skins/common/feed.css?270"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wiki.compsci.ca/index.php?feed=atom&amp;target=Saad&amp;title=Special%3AContributions%2FSaad</id>
		<title>Compsci.ca Wiki - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.compsci.ca/index.php?feed=atom&amp;target=Saad&amp;title=Special%3AContributions%2FSaad"/>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Special:Contributions/Saad"/>
		<updated>2026-04-15T00:53:15Z</updated>
		<subtitle>From Compsci.ca Wiki</subtitle>
		<generator>MediaWiki 1.16.0</generator>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_Joysticks</id>
		<title>Turing Joysticks</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_Joysticks"/>
				<updated>2008-10-12T21:56:33Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Hello and welcome to the tutorial on using joysticks/joypads. I personally have a joypad with 2 analog sticks, 12 buttons, and a D-PAD/POV which has been tested. This tutorial will require two things: &lt;br /&gt;
*You have at least one joystick/joypad&lt;br /&gt;
*Secondly, you need the revised module at the bottom as the Turing default will not work&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==FAQ==&lt;br /&gt;
*So what do you plan to accomplish in this tutorial?&lt;br /&gt;
**I'm planning to tackle the four main parts of the joystick module, breaking it down into parts, and ultimatly sharing this method of input with the world!&lt;br /&gt;
&lt;br /&gt;
*So what's so great about joysticks/joypads?&lt;br /&gt;
**Well the mouse has upto 3 buttons and a cursor, a keyboard has keys, however a joypad (depending on the model) will have upto 32 buttons, upto 3 analog sticks, and a D-PAD/POV! that equals a ton more input and functionallity&lt;br /&gt;
&lt;br /&gt;
*What if I don't have a joypad/joystick?&lt;br /&gt;
**Not to worry just follow along&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Well now to get started!&lt;br /&gt;
&lt;br /&gt;
==Setup==&lt;br /&gt;
First things first, since the module is not pervasive it needs to be imported, also make sure the module is in the same folder as the program using it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import joystick&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now that that is done varibles need to be initalized just as though mouse input or keyboard input were being used.&lt;br /&gt;
&amp;lt;pre&amp;gt;type joypad : % create a joypad type&lt;br /&gt;
    record&lt;br /&gt;
        button : array 1 .. 32 of boolean %create 32 instances of buttons&lt;br /&gt;
        pos : joystick.PosRecord /*used to find all joypad intake with&lt;br /&gt;
         the exception of buttons*/&lt;br /&gt;
        caps : joystick.CapsRecord %used to find the max on joypad analog&lt;br /&gt;
    end record&lt;br /&gt;
&lt;br /&gt;
var joy : array 1 .. 2 of joypad %create an array of joypad&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What just happened? Well now to break that down.&lt;br /&gt;
&lt;br /&gt;
First an instance of the joypad type containing a set of varibles was created. The first varible is an array of 32 boolean the number can be any number 1&amp;lt;n&amp;lt;32, basically it's all the buttons being used (32buttons were used to show the maximum potential). The next varible is actually taking the type of another record.&lt;br /&gt;
The record can be found in the joystick module it is basically bringing all input except the button states. Next is another type found in the joystick module this time bringing back the maximum potential of the joystick/joypad. Then a joy varible to use the varibles was created an array can be used for multiple joysticks/joypads.&lt;br /&gt;
&lt;br /&gt;
Well now that is done time to move on. Default values need to be assigned as a failsafe for any joysticks not detected.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/*set default joypad varibles*/&lt;br /&gt;
for i : 1 .. upper (joy)&lt;br /&gt;
    for ii : 1 .. upper (joy (i).button)&lt;br /&gt;
        joy (i).button (ii) := false&lt;br /&gt;
    end for&lt;br /&gt;
    joy (i).pos.POV := 66535&lt;br /&gt;
    joystick.Capabilities (i - 1, joy (i).caps)&lt;br /&gt;
end for&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What just happened is rather simple. First a for loop was created to to cover all elements of joy, then another for loop for all 32 buttons to set the array to false false. Then the D-PAD/POV' was set to the default value of 66535. Lastly the joystick capibilities was called to find maximum capiblities of the joystick/joypad. Next joystick needs to be galled upon each loop run in the main program to gather information to what buttons are being pressed, etc... just as if it were a mouse or keyboard.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;joystick.GetInfo (joystick[1/2], joy ([1/2]).button)&lt;br /&gt;
joystick.Read (joystick[1/2], joy ([1/2]).pos)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The GetInfo procedure will return upto 32 button values. The Read procedure will return with Analog and D-PAD/POV information.&lt;br /&gt;
&lt;br /&gt;
==Using Buttons==&lt;br /&gt;
This part of the tutorial is similar to a keyboard. A keyboard has an array of keys that return true if the key is pressed (info found with Input.KeyDown). A joystick/joypad has an array of buttons that return true if the button is pressed (info found with joystick.GetInfo). Therefore:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;if joy.button(4) then&lt;br /&gt;
    put &amp;quot;Button 4 is being pressed&amp;quot;&lt;br /&gt;
end if&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Hopefully that is understandable. And that is all for buttons.&lt;br /&gt;
&lt;br /&gt;
==Using Analog Sticks==&lt;br /&gt;
&lt;br /&gt;
This part is not to hard however the analog does not return an X/Y pixel value, so a conversion is needed. First to get the current analog position.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;put &amp;quot;analog 1 - x - &amp;quot;, joy (1).pos.xpos&lt;br /&gt;
put &amp;quot;analog 1 - y - &amp;quot;, joy (1).pos.ypos&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Well these values need to be converted like so.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;put &amp;quot;analog 1 - x - &amp;quot;, round (joy (joystickNO + 1).pos.xpos / joy (joystickNO + 1).caps.maxX * maxx)&lt;br /&gt;
put &amp;quot;analog 1 - y - &amp;quot;, round (joy (joystickNO + 1).pos.ypos / joy (joystickNO + 1).caps.maxY * maxy)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This must be done everytime but it shouldn't be to hard to create a function to do this.&lt;br /&gt;
&lt;br /&gt;
The axis use a name for each x and y direction: analog 1 x - xpos, analog 1 y - ypos, analog 2 x - zpos, analog 2 y - rpos, analog 3 x - upos, analog 3 y - vpos.&lt;br /&gt;
&lt;br /&gt;
==Using D-PAD/POV==&lt;br /&gt;
First off, reading D-PAD/POV data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;put joy ([1/2]).pos.POV&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This to needs to be converted into usable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; put &amp;quot;D-PAD (AKA POV) - &amp;quot; ..&lt;br /&gt;
if joy (    [ 1 / 2]).pos.POV = 0 then&lt;br /&gt;
    put &amp;quot;up&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2]).pos.POV = 4500 then&lt;br /&gt;
    put &amp;quot;up right&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2 ]).pos.POV = 9000 then&lt;br /&gt;
    put &amp;quot;right&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2 ]).pos.POV = 13500 then&lt;br /&gt;
    put &amp;quot;down right&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2 ]).pos.POV = 18000 then&lt;br /&gt;
    put &amp;quot;down&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2  ]).pos.POV = 22500 then&lt;br /&gt;
    put &amp;quot;down left&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2 ]).pos.POV = 27000 then&lt;br /&gt;
    put &amp;quot;left&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2  ]).pos.POV = 31500 then&lt;br /&gt;
    put &amp;quot;up left&amp;quot;&lt;br /&gt;
else&lt;br /&gt;
    put &amp;quot;none&amp;quot;&lt;br /&gt;
end if&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Well that is all there is to it.&lt;br /&gt;
&lt;br /&gt;
==Challenges==&lt;br /&gt;
Here are some challenges to try out.&lt;br /&gt;
&lt;br /&gt;
# Make a circle that moves with the analog stick&lt;br /&gt;
# Move a sprite around the screen with the D-PAD/POV&lt;br /&gt;
# Make a simple Simon game with the buttons&lt;br /&gt;
# Try to make a basic game where you talk to people, walking around with D-PAD/POV talking to the character with the buttons&lt;br /&gt;
&lt;br /&gt;
==Refrences==&lt;br /&gt;
*Joystick info http://compsci.ca/v3/viewtopic.php?t=6373&lt;br /&gt;
*Joystick info http://compsci.ca/v3/viewtopic.php?t=169&lt;br /&gt;
&lt;br /&gt;
==Downloads==&lt;br /&gt;
[http://compsci.ca/v3/download.php?id=5324&amp;gt;Joystick Module]&lt;br /&gt;
&lt;br /&gt;
[http://compsci.ca/v3/download.php?id=5325&amp;gt;Demonstration]&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures</id>
		<title>Turing Functions and Procedures</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures"/>
				<updated>2008-10-12T20:25:36Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: /* Functions and Procedures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Thus far, we've learned enough of the Turing language to be able to write some decently large programs. We've learned some fundamentals of Turing -- variables, basic input/output, and if statements. Indeed, these concepts translate relatively smoothly to most any other programming language you might learn. So, having mastered these basics, what's next? Well, having a way to organize our code would be good. Right now, whatever code you write runs linearly, from top to bottom. If you want to run a section of code twice, you have to type it out twice. Wouldn't it be great if we could factor out the commonalities in our code, so we can write less? In truth, we haven't learned very much, so it might be difficult to see the advantage of this. Just bear with me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing the Procedure==&lt;br /&gt;
&lt;br /&gt;
At its simplest, a procedure is just some lines of Turing code. We wrap some special syntax around our lines of Turing code and think up a name for them. After we have defined our procedure, we can ''call'' the procedure by saying its name. When we do this, we run those lines of Turing code that are wrapped inside the procedure definition. This is easiest to see with an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So now the single line of code, ''put &amp;quot;Hello world!&amp;quot;'', can be executed any time we call the ''greet'' procedure. The above code is identical to the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
All I've done is substituted the code inside the ''greet'' procedure for the call to ''greet'' in the last line of the original program.&lt;br /&gt;
&lt;br /&gt;
So, why is this useful? It's useful because now whenever we want to output &amp;quot;Hello world!&amp;quot;, we don't have to type, ''put &amp;quot;Hello world!&amp;quot;'', but only ''greet''. Okay, so we saved on typing fourteen charactes. Not a big deal, right? But what if we wrote a program that said &amp;quot;Hello world!&amp;quot; a lot. Our program said hello to the world ten times during the course of its execution. Now, we're looking back at our program and thinking, &amp;quot;You know, 'Hello world!' isn't really what we want to say. We really are only talking to one person--George. We should instead say 'Hello George!'.&amp;quot; So, now we've got to go into our code and change it. If we used a ''greet'' procedure, this change involves changing the definition of the procedure so that it becomes the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello George!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we hadn't used this procedure, we would have to go through our code, searching for any time we said &amp;quot;Hello world!&amp;quot; and change it to be &amp;quot;Hello George!&amp;quot;. That's a lot of unnecessary work, relative to how effective our ''greet'' solution is.&lt;br /&gt;
&lt;br /&gt;
But now, what if we aren't always talking to George?&lt;br /&gt;
&lt;br /&gt;
==Procedures with Parameters==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure to greet to George and another procedure to greet to Phil and another to greet to Anthony, we should try to write a procedure that can greet to anyone. Here's one possibility:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name : string&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;George&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Phil&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Anthony&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This works, but it's clunky. We've got that variable up there, ''name''. It's what we call a '''global variable'''. It's called that because it is global to the whole program. No matter where we are in our code, we can always see the ''name'' variable. Really, we don't need to keep track of ''name''. We only need it ''inside the greet procedure''. Here's where '''parameters''' are used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Phil&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Anthony&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the definition of our greet procedure, I added that extra bit at the end--the stuff in parentheses. That says that when we call the greet procedure, we must give it one '''argument''', and that argument must be a string. It also says that for all code inside the procedure, there exists a '''constant''' called &amp;quot;name&amp;quot; that has the value we give it when we call the procedure. So, when we call ''greet (&amp;quot;George&amp;quot;)'', we are setting ''name'' to be &amp;quot;George&amp;quot;, and we execute the code inside the procedure. The following two pieces of code are identical:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, &amp;quot;George&amp;quot;, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I mentioned earlier that inside the ''greet'' procedure there exists this constant called name. There are two things we need to understand about it. First, it is constant. We can't change the value of name. Let's get some example code and figure out why:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This code fails because, inside ''greet'', ''name'' is really just a ''reference to &amp;quot;George&amp;quot;''. When we try to redefine name, we're really trying to redefine the string literal &amp;quot;George&amp;quot;. Of course, that's impossible. What if we did this, though?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now you might think that, inside ''greet'', ''name'' is a reference to ''person'', which is a variable, so attempting to redefine ''name'' is really redefining ''person'', and that's okay. However, it's not quite that simple. See, ''greet'' expects a string as its argument, not a variable. In ''greet (person)'', ''person'' is an expression. It gets evaluated down to a string value; it gets evaluated to &amp;quot;George&amp;quot;. Now, once again, we're passing the string literal &amp;quot;George&amp;quot; into ''greet'', and we're back at our first situation.&lt;br /&gt;
&lt;br /&gt;
The second thing we need to understand about ''name'' is that it's '''local'''. It exists only within the procedure. The following code fails:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
put name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''name'' doesn't exist outside of the ''greet'' procedure. It's '''local'''. It's '''scope''' is the ''greet'' procedure. You should also note that each time we call ''greet'' we are creating a new ''name'' constant.&lt;br /&gt;
&lt;br /&gt;
What if we wanted to write a procedure that introduces two people to each other? We need a procedure that can two arguments, not one.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Procedures with Multiple Parameters==&lt;br /&gt;
&lt;br /&gt;
I will now define a procedure, called ''introduce'', that introduces two people to each other:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1 : string, person2 : string)&lt;br /&gt;
    put person1, &amp;quot;, this is &amp;quot;, person2, &amp;quot;.&amp;quot;&lt;br /&gt;
    put person2, &amp;quot;, meet &amp;quot;, person1, &amp;quot;.&amp;quot;&lt;br /&gt;
end introduce&lt;br /&gt;
&lt;br /&gt;
% Call the procedure&lt;br /&gt;
introduce (&amp;quot;Jackie&amp;quot;, &amp;quot;Bruce&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output of this program will be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Jackie, this is Bruce.&lt;br /&gt;
Bruce, meet Jackie.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, reversing the order of the names results in a different output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
introduce (&amp;quot;Bruce&amp;quot;, &amp;quot;Jackie&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bruce, this is Jackie.&lt;br /&gt;
Jackie, meet Bruce.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You should be able to see how this works. When I call ''introduce'', person1 references the first name I give, and person2 references the second name I give. It's based on the ''order'' of the parameters/arguments. (Note some terminology here: person1 and person2 are the parameters; &amp;quot;Bruce&amp;quot; and &amp;quot;Jackie&amp;quot; are the arguments.)&lt;br /&gt;
&lt;br /&gt;
Here's a little, unimportant shortcut. We can declare more than one variable on the same line, provided they are of the same type:&lt;br /&gt;
&amp;lt;pre&amp;gt;var name1, name2 : string&amp;lt;/pre&amp;gt;&lt;br /&gt;
We can do the same with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1, person2 : string)&lt;br /&gt;
    %...&lt;br /&gt;
end introduce&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, I'll give another trivial example. This one will involve writing a procedure that takes two numbers and outputs their product.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure put_product (num1, num2 : real)&lt;br /&gt;
    put num1 * num2&lt;br /&gt;
end put_product&lt;br /&gt;
&lt;br /&gt;
put_product (4, 6)  % Outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, what we're doing is rather silly, isn't it? I mean, aside from the fact that we're defining a procedure to do multiplication, which we can do very quickly anyways. What's silly about this is that we've cornered ourselves. This procedure forces us to send the product to the standard output. What if we wanted to store it in a variable, or draw it in a pretty font, or pass it to some other procedure? Take a minute and think about how we might solve this problem. We want to be able to generalize, here. We want to write something that can multiply two numbers we give it, but still give us the flexibility to do what we want with the answer.&lt;br /&gt;
&lt;br /&gt;
If you're thinking about passing in another parameter that says what we are to do with the prodcut we calculate, then you're thinking way ahead of the game. It's possible, but not easy. There's an easier way.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing Functions==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure that outputs the product of two numbers, we'll write a function that computes the prodcut of two numbers and ''returns that value''. Then we can decide what to do with that number. That's what a function is: it's the same as a procedure, except it returns a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function product (num1, num2 : real) : real&lt;br /&gt;
    result num1 * num2&lt;br /&gt;
end product&lt;br /&gt;
&lt;br /&gt;
put product (4, 6)  % outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There's some new syntax there. First, we're using the keyword, &amp;quot;function&amp;quot;, instead of &amp;quot;procedure&amp;quot;. Next, there's a &amp;quot;: real&amp;quot; after our parameter list. That signifies what type of value our function will return. Also, we've used the '''result''' keyword. Earlier I said that functions return a value. The value returned is given by what is immediately after the '''result''' keyword.&lt;br /&gt;
&lt;br /&gt;
Using a function gives us more flexibility. We don't have to send our answer to the standard output. We could store it in a variable, for instance.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : real := product (1.5, 3)   % n := 4.5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Then we could use that elsewhere:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (n, 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or perhaps better yet:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (product 1.5, 3) 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's now look a little closer at '''result'''. It has an interesting behaviour that we have yet to examine.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Closer Look at 'result'==&lt;br /&gt;
&lt;br /&gt;
A function's sole purpose is to compute and return a value. With that in mind, let's write a ficticious function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function ficticious (number : int) : int&lt;br /&gt;
    result number + 1&lt;br /&gt;
    put &amp;quot;Yarr, it be a cold day.&amp;quot;&lt;br /&gt;
end ficticious&lt;br /&gt;
&lt;br /&gt;
% Call our function and store its answer in a variable&lt;br /&gt;
var n : int := ficticious (4)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
What has happened here? Surely, ''ficticious (4)'' returns the value 5. So ''n'' should be 5. What about that crazy pirate message? Well, as soon as the function computed its value, it returned that value and completed execution. The &amp;quot;Yarr...&amp;quot; message was never output; that line of code was never reached.&lt;br /&gt;
&lt;br /&gt;
This behaviour is a bit irratic. It can make it difficult to understand how a function is working. At the same time, it can be quite useful in shortening code. You've got a find a balance between short code and understandable code.&lt;br /&gt;
&lt;br /&gt;
Soon we will compare and contrast procedures and functions. Before we can do that, however, we need to re-examine what I said earlier about our parameter being constant.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable Parameters==&lt;br /&gt;
&lt;br /&gt;
Let's go back to our first example with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I had argued that ''name'' is constant. We cannot change its value. Indeed, we cannot. However, we can change this code so that ''name'' is in fact variable. All we have to do is specify that the ''name'' parameter is not simply a string, but is a variable that is a string. We do this by using the '''var''' keyword.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (var name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
put &amp;quot;After calling greet, the value of person is now &amp;quot;, person&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Hello George!&lt;br /&gt;
And hello to Freddy as well!&lt;br /&gt;
After calling greet, the value of person is now Freddy&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The difference here is that ''greet'' is expecting to take a string variable, instead of just a string. So when we pass ''person'' to it, ''name'' actually becomes a reference to the variable ''person''. Then, changing the value of ''name'' changes the value of ''person''.&lt;br /&gt;
&lt;br /&gt;
Now, we're familiar with procedures and functions. We now know enough to start thinking about when we should use a procedure and when we should use a function.&lt;br /&gt;
&lt;br /&gt;
==Functions vs. Procedures==&lt;br /&gt;
&lt;br /&gt;
Both functions and procedures are what some call &amp;quot;''subroutines''&amp;quot;--they are something we can use to store code and call at a later point. A function computes and returns a value, whereas a procedure just runs some code. We need to get an understanding of when to use a function and when to use a procedure. Let's look at a pretty famous case study.&lt;br /&gt;
&lt;br /&gt;
In Turing, there are two ways of generating a random integer. The first way is to use the built-in function, ''Rand.Int''. (This is a function just like what we've been writing so far. Don't let the fact that it has a period in it's name fool you.) ''Rand.Int'' takes two numbers, a low and a high; it produces a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
n := Rand.Int (4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now n is a random integer between 4 and 11.&lt;br /&gt;
&lt;br /&gt;
The second way to produce random integers is to use the procedure, ''randint''. This procedure first takes a variable (like our ''greet'' procedure did in the last section) and also takes two numbers, a low and a high. ''randint'' takes the variable you give it and sets it to a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, which is better: ''Rand.Int'' or ''randint''? The correct answer is ''Rand.Int''. Here's why:&lt;br /&gt;
&lt;br /&gt;
First, ''Rand.Int'' is more flexible. Say you wrote a function called &amp;quot;squeeze&amp;quot; that took in an integer. You want to give it a random integer between 1 and 6. Let's look at the two options:&lt;br /&gt;
&amp;lt;pre&amp;gt;squeeze (Rand.Int (1, 6))&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 1, 6)&lt;br /&gt;
squeeze (n)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Clearly the ''Rand.Int'' version is cleaner. There's no polluting the global namespace with useless variables like &amp;quot;''n''&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The second reason is a bit more theoretical, but is of equal importance. ''randint'' takes in a variable and does something to it. We don't really know what. We have trust the documentation. Now, we've also got to trust the documentation of ''Rand.Int''. However, there's an important difference. With ''Rand.Int'', we assign to our variable the value returned by the function. With ''randint'', we have given our precious variable to the procedure, along with all the permissions necessary for the procedure to slaughter our poor variable. It has complete control. It can write whatever it wants in there. We've suddenly given away control of the ''state'' of our program. (State is a term that collectively means the values of all our variables and also our current location in the execution of the code.) The fewer people we give this control to, the better.&lt;br /&gt;
&lt;br /&gt;
If you're not yet convinced, take a look at this. I'll code a version of ''randint'' for you.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure my_randint (var num : int, low, high : int)&lt;br /&gt;
    if num mod 2 = 0&lt;br /&gt;
        num := low + (num * 2) mod (high - low + 1)&lt;br /&gt;
    else&lt;br /&gt;
        num := low + (num + 2) mod (high - low + 1)&lt;br /&gt;
    end if&lt;br /&gt;
end my_randint&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So ''my_randint'' isn't really random, but it might fool you. Take a look at some sample data:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% I've taken a shortcut here. When I say&lt;br /&gt;
%   my_randint (0, 5, 10)&lt;br /&gt;
% I really mean&lt;br /&gt;
%   var n : int := 0&lt;br /&gt;
%   my_randint (n, 5, 10)&lt;br /&gt;
&lt;br /&gt;
my_randint (0, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (1, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (2, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (3, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (4, 5, 10)  %-&amp;gt; 7&lt;br /&gt;
my_randint (5, 5, 10)  %-&amp;gt; 6&lt;br /&gt;
my_randint (6, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (7, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (8, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (9, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (10, 5, 10) %-&amp;gt; 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I could not have done a similar thing if I were using a function, because I was never given a value for ''num''. (I know what some of you are thinking: &amp;quot;What if I give an uninitialized variable to my_randint? It'll crash.&amp;quot; It's possible that I could hack something together that could take care of this, but that's not the point.) The point of this is to show you that by giving the procedure control of your variable, it can use it that variable for evil purposes, as I have done with this ''my_randint'' procedure.&lt;br /&gt;
&lt;br /&gt;
I sincerely hope this has convinced you that functions are superior to procedures. I'm not saying that functions should be used exclusively. There are times when procedures are needed. Rather, I'm just saying that when you have a choice of using a function or using a procedure, choose the function. Nice functions are ones that compute a value based soley on the parameters they take. They don't rely on any global variables. These are functions that are easily documented. At the other extreme are procedures that use global variables inside their definition and also take in variables and modify them. These procedures are ''dangerous!'' and very hard to document.&lt;br /&gt;
&lt;br /&gt;
There is one last thing for us to discuss. It's a very powerful concept known as &amp;quot;recursion&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Recursion==&lt;br /&gt;
&lt;br /&gt;
I'd like to start this section off with a quotation:&lt;br /&gt;
&amp;lt;pre&amp;gt;To understand recursion, you must first understand recursion.&amp;lt;/pre&amp;gt;&lt;br /&gt;
A recursive subroutine is a subroutine that calls itself. One of the most famous and basic examples is the factorial function. The factorial function takes in a number and returns the product of that number and the factorial of the number minus one. Also, the factorial of one is defined to be one. This is the recursive definition of the function. It translates very easily into code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function factorial (n : int) : int&lt;br /&gt;
    if n = 1 then&lt;br /&gt;
        result 1&lt;br /&gt;
    else&lt;br /&gt;
        result n * factorial (n - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end factorial&lt;br /&gt;
&lt;br /&gt;
put factorial (3)  % Outputs &amp;quot;6&amp;quot;. Note 6 = 3*2*1&lt;br /&gt;
put factorial (5)  % Outputs &amp;quot;120&amp;quot;. Note 120 = 5*4*3*2*1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I will give a condensed trace of the factorial function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
factorial (5)&lt;br /&gt;
(5 * factorial (4))&lt;br /&gt;
(5 * (4 * factorial (3)))&lt;br /&gt;
(5 * (4 * (3 * factorial (2))))&lt;br /&gt;
(5 * (4 * (3 * (2 * factorial (1)))))&lt;br /&gt;
(5 * (4 * (3 * (2 * 1))))&lt;br /&gt;
(5 * (4 * (3 * 2)))&lt;br /&gt;
(5 * (4 * 6))&lt;br /&gt;
(5 * 24)&lt;br /&gt;
120&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I've given the recursive definition of the factorial function. A non-recursive definition is to say that factorial (n) = n * (n-1) * (n-2) * ... * 3 * 2 * 1. if you know about loops, you can code this definition of the factorial function. You can then compare and contrast the two definitions and see which you prefer. I hope you'll prefer this version, but that's for time to decide.&lt;br /&gt;
&lt;br /&gt;
As a segue into the next tutorial (which will be a tutorial on looping constructs), I will now present a recursive procedure that counts down to 1, and then displays, &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure countdown (high : int)&lt;br /&gt;
    if high = 0 then&lt;br /&gt;
        put &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
    else&lt;br /&gt;
        put high, &amp;quot;...&amp;quot;&lt;br /&gt;
        countdown (high - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end countdown&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Calling ''countdown (5)'' produces:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
5...&lt;br /&gt;
4...&lt;br /&gt;
3...&lt;br /&gt;
2...&lt;br /&gt;
1...&lt;br /&gt;
Blast off!!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Question One!''' Create a guessing game. Your program will have a global variable that represents the number the user is trying to guess. Use recursion to write a procedure called ''guess''. When run, ''guess'' prompts the user to input a number. If the guessed number is equal to ''the'' number, a winning message is displayed. Otherwise, the user is told whether his/her guess was too high or too low, and is prompted to guess another number.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Two!''' Define a function called ''add'' that takes two natural numbers and returns their sum. There's a catch, though. You're not allowed to use the + operator. However, you are allowed to use these two functions that I supply you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function add1 (n : int) : int&lt;br /&gt;
    result n + 1&lt;br /&gt;
end add1&lt;br /&gt;
function sub1 (n : int) : int&lt;br /&gt;
    result n - 1&lt;br /&gt;
end sub1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Three''' Define a function called ''multiply'' that takes two natural numbers and returns their product. Once again, there's a catch. You're not allowed to use the built in * operator. (You are allowed to use the + operator though.) You can, however, use the following two functions that I provide you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function double (n : int) : int&lt;br /&gt;
    result n + n&lt;br /&gt;
end double&lt;br /&gt;
&lt;br /&gt;
% The halve function performs integer division by 2.&lt;br /&gt;
% Examples: halve (8) -&amp;gt; 4&lt;br /&gt;
%           halve (17) -&amp;gt; 8&lt;br /&gt;
function halve (n : int) : int&lt;br /&gt;
    result floor (n / 2)&lt;br /&gt;
end halve&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Hint: You should use the binary definition of a natural number. That is, a natural number is one of&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*2*k where k is a natural number&lt;br /&gt;
*2*k + 1 where k is a natural number&lt;br /&gt;
&lt;br /&gt;
That is why I gave you the double and halve functions. It's possible to write a solution using the unary definition of a natural number (sort of like in the solution to problem two), but it will be much slower than the solution using the binary definition.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
I hope you find these exercises in recursion interesting. At this point, we've learned a small amount of the Turing language, but you see we can do some pretty powerful things using this concept of recursion.&lt;br /&gt;
&lt;br /&gt;
Even if we don't take advantage of recursion, however, the topics covered in this tutorial still give us considerable power. They allow us to organize our code. We can save ourselves from repeating ourselves. We can generalize by using parameters. Indeed, the rather simple idea of factoring out common code has led to powerful advances.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
&lt;br /&gt;
===Problem One===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var the_num := Rand.Int (1, 100)&lt;br /&gt;
&lt;br /&gt;
proc guess&lt;br /&gt;
    put &amp;quot;Please input a guess: &amp;quot; ..&lt;br /&gt;
    var g : int&lt;br /&gt;
    get g&lt;br /&gt;
    if g = the_num then&lt;br /&gt;
        put &amp;quot;You've got it!&amp;quot;&lt;br /&gt;
    elsif g &amp;lt; the_num then&lt;br /&gt;
        put &amp;quot;You've guessed too low. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    else&lt;br /&gt;
        put &amp;quot;You've guessed too high. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    end if&lt;br /&gt;
end guess&lt;br /&gt;
&lt;br /&gt;
guess&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Two===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by increasing n2 by one as we &lt;br /&gt;
% decrease n1 by one. If n2 = 0, then we just return n1.&lt;br /&gt;
% Essentially, it works on the following identity:&lt;br /&gt;
%    n1 + n2 = (n1 + 1) + (n2 - 1)&lt;br /&gt;
function add (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 0 then&lt;br /&gt;
        result n1&lt;br /&gt;
    else&lt;br /&gt;
        result add (add1 (n1), sub1 (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end add&lt;br /&gt;
&lt;br /&gt;
% Note that this function could be made more efficient if, instead &lt;br /&gt;
% of blindly choosing to decrease n2 and increase n1, we chose to&lt;br /&gt;
% decrease the smaller of n1 and n2 and increase the larger of n1 and n2.&lt;br /&gt;
% Try writing another function that calls this add function in that way.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Three===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by decreasing n2 and increasing n1.&lt;br /&gt;
% It works on the following identity:&lt;br /&gt;
% If n2 is even:  n1 * n2 = double (n1) * halve (n2)&lt;br /&gt;
% If n2 is odd:   n1 * n2 = n1 + double (n1) * halve (n2)&lt;br /&gt;
function multiply (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 1 then&lt;br /&gt;
        result n1&lt;br /&gt;
    elsif n2 mod 2 = 0 then              % n2 is even&lt;br /&gt;
        result multiply (double (n1), halve (n2))&lt;br /&gt;
    else                                 % n2 is odd&lt;br /&gt;
        result n1 + multiply (double (n1), halve (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end multiply&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Credits==&lt;br /&gt;
Authour: [[Cervantes]]&lt;br /&gt;
&lt;br /&gt;
Added by: [[Saad]]&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_Joysticks</id>
		<title>Turing Joysticks</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_Joysticks"/>
				<updated>2008-10-12T20:24:21Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: /* Body */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Hello welcome to my tutorial on using joysticks/joypads. I personally have a joypad with 2 analog sticks, 12 buttons, and a D-PAD/POV which has been tested. This tutorial will require two things: Firstly, you have atleast one joystick/joypad. Secondly, you grab my revised module at the bottom as the Turing default will not work. Now for some Q and A.&lt;br /&gt;
&lt;br /&gt;
==FAQS==&lt;br /&gt;
&lt;br /&gt;
q)So what do you plan to accomplish in this tutorial?&lt;br /&gt;
&lt;br /&gt;
a)Well I'm planning to tackle the four main parts of the joystick module, breaking it down into parts, and ultimatly sharing this method of input with the world!&lt;br /&gt;
&lt;br /&gt;
q)So what's so great about joysticks/joypads?&lt;br /&gt;
&lt;br /&gt;
a)Well the mouse has upto 3 buttons and a cursor, a keyboard has keys, however a joypad (depending on the model) will have upto 32 buttons, upto 3 analog sticks, and a D-PAD/POV! that equals a ton more input and functionallity.&lt;br /&gt;
&lt;br /&gt;
q)What if I don't have a joypad/joystick?&lt;br /&gt;
&lt;br /&gt;
a)Not to worry just follow along and post/PM me with the questions (I'm sometimes busy so results not will be immediate).&lt;br /&gt;
&lt;br /&gt;
Well now to get started!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Setup==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
First things first, since the module is not pervasive it needs to be imported, also make sure the module is in the same folder as the program using it.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
import joystick&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now that that is done varibles need to be initalized just as though mouse input or keyboard input were being used.&lt;br /&gt;
Code:&lt;br /&gt;
&amp;lt;pre&amp;gt;type joypad : % create a joypad type&lt;br /&gt;
    record&lt;br /&gt;
        button : array 1 .. 32 of boolean %create 32 instances of buttons&lt;br /&gt;
        pos : joystick.PosRecord /*used to find all joypad intake with&lt;br /&gt;
         the exception of buttons*/&lt;br /&gt;
        caps : joystick.CapsRecord %used to find the max on joypad analog&lt;br /&gt;
    end record&lt;br /&gt;
&lt;br /&gt;
var joy : array 1 .. 2 of joypad %create an array of joypad&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What just happened? Well now to break that down.&lt;br /&gt;
&lt;br /&gt;
First an instance of the joypad type containing a set of varibles was created. The first varible is an array of 32 boolean the number can be any number 1&amp;lt;n&amp;lt;32, basically it's all the buttons being used (32buttons were used to show the maximum potential). The next varible is actually taking the type of another record.&lt;br /&gt;
The record can be found in the joystick module it is basically bringing all input except the button states. Next is another type found in the joystick module this time bringing back the maximum potential of the joystick/joypad. Then a joy varible to use the varibles was created an array can be used for multiple joysticks/joypads.&lt;br /&gt;
&lt;br /&gt;
Well now that is done time to move on. Default values need to be assigned as a failsafe for any joysticks not detected.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&amp;lt;pre&amp;gt;/*set default joypad varibles*/&lt;br /&gt;
for i : 1 .. upper (joy)&lt;br /&gt;
    for ii : 1 .. upper (joy (i).button)&lt;br /&gt;
        joy (i).button (ii) := false&lt;br /&gt;
    end for&lt;br /&gt;
    joy (i).pos.POV := 66535&lt;br /&gt;
    joystick.Capabilities (i - 1, joy (i).caps)&lt;br /&gt;
end for&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What just happened is rather simple. First a for loop was created to to cover all elements of joy, then another for loop for all 32 buttons to set the array to false false. Then the D-PAD/POV' was set to the default value of 66535. Lastly the joystick capibilities was called to find maximum capiblities of the joystick/joypad. Next joystick needs to be galled upon each loop run in the main program to gather information to what buttons are being pressed, etc... just as if it were a mouse or keyboard.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&amp;lt;pre&amp;gt;joystick.GetInfo (joystick[1/2], joy ([1/2]).button)&lt;br /&gt;
joystick.Read (joystick[1/2], joy ([1/2]).pos)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The GetInfo procedure will return upto 32 button values. The Read procedure will return with Analog and D-PAD/POV information.&lt;br /&gt;
&lt;br /&gt;
==Using Buttons==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This part of the tutorial is similar to a keyboard. A keyboard has an array of keys that return true if the key is pressed (info found with Input.KeyDown). A joystick/joypad has an array of buttons that return true if the button is pressed (info found with joystick.GetInfo). Therefore:&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&amp;lt;pre&amp;gt;if joy.button(4) then&lt;br /&gt;
    put &amp;quot;Button 4 is being pressed&amp;quot;&lt;br /&gt;
end if&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Hopefully that is understandable. And that is all for buttons.&lt;br /&gt;
&lt;br /&gt;
==Using Analog Sticks==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This part is not to hard however the analog does not return an X/Y pixel value, so a conversion is needed. First to get the current analog position.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&amp;lt;pre&amp;gt;put &amp;quot;analog 1 - x - &amp;quot;, joy (1).pos.xpos&lt;br /&gt;
put &amp;quot;analog 1 - y - &amp;quot;, joy (1).pos.ypos&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Well these values need to be converted like so.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;put &amp;quot;analog 1 - x - &amp;quot;, round (joy (joystickNO + 1).pos.xpos / joy (joystickNO + 1).caps.maxX * maxx)&lt;br /&gt;
put &amp;quot;analog 1 - y - &amp;quot;, round (joy (joystickNO + 1).pos.ypos / joy (joystickNO + 1).caps.maxY * maxy)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This must be done everytime but it shouldn't be to hard to create a function to do this.&lt;br /&gt;
&lt;br /&gt;
The axis use a name for each x and y direction: analog 1 x - xpos, analog 1 y - ypos, analog 2 x - zpos, analog 2 y - rpos, analog 3 x - upos, analog 3 y - vpos.&lt;br /&gt;
&lt;br /&gt;
==Using D-PAD/POV==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
First off, reading D-PAD/POV data.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&amp;lt;pre&amp;gt;put joy ([1/2]).pos.POV&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This to needs to be converted into usable code.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&amp;lt;pre&amp;gt; put &amp;quot;D-PAD (AKA POV) - &amp;quot; ..&lt;br /&gt;
if joy (    [ 1 / 2]).pos.POV = 0 then&lt;br /&gt;
    put &amp;quot;up&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2]).pos.POV = 4500 then&lt;br /&gt;
    put &amp;quot;up right&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2 ]).pos.POV = 9000 then&lt;br /&gt;
    put &amp;quot;right&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2 ]).pos.POV = 13500 then&lt;br /&gt;
    put &amp;quot;down right&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2 ]).pos.POV = 18000 then&lt;br /&gt;
    put &amp;quot;down&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2  ]).pos.POV = 22500 then&lt;br /&gt;
    put &amp;quot;down left&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2 ]).pos.POV = 27000 then&lt;br /&gt;
    put &amp;quot;left&amp;quot;&lt;br /&gt;
elsif joy (    [ 1 / 2  ]).pos.POV = 31500 then&lt;br /&gt;
    put &amp;quot;up left&amp;quot;&lt;br /&gt;
else&lt;br /&gt;
    put &amp;quot;none&amp;quot;&lt;br /&gt;
end if&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Well that is all there is to it.&lt;br /&gt;
&lt;br /&gt;
==Challenges==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here are some challenges to try out.&lt;br /&gt;
&lt;br /&gt;
1)make a circle that moves with the analog stick&lt;br /&gt;
&lt;br /&gt;
2)move a sprite around the screen with the D-PAD/POV&lt;br /&gt;
&lt;br /&gt;
3)make a simple Simon game with the buttons&lt;br /&gt;
&lt;br /&gt;
4)try to make a basic game where you talk to people, walking around with D-PAD/POV talking to the character with the buttons&lt;br /&gt;
&lt;br /&gt;
==Refrences==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
1) joystick info http://compsci.ca/v3/viewtopic.php?t=6373&lt;br /&gt;
&lt;br /&gt;
2) joystick info http://compsci.ca/v3/viewtopic.php?t=169&lt;br /&gt;
&lt;br /&gt;
And I am terribly sorry but there was one another post that really helped me understand joysticks myself but I forget who posted it and I can no longer find the post.&lt;br /&gt;
&lt;br /&gt;
==Downloads==&lt;br /&gt;
&lt;br /&gt;
[http://compsci.ca/v3/download.php?id=5324&amp;gt;Joystick Module]&lt;br /&gt;
&lt;br /&gt;
[http://compsci.ca/v3/download.php?id=5325&amp;gt;Demonstration]&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures</id>
		<title>Turing Functions and Procedures</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures"/>
				<updated>2008-10-12T19:39:31Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: /* Functions and Procedures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Functions and Procedures==&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Thus far, we've learned enough of the Turing language to be able to write some decently large programs. We've learned some fundamentals of Turing -- variables, basic input/output, and if statements. Indeed, these concepts translate relatively smoothly to most any other programming language you might learn. So, having mastered these basics, what's next? Well, having a way to organize our code would be good. Right now, whatever code you write runs linearly, from top to bottom. If you want to run a section of code twice, you have to type it out twice. Wouldn't it be great if we could factor out the commonalities in our code, so we can write less? In truth, we haven't learned very much, so it might be difficult to see the advantage of this. Just bear with me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing the Procedure==&lt;br /&gt;
&lt;br /&gt;
At its simplest, a procedure is just some lines of Turing code. We wrap some special syntax around our lines of Turing code and think up a name for them. After we have defined our procedure, we can ''call'' the procedure by saying its name. When we do this, we run those lines of Turing code that are wrapped inside the procedure definition. This is easiest to see with an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So now the single line of code, ''put &amp;quot;Hello world!&amp;quot;'', can be executed any time we call the ''greet'' procedure. The above code is identical to the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
All I've done is substituted the code inside the ''greet'' procedure for the call to ''greet'' in the last line of the original program.&lt;br /&gt;
&lt;br /&gt;
So, why is this useful? It's useful because now whenever we want to output &amp;quot;Hello world!&amp;quot;, we don't have to type, ''put &amp;quot;Hello world!&amp;quot;'', but only ''greet''. Okay, so we saved on typing fourteen charactes. Not a big deal, right? But what if we wrote a program that said &amp;quot;Hello world!&amp;quot; a lot. Our program said hello to the world ten times during the course of its execution. Now, we're looking back at our program and thinking, &amp;quot;You know, 'Hello world!' isn't really what we want to say. We really are only talking to one person--George. We should instead say 'Hello George!'.&amp;quot; So, now we've got to go into our code and change it. If we used a ''greet'' procedure, this change involves changing the definition of the procedure so that it becomes the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello George!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we hadn't used this procedure, we would have to go through our code, searching for any time we said &amp;quot;Hello world!&amp;quot; and change it to be &amp;quot;Hello George!&amp;quot;. That's a lot of unnecessary work, relative to how effective our ''greet'' solution is.&lt;br /&gt;
&lt;br /&gt;
But now, what if we aren't always talking to George?&lt;br /&gt;
&lt;br /&gt;
==Procedures with Parameters==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure to greet to George and another procedure to greet to Phil and another to greet to Anthony, we should try to write a procedure that can greet to anyone. Here's one possibility:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name : string&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;George&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Phil&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Anthony&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This works, but it's clunky. We've got that variable up there, ''name''. It's what we call a '''global variable'''. It's called that because it is global to the whole program. No matter where we are in our code, we can always see the ''name'' variable. Really, we don't need to keep track of ''name''. We only need it ''inside the greet procedure''. Here's where '''parameters''' are used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Phil&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Anthony&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the definition of our greet procedure, I added that extra bit at the end--the stuff in parentheses. That says that when we call the greet procedure, we must give it one '''argument''', and that argument must be a string. It also says that for all code inside the procedure, there exists a '''constant''' called &amp;quot;name&amp;quot; that has the value we give it when we call the procedure. So, when we call ''greet (&amp;quot;George&amp;quot;)'', we are setting ''name'' to be &amp;quot;George&amp;quot;, and we execute the code inside the procedure. The following two pieces of code are identical:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, &amp;quot;George&amp;quot;, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I mentioned earlier that inside the ''greet'' procedure there exists this constant called name. There are two things we need to understand about it. First, it is constant. We can't change the value of name. Let's get some example code and figure out why:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This code fails because, inside ''greet'', ''name'' is really just a ''reference to &amp;quot;George&amp;quot;''. When we try to redefine name, we're really trying to redefine the string literal &amp;quot;George&amp;quot;. Of course, that's impossible. What if we did this, though?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now you might think that, inside ''greet'', ''name'' is a reference to ''person'', which is a variable, so attempting to redefine ''name'' is really redefining ''person'', and that's okay. However, it's not quite that simple. See, ''greet'' expects a string as its argument, not a variable. In ''greet (person)'', ''person'' is an expression. It gets evaluated down to a string value; it gets evaluated to &amp;quot;George&amp;quot;. Now, once again, we're passing the string literal &amp;quot;George&amp;quot; into ''greet'', and we're back at our first situation.&lt;br /&gt;
&lt;br /&gt;
The second thing we need to understand about ''name'' is that it's '''local'''. It exists only within the procedure. The following code fails:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
put name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''name'' doesn't exist outside of the ''greet'' procedure. It's '''local'''. It's '''scope''' is the ''greet'' procedure. You should also note that each time we call ''greet'' we are creating a new ''name'' constant.&lt;br /&gt;
&lt;br /&gt;
What if we wanted to write a procedure that introduces two people to each other? We need a procedure that can two arguments, not one.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Procedures with Multiple Parameters==&lt;br /&gt;
&lt;br /&gt;
I will now define a procedure, called ''introduce'', that introduces two people to each other:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1 : string, person2 : string)&lt;br /&gt;
    put person1, &amp;quot;, this is &amp;quot;, person2, &amp;quot;.&amp;quot;&lt;br /&gt;
    put person2, &amp;quot;, meet &amp;quot;, person1, &amp;quot;.&amp;quot;&lt;br /&gt;
end introduce&lt;br /&gt;
&lt;br /&gt;
% Call the procedure&lt;br /&gt;
introduce (&amp;quot;Jackie&amp;quot;, &amp;quot;Bruce&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output of this program will be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Jackie, this is Bruce.&lt;br /&gt;
Bruce, meet Jackie.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, reversing the order of the names results in a different output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
introduce (&amp;quot;Bruce&amp;quot;, &amp;quot;Jackie&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bruce, this is Jackie.&lt;br /&gt;
Jackie, meet Bruce.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You should be able to see how this works. When I call ''introduce'', person1 references the first name I give, and person2 references the second name I give. It's based on the ''order'' of the parameters/arguments. (Note some terminology here: person1 and person2 are the parameters; &amp;quot;Bruce&amp;quot; and &amp;quot;Jackie&amp;quot; are the arguments.)&lt;br /&gt;
&lt;br /&gt;
Here's a little, unimportant shortcut. We can declare more than one variable on the same line, provided they are of the same type:&lt;br /&gt;
&amp;lt;pre&amp;gt;var name1, name2 : string&amp;lt;/pre&amp;gt;&lt;br /&gt;
We can do the same with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1, person2 : string)&lt;br /&gt;
    %...&lt;br /&gt;
end introduce&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, I'll give another trivial example. This one will involve writing a procedure that takes two numbers and outputs their product.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure put_product (num1, num2 : real)&lt;br /&gt;
    put num1 * num2&lt;br /&gt;
end put_product&lt;br /&gt;
&lt;br /&gt;
put_product (4, 6)  % Outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, what we're doing is rather silly, isn't it? I mean, aside from the fact that we're defining a procedure to do multiplication, which we can do very quickly anyways. What's silly about this is that we've cornered ourselves. This procedure forces us to send the product to the standard output. What if we wanted to store it in a variable, or draw it in a pretty font, or pass it to some other procedure? Take a minute and think about how we might solve this problem. We want to be able to generalize, here. We want to write something that can multiply two numbers we give it, but still give us the flexibility to do what we want with the answer.&lt;br /&gt;
&lt;br /&gt;
If you're thinking about passing in another parameter that says what we are to do with the prodcut we calculate, then you're thinking way ahead of the game. It's possible, but not easy. There's an easier way.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing Functions==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure that outputs the product of two numbers, we'll write a function that computes the prodcut of two numbers and ''returns that value''. Then we can decide what to do with that number. That's what a function is: it's the same as a procedure, except it returns a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function product (num1, num2 : real) : real&lt;br /&gt;
    result num1 * num2&lt;br /&gt;
end product&lt;br /&gt;
&lt;br /&gt;
put product (4, 6)  % outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There's some new syntax there. First, we're using the keyword, &amp;quot;function&amp;quot;, instead of &amp;quot;procedure&amp;quot;. Next, there's a &amp;quot;: real&amp;quot; after our parameter list. That signifies what type of value our function will return. Also, we've used the '''result''' keyword. Earlier I said that functions return a value. The value returned is given by what is immediately after the '''result''' keyword.&lt;br /&gt;
&lt;br /&gt;
Using a function gives us more flexibility. We don't have to send our answer to the standard output. We could store it in a variable, for instance.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : real := product (1.5, 3)   % n := 4.5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Then we could use that elsewhere:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (n, 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or perhaps better yet:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (product 1.5, 3) 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's now look a little closer at '''result'''. It has an interesting behaviour that we have yet to examine.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Closer Look at 'result'==&lt;br /&gt;
&lt;br /&gt;
A function's sole purpose is to compute and return a value. With that in mind, let's write a ficticious function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function ficticious (number : int) : int&lt;br /&gt;
    result number + 1&lt;br /&gt;
    put &amp;quot;Yarr, it be a cold day.&amp;quot;&lt;br /&gt;
end ficticious&lt;br /&gt;
&lt;br /&gt;
% Call our function and store its answer in a variable&lt;br /&gt;
var n : int := ficticious (4)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
What has happened here? Surely, ''ficticious (4)'' returns the value 5. So ''n'' should be 5. What about that crazy pirate message? Well, as soon as the function computed its value, it returned that value and completed execution. The &amp;quot;Yarr...&amp;quot; message was never output; that line of code was never reached.&lt;br /&gt;
&lt;br /&gt;
This behaviour is a bit irratic. It can make it difficult to understand how a function is working. At the same time, it can be quite useful in shortening code. You've got a find a balance between short code and understandable code.&lt;br /&gt;
&lt;br /&gt;
Soon we will compare and contrast procedures and functions. Before we can do that, however, we need to re-examine what I said earlier about our parameter being constant.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable Parameters==&lt;br /&gt;
&lt;br /&gt;
Let's go back to our first example with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I had argued that ''name'' is constant. We cannot change its value. Indeed, we cannot. However, we can change this code so that ''name'' is in fact variable. All we have to do is specify that the ''name'' parameter is not simply a string, but is a variable that is a string. We do this by using the '''var''' keyword.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (var name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
put &amp;quot;After calling greet, the value of person is now &amp;quot;, person&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Hello George!&lt;br /&gt;
And hello to Freddy as well!&lt;br /&gt;
After calling greet, the value of person is now Freddy&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The difference here is that ''greet'' is expecting to take a string variable, instead of just a string. So when we pass ''person'' to it, ''name'' actually becomes a reference to the variable ''person''. Then, changing the value of ''name'' changes the value of ''person''.&lt;br /&gt;
&lt;br /&gt;
Now, we're familiar with procedures and functions. We now know enough to start thinking about when we should use a procedure and when we should use a function.&lt;br /&gt;
&lt;br /&gt;
==Functions vs. Procedures==&lt;br /&gt;
&lt;br /&gt;
Both functions and procedures are what some call &amp;quot;''subroutines''&amp;quot;--they are something we can use to store code and call at a later point. A function computes and returns a value, whereas a procedure just runs some code. We need to get an understanding of when to use a function and when to use a procedure. Let's look at a pretty famous case study.&lt;br /&gt;
&lt;br /&gt;
In Turing, there are two ways of generating a random integer. The first way is to use the built-in function, ''Rand.Int''. (This is a function just like what we've been writing so far. Don't let the fact that it has a period in it's name fool you.) ''Rand.Int'' takes two numbers, a low and a high; it produces a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
n := Rand.Int (4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now n is a random integer between 4 and 11.&lt;br /&gt;
&lt;br /&gt;
The second way to produce random integers is to use the procedure, ''randint''. This procedure first takes a variable (like our ''greet'' procedure did in the last section) and also takes two numbers, a low and a high. ''randint'' takes the variable you give it and sets it to a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, which is better: ''Rand.Int'' or ''randint''? The correct answer is ''Rand.Int''. Here's why:&lt;br /&gt;
&lt;br /&gt;
First, ''Rand.Int'' is more flexible. Say you wrote a function called &amp;quot;squeeze&amp;quot; that took in an integer. You want to give it a random integer between 1 and 6. Let's look at the two options:&lt;br /&gt;
&amp;lt;pre&amp;gt;squeeze (Rand.Int (1, 6))&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 1, 6)&lt;br /&gt;
squeeze (n)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Clearly the ''Rand.Int'' version is cleaner. There's no polluting the global namespace with useless variables like &amp;quot;''n''&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The second reason is a bit more theoretical, but is of equal importance. ''randint'' takes in a variable and does something to it. We don't really know what. We have trust the documentation. Now, we've also got to trust the documentation of ''Rand.Int''. However, there's an important difference. With ''Rand.Int'', we assign to our variable the value returned by the function. With ''randint'', we have given our precious variable to the procedure, along with all the permissions necessary for the procedure to slaughter our poor variable. It has complete control. It can write whatever it wants in there. We've suddenly given away control of the ''state'' of our program. (State is a term that collectively means the values of all our variables and also our current location in the execution of the code.) The fewer people we give this control to, the better.&lt;br /&gt;
&lt;br /&gt;
If you're not yet convinced, take a look at this. I'll code a version of ''randint'' for you.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure my_randint (var num : int, low, high : int)&lt;br /&gt;
    if num mod 2 = 0&lt;br /&gt;
        num := low + (num * 2) mod (high - low + 1)&lt;br /&gt;
    else&lt;br /&gt;
        num := low + (num + 2) mod (high - low + 1)&lt;br /&gt;
    end if&lt;br /&gt;
end my_randint&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So ''my_randint'' isn't really random, but it might fool you. Take a look at some sample data:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% I've taken a shortcut here. When I say&lt;br /&gt;
%   my_randint (0, 5, 10)&lt;br /&gt;
% I really mean&lt;br /&gt;
%   var n : int := 0&lt;br /&gt;
%   my_randint (n, 5, 10)&lt;br /&gt;
&lt;br /&gt;
my_randint (0, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (1, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (2, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (3, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (4, 5, 10)  %-&amp;gt; 7&lt;br /&gt;
my_randint (5, 5, 10)  %-&amp;gt; 6&lt;br /&gt;
my_randint (6, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (7, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (8, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (9, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (10, 5, 10) %-&amp;gt; 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I could not have done a similar thing if I were using a function, because I was never given a value for ''num''. (I know what some of you are thinking: &amp;quot;What if I give an uninitialized variable to my_randint? It'll crash.&amp;quot; It's possible that I could hack something together that could take care of this, but that's not the point.) The point of this is to show you that by giving the procedure control of your variable, it can use it that variable for evil purposes, as I have done with this ''my_randint'' procedure.&lt;br /&gt;
&lt;br /&gt;
I sincerely hope this has convinced you that functions are superior to procedures. I'm not saying that functions should be used exclusively. There are times when procedures are needed. Rather, I'm just saying that when you have a choice of using a function or using a procedure, choose the function. Nice functions are ones that compute a value based soley on the parameters they take. They don't rely on any global variables. These are functions that are easily documented. At the other extreme are procedures that use global variables inside their definition and also take in variables and modify them. These procedures are ''dangerous!'' and very hard to document.&lt;br /&gt;
&lt;br /&gt;
There is one last thing for us to discuss. It's a very powerful concept known as &amp;quot;recursion&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Recursion==&lt;br /&gt;
&lt;br /&gt;
I'd like to start this section off with a quotation:&lt;br /&gt;
&amp;lt;pre&amp;gt;To understand recursion, you must first understand recursion.&amp;lt;/pre&amp;gt;&lt;br /&gt;
A recursive subroutine is a subroutine that calls itself. One of the most famous and basic examples is the factorial function. The factorial function takes in a number and returns the product of that number and the factorial of the number minus one. Also, the factorial of one is defined to be one. This is the recursive definition of the function. It translates very easily into code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function factorial (n : int) : int&lt;br /&gt;
    if n = 1 then&lt;br /&gt;
        result 1&lt;br /&gt;
    else&lt;br /&gt;
        result n * factorial (n - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end factorial&lt;br /&gt;
&lt;br /&gt;
put factorial (3)  % Outputs &amp;quot;6&amp;quot;. Note 6 = 3*2*1&lt;br /&gt;
put factorial (5)  % Outputs &amp;quot;120&amp;quot;. Note 120 = 5*4*3*2*1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I will give a condensed trace of the factorial function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
factorial (5)&lt;br /&gt;
(5 * factorial (4))&lt;br /&gt;
(5 * (4 * factorial (3)))&lt;br /&gt;
(5 * (4 * (3 * factorial (2))))&lt;br /&gt;
(5 * (4 * (3 * (2 * factorial (1)))))&lt;br /&gt;
(5 * (4 * (3 * (2 * 1))))&lt;br /&gt;
(5 * (4 * (3 * 2)))&lt;br /&gt;
(5 * (4 * 6))&lt;br /&gt;
(5 * 24)&lt;br /&gt;
120&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I've given the recursive definition of the factorial function. A non-recursive definition is to say that factorial (n) = n * (n-1) * (n-2) * ... * 3 * 2 * 1. if you know about loops, you can code this definition of the factorial function. You can then compare and contrast the two definitions and see which you prefer. I hope you'll prefer this version, but that's for time to decide.&lt;br /&gt;
&lt;br /&gt;
As a segue into the next tutorial (which will be a tutorial on looping constructs), I will now present a recursive procedure that counts down to 1, and then displays, &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure countdown (high : int)&lt;br /&gt;
    if high = 0 then&lt;br /&gt;
        put &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
    else&lt;br /&gt;
        put high, &amp;quot;...&amp;quot;&lt;br /&gt;
        countdown (high - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end countdown&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Calling ''countdown (5)'' produces:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
5...&lt;br /&gt;
4...&lt;br /&gt;
3...&lt;br /&gt;
2...&lt;br /&gt;
1...&lt;br /&gt;
Blast off!!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Question One!''' Create a guessing game. Your program will have a global variable that represents the number the user is trying to guess. Use recursion to write a procedure called ''guess''. When run, ''guess'' prompts the user to input a number. If the guessed number is equal to ''the'' number, a winning message is displayed. Otherwise, the user is told whether his/her guess was too high or too low, and is prompted to guess another number.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Two!''' Define a function called ''add'' that takes two natural numbers and returns their sum. There's a catch, though. You're not allowed to use the + operator. However, you are allowed to use these two functions that I supply you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function add1 (n : int) : int&lt;br /&gt;
    result n + 1&lt;br /&gt;
end add1&lt;br /&gt;
function sub1 (n : int) : int&lt;br /&gt;
    result n - 1&lt;br /&gt;
end sub1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Three''' Define a function called ''multiply'' that takes two natural numbers and returns their product. Once again, there's a catch. You're not allowed to use the built in * operator. (You are allowed to use the + operator though.) You can, however, use the following two functions that I provide you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function double (n : int) : int&lt;br /&gt;
    result n + n&lt;br /&gt;
end double&lt;br /&gt;
&lt;br /&gt;
% The halve function performs integer division by 2.&lt;br /&gt;
% Examples: halve (8) -&amp;gt; 4&lt;br /&gt;
%           halve (17) -&amp;gt; 8&lt;br /&gt;
function halve (n : int) : int&lt;br /&gt;
    result floor (n / 2)&lt;br /&gt;
end halve&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Hint: You should use the binary definition of a natural number. That is, a natural number is one of&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*2*k where k is a natural number&lt;br /&gt;
*2*k + 1 where k is a natural number&lt;br /&gt;
&lt;br /&gt;
That is why I gave you the double and halve functions. It's possible to write a solution using the unary definition of a natural number (sort of like in the solution to problem two), but it will be much slower than the solution using the binary definition.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
I hope you find these exercises in recursion interesting. At this point, we've learned a small amount of the Turing language, but you see we can do some pretty powerful things using this concept of recursion.&lt;br /&gt;
&lt;br /&gt;
Even if we don't take advantage of recursion, however, the topics covered in this tutorial still give us considerable power. They allow us to organize our code. We can save ourselves from repeating ourselves. We can generalize by using parameters. Indeed, the rather simple idea of factoring out common code has led to powerful advances.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
&lt;br /&gt;
===Problem One===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var the_num := Rand.Int (1, 100)&lt;br /&gt;
&lt;br /&gt;
proc guess&lt;br /&gt;
    put &amp;quot;Please input a guess: &amp;quot; ..&lt;br /&gt;
    var g : int&lt;br /&gt;
    get g&lt;br /&gt;
    if g = the_num then&lt;br /&gt;
        put &amp;quot;You've got it!&amp;quot;&lt;br /&gt;
    elsif g &amp;lt; the_num then&lt;br /&gt;
        put &amp;quot;You've guessed too low. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    else&lt;br /&gt;
        put &amp;quot;You've guessed too high. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    end if&lt;br /&gt;
end guess&lt;br /&gt;
&lt;br /&gt;
guess&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Two===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by increasing n2 by one as we &lt;br /&gt;
% decrease n1 by one. If n2 = 0, then we just return n1.&lt;br /&gt;
% Essentially, it works on the following identity:&lt;br /&gt;
%    n1 + n2 = (n1 + 1) + (n2 - 1)&lt;br /&gt;
function add (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 0 then&lt;br /&gt;
        result n1&lt;br /&gt;
    else&lt;br /&gt;
        result add (add1 (n1), sub1 (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end add&lt;br /&gt;
&lt;br /&gt;
% Note that this function could be made more efficient if, instead &lt;br /&gt;
% of blindly choosing to decrease n2 and increase n1, we chose to&lt;br /&gt;
% decrease the smaller of n1 and n2 and increase the larger of n1 and n2.&lt;br /&gt;
% Try writing another function that calls this add function in that way.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Three===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by decreasing n2 and increasing n1.&lt;br /&gt;
% It works on the following identity:&lt;br /&gt;
% If n2 is even:  n1 * n2 = double (n1) * halve (n2)&lt;br /&gt;
% If n2 is odd:   n1 * n2 = n1 + double (n1) * halve (n2)&lt;br /&gt;
function multiply (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 1 then&lt;br /&gt;
        result n1&lt;br /&gt;
    elsif n2 mod 2 = 0 then              % n2 is even&lt;br /&gt;
        result multiply (double (n1), halve (n2))&lt;br /&gt;
    else                                 % n2 is odd&lt;br /&gt;
        result n1 + multiply (double (n1), halve (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end multiply&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Credits==&lt;br /&gt;
Authour: [[Cervantes]]&lt;br /&gt;
&lt;br /&gt;
Added by: [[Saad]]&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures</id>
		<title>Turing Functions and Procedures</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures"/>
				<updated>2008-10-12T19:39:09Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: /* Functions and Procedures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Functions and Procedures=&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Thus far, we've learned enough of the Turing language to be able to write some decently large programs. We've learned some fundamentals of Turing -- variables, basic input/output, and if statements. Indeed, these concepts translate relatively smoothly to most any other programming language you might learn. So, having mastered these basics, what's next? Well, having a way to organize our code would be good. Right now, whatever code you write runs linearly, from top to bottom. If you want to run a section of code twice, you have to type it out twice. Wouldn't it be great if we could factor out the commonalities in our code, so we can write less? In truth, we haven't learned very much, so it might be difficult to see the advantage of this. Just bear with me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing the Procedure==&lt;br /&gt;
&lt;br /&gt;
At its simplest, a procedure is just some lines of Turing code. We wrap some special syntax around our lines of Turing code and think up a name for them. After we have defined our procedure, we can ''call'' the procedure by saying its name. When we do this, we run those lines of Turing code that are wrapped inside the procedure definition. This is easiest to see with an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So now the single line of code, ''put &amp;quot;Hello world!&amp;quot;'', can be executed any time we call the ''greet'' procedure. The above code is identical to the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
All I've done is substituted the code inside the ''greet'' procedure for the call to ''greet'' in the last line of the original program.&lt;br /&gt;
&lt;br /&gt;
So, why is this useful? It's useful because now whenever we want to output &amp;quot;Hello world!&amp;quot;, we don't have to type, ''put &amp;quot;Hello world!&amp;quot;'', but only ''greet''. Okay, so we saved on typing fourteen charactes. Not a big deal, right? But what if we wrote a program that said &amp;quot;Hello world!&amp;quot; a lot. Our program said hello to the world ten times during the course of its execution. Now, we're looking back at our program and thinking, &amp;quot;You know, 'Hello world!' isn't really what we want to say. We really are only talking to one person--George. We should instead say 'Hello George!'.&amp;quot; So, now we've got to go into our code and change it. If we used a ''greet'' procedure, this change involves changing the definition of the procedure so that it becomes the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello George!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we hadn't used this procedure, we would have to go through our code, searching for any time we said &amp;quot;Hello world!&amp;quot; and change it to be &amp;quot;Hello George!&amp;quot;. That's a lot of unnecessary work, relative to how effective our ''greet'' solution is.&lt;br /&gt;
&lt;br /&gt;
But now, what if we aren't always talking to George?&lt;br /&gt;
&lt;br /&gt;
==Procedures with Parameters==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure to greet to George and another procedure to greet to Phil and another to greet to Anthony, we should try to write a procedure that can greet to anyone. Here's one possibility:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name : string&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;George&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Phil&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Anthony&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This works, but it's clunky. We've got that variable up there, ''name''. It's what we call a '''global variable'''. It's called that because it is global to the whole program. No matter where we are in our code, we can always see the ''name'' variable. Really, we don't need to keep track of ''name''. We only need it ''inside the greet procedure''. Here's where '''parameters''' are used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Phil&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Anthony&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the definition of our greet procedure, I added that extra bit at the end--the stuff in parentheses. That says that when we call the greet procedure, we must give it one '''argument''', and that argument must be a string. It also says that for all code inside the procedure, there exists a '''constant''' called &amp;quot;name&amp;quot; that has the value we give it when we call the procedure. So, when we call ''greet (&amp;quot;George&amp;quot;)'', we are setting ''name'' to be &amp;quot;George&amp;quot;, and we execute the code inside the procedure. The following two pieces of code are identical:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, &amp;quot;George&amp;quot;, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I mentioned earlier that inside the ''greet'' procedure there exists this constant called name. There are two things we need to understand about it. First, it is constant. We can't change the value of name. Let's get some example code and figure out why:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This code fails because, inside ''greet'', ''name'' is really just a ''reference to &amp;quot;George&amp;quot;''. When we try to redefine name, we're really trying to redefine the string literal &amp;quot;George&amp;quot;. Of course, that's impossible. What if we did this, though?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now you might think that, inside ''greet'', ''name'' is a reference to ''person'', which is a variable, so attempting to redefine ''name'' is really redefining ''person'', and that's okay. However, it's not quite that simple. See, ''greet'' expects a string as its argument, not a variable. In ''greet (person)'', ''person'' is an expression. It gets evaluated down to a string value; it gets evaluated to &amp;quot;George&amp;quot;. Now, once again, we're passing the string literal &amp;quot;George&amp;quot; into ''greet'', and we're back at our first situation.&lt;br /&gt;
&lt;br /&gt;
The second thing we need to understand about ''name'' is that it's '''local'''. It exists only within the procedure. The following code fails:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
put name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''name'' doesn't exist outside of the ''greet'' procedure. It's '''local'''. It's '''scope''' is the ''greet'' procedure. You should also note that each time we call ''greet'' we are creating a new ''name'' constant.&lt;br /&gt;
&lt;br /&gt;
What if we wanted to write a procedure that introduces two people to each other? We need a procedure that can two arguments, not one.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Procedures with Multiple Parameters==&lt;br /&gt;
&lt;br /&gt;
I will now define a procedure, called ''introduce'', that introduces two people to each other:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1 : string, person2 : string)&lt;br /&gt;
    put person1, &amp;quot;, this is &amp;quot;, person2, &amp;quot;.&amp;quot;&lt;br /&gt;
    put person2, &amp;quot;, meet &amp;quot;, person1, &amp;quot;.&amp;quot;&lt;br /&gt;
end introduce&lt;br /&gt;
&lt;br /&gt;
% Call the procedure&lt;br /&gt;
introduce (&amp;quot;Jackie&amp;quot;, &amp;quot;Bruce&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output of this program will be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Jackie, this is Bruce.&lt;br /&gt;
Bruce, meet Jackie.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, reversing the order of the names results in a different output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
introduce (&amp;quot;Bruce&amp;quot;, &amp;quot;Jackie&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bruce, this is Jackie.&lt;br /&gt;
Jackie, meet Bruce.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You should be able to see how this works. When I call ''introduce'', person1 references the first name I give, and person2 references the second name I give. It's based on the ''order'' of the parameters/arguments. (Note some terminology here: person1 and person2 are the parameters; &amp;quot;Bruce&amp;quot; and &amp;quot;Jackie&amp;quot; are the arguments.)&lt;br /&gt;
&lt;br /&gt;
Here's a little, unimportant shortcut. We can declare more than one variable on the same line, provided they are of the same type:&lt;br /&gt;
&amp;lt;pre&amp;gt;var name1, name2 : string&amp;lt;/pre&amp;gt;&lt;br /&gt;
We can do the same with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1, person2 : string)&lt;br /&gt;
    %...&lt;br /&gt;
end introduce&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, I'll give another trivial example. This one will involve writing a procedure that takes two numbers and outputs their product.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure put_product (num1, num2 : real)&lt;br /&gt;
    put num1 * num2&lt;br /&gt;
end put_product&lt;br /&gt;
&lt;br /&gt;
put_product (4, 6)  % Outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, what we're doing is rather silly, isn't it? I mean, aside from the fact that we're defining a procedure to do multiplication, which we can do very quickly anyways. What's silly about this is that we've cornered ourselves. This procedure forces us to send the product to the standard output. What if we wanted to store it in a variable, or draw it in a pretty font, or pass it to some other procedure? Take a minute and think about how we might solve this problem. We want to be able to generalize, here. We want to write something that can multiply two numbers we give it, but still give us the flexibility to do what we want with the answer.&lt;br /&gt;
&lt;br /&gt;
If you're thinking about passing in another parameter that says what we are to do with the prodcut we calculate, then you're thinking way ahead of the game. It's possible, but not easy. There's an easier way.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing Functions==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure that outputs the product of two numbers, we'll write a function that computes the prodcut of two numbers and ''returns that value''. Then we can decide what to do with that number. That's what a function is: it's the same as a procedure, except it returns a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function product (num1, num2 : real) : real&lt;br /&gt;
    result num1 * num2&lt;br /&gt;
end product&lt;br /&gt;
&lt;br /&gt;
put product (4, 6)  % outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There's some new syntax there. First, we're using the keyword, &amp;quot;function&amp;quot;, instead of &amp;quot;procedure&amp;quot;. Next, there's a &amp;quot;: real&amp;quot; after our parameter list. That signifies what type of value our function will return. Also, we've used the '''result''' keyword. Earlier I said that functions return a value. The value returned is given by what is immediately after the '''result''' keyword.&lt;br /&gt;
&lt;br /&gt;
Using a function gives us more flexibility. We don't have to send our answer to the standard output. We could store it in a variable, for instance.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : real := product (1.5, 3)   % n := 4.5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Then we could use that elsewhere:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (n, 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or perhaps better yet:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (product 1.5, 3) 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's now look a little closer at '''result'''. It has an interesting behaviour that we have yet to examine.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Closer Look at 'result'==&lt;br /&gt;
&lt;br /&gt;
A function's sole purpose is to compute and return a value. With that in mind, let's write a ficticious function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function ficticious (number : int) : int&lt;br /&gt;
    result number + 1&lt;br /&gt;
    put &amp;quot;Yarr, it be a cold day.&amp;quot;&lt;br /&gt;
end ficticious&lt;br /&gt;
&lt;br /&gt;
% Call our function and store its answer in a variable&lt;br /&gt;
var n : int := ficticious (4)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
What has happened here? Surely, ''ficticious (4)'' returns the value 5. So ''n'' should be 5. What about that crazy pirate message? Well, as soon as the function computed its value, it returned that value and completed execution. The &amp;quot;Yarr...&amp;quot; message was never output; that line of code was never reached.&lt;br /&gt;
&lt;br /&gt;
This behaviour is a bit irratic. It can make it difficult to understand how a function is working. At the same time, it can be quite useful in shortening code. You've got a find a balance between short code and understandable code.&lt;br /&gt;
&lt;br /&gt;
Soon we will compare and contrast procedures and functions. Before we can do that, however, we need to re-examine what I said earlier about our parameter being constant.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable Parameters==&lt;br /&gt;
&lt;br /&gt;
Let's go back to our first example with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I had argued that ''name'' is constant. We cannot change its value. Indeed, we cannot. However, we can change this code so that ''name'' is in fact variable. All we have to do is specify that the ''name'' parameter is not simply a string, but is a variable that is a string. We do this by using the '''var''' keyword.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (var name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
put &amp;quot;After calling greet, the value of person is now &amp;quot;, person&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Hello George!&lt;br /&gt;
And hello to Freddy as well!&lt;br /&gt;
After calling greet, the value of person is now Freddy&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The difference here is that ''greet'' is expecting to take a string variable, instead of just a string. So when we pass ''person'' to it, ''name'' actually becomes a reference to the variable ''person''. Then, changing the value of ''name'' changes the value of ''person''.&lt;br /&gt;
&lt;br /&gt;
Now, we're familiar with procedures and functions. We now know enough to start thinking about when we should use a procedure and when we should use a function.&lt;br /&gt;
&lt;br /&gt;
==Functions vs. Procedures==&lt;br /&gt;
&lt;br /&gt;
Both functions and procedures are what some call &amp;quot;''subroutines''&amp;quot;--they are something we can use to store code and call at a later point. A function computes and returns a value, whereas a procedure just runs some code. We need to get an understanding of when to use a function and when to use a procedure. Let's look at a pretty famous case study.&lt;br /&gt;
&lt;br /&gt;
In Turing, there are two ways of generating a random integer. The first way is to use the built-in function, ''Rand.Int''. (This is a function just like what we've been writing so far. Don't let the fact that it has a period in it's name fool you.) ''Rand.Int'' takes two numbers, a low and a high; it produces a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
n := Rand.Int (4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now n is a random integer between 4 and 11.&lt;br /&gt;
&lt;br /&gt;
The second way to produce random integers is to use the procedure, ''randint''. This procedure first takes a variable (like our ''greet'' procedure did in the last section) and also takes two numbers, a low and a high. ''randint'' takes the variable you give it and sets it to a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, which is better: ''Rand.Int'' or ''randint''? The correct answer is ''Rand.Int''. Here's why:&lt;br /&gt;
&lt;br /&gt;
First, ''Rand.Int'' is more flexible. Say you wrote a function called &amp;quot;squeeze&amp;quot; that took in an integer. You want to give it a random integer between 1 and 6. Let's look at the two options:&lt;br /&gt;
&amp;lt;pre&amp;gt;squeeze (Rand.Int (1, 6))&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 1, 6)&lt;br /&gt;
squeeze (n)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Clearly the ''Rand.Int'' version is cleaner. There's no polluting the global namespace with useless variables like &amp;quot;''n''&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The second reason is a bit more theoretical, but is of equal importance. ''randint'' takes in a variable and does something to it. We don't really know what. We have trust the documentation. Now, we've also got to trust the documentation of ''Rand.Int''. However, there's an important difference. With ''Rand.Int'', we assign to our variable the value returned by the function. With ''randint'', we have given our precious variable to the procedure, along with all the permissions necessary for the procedure to slaughter our poor variable. It has complete control. It can write whatever it wants in there. We've suddenly given away control of the ''state'' of our program. (State is a term that collectively means the values of all our variables and also our current location in the execution of the code.) The fewer people we give this control to, the better.&lt;br /&gt;
&lt;br /&gt;
If you're not yet convinced, take a look at this. I'll code a version of ''randint'' for you.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure my_randint (var num : int, low, high : int)&lt;br /&gt;
    if num mod 2 = 0&lt;br /&gt;
        num := low + (num * 2) mod (high - low + 1)&lt;br /&gt;
    else&lt;br /&gt;
        num := low + (num + 2) mod (high - low + 1)&lt;br /&gt;
    end if&lt;br /&gt;
end my_randint&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So ''my_randint'' isn't really random, but it might fool you. Take a look at some sample data:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% I've taken a shortcut here. When I say&lt;br /&gt;
%   my_randint (0, 5, 10)&lt;br /&gt;
% I really mean&lt;br /&gt;
%   var n : int := 0&lt;br /&gt;
%   my_randint (n, 5, 10)&lt;br /&gt;
&lt;br /&gt;
my_randint (0, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (1, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (2, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (3, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (4, 5, 10)  %-&amp;gt; 7&lt;br /&gt;
my_randint (5, 5, 10)  %-&amp;gt; 6&lt;br /&gt;
my_randint (6, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (7, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (8, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (9, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (10, 5, 10) %-&amp;gt; 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I could not have done a similar thing if I were using a function, because I was never given a value for ''num''. (I know what some of you are thinking: &amp;quot;What if I give an uninitialized variable to my_randint? It'll crash.&amp;quot; It's possible that I could hack something together that could take care of this, but that's not the point.) The point of this is to show you that by giving the procedure control of your variable, it can use it that variable for evil purposes, as I have done with this ''my_randint'' procedure.&lt;br /&gt;
&lt;br /&gt;
I sincerely hope this has convinced you that functions are superior to procedures. I'm not saying that functions should be used exclusively. There are times when procedures are needed. Rather, I'm just saying that when you have a choice of using a function or using a procedure, choose the function. Nice functions are ones that compute a value based soley on the parameters they take. They don't rely on any global variables. These are functions that are easily documented. At the other extreme are procedures that use global variables inside their definition and also take in variables and modify them. These procedures are ''dangerous!'' and very hard to document.&lt;br /&gt;
&lt;br /&gt;
There is one last thing for us to discuss. It's a very powerful concept known as &amp;quot;recursion&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Recursion==&lt;br /&gt;
&lt;br /&gt;
I'd like to start this section off with a quotation:&lt;br /&gt;
&amp;lt;pre&amp;gt;To understand recursion, you must first understand recursion.&amp;lt;/pre&amp;gt;&lt;br /&gt;
A recursive subroutine is a subroutine that calls itself. One of the most famous and basic examples is the factorial function. The factorial function takes in a number and returns the product of that number and the factorial of the number minus one. Also, the factorial of one is defined to be one. This is the recursive definition of the function. It translates very easily into code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function factorial (n : int) : int&lt;br /&gt;
    if n = 1 then&lt;br /&gt;
        result 1&lt;br /&gt;
    else&lt;br /&gt;
        result n * factorial (n - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end factorial&lt;br /&gt;
&lt;br /&gt;
put factorial (3)  % Outputs &amp;quot;6&amp;quot;. Note 6 = 3*2*1&lt;br /&gt;
put factorial (5)  % Outputs &amp;quot;120&amp;quot;. Note 120 = 5*4*3*2*1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I will give a condensed trace of the factorial function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
factorial (5)&lt;br /&gt;
(5 * factorial (4))&lt;br /&gt;
(5 * (4 * factorial (3)))&lt;br /&gt;
(5 * (4 * (3 * factorial (2))))&lt;br /&gt;
(5 * (4 * (3 * (2 * factorial (1)))))&lt;br /&gt;
(5 * (4 * (3 * (2 * 1))))&lt;br /&gt;
(5 * (4 * (3 * 2)))&lt;br /&gt;
(5 * (4 * 6))&lt;br /&gt;
(5 * 24)&lt;br /&gt;
120&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I've given the recursive definition of the factorial function. A non-recursive definition is to say that factorial (n) = n * (n-1) * (n-2) * ... * 3 * 2 * 1. if you know about loops, you can code this definition of the factorial function. You can then compare and contrast the two definitions and see which you prefer. I hope you'll prefer this version, but that's for time to decide.&lt;br /&gt;
&lt;br /&gt;
As a segue into the next tutorial (which will be a tutorial on looping constructs), I will now present a recursive procedure that counts down to 1, and then displays, &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure countdown (high : int)&lt;br /&gt;
    if high = 0 then&lt;br /&gt;
        put &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
    else&lt;br /&gt;
        put high, &amp;quot;...&amp;quot;&lt;br /&gt;
        countdown (high - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end countdown&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Calling ''countdown (5)'' produces:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
5...&lt;br /&gt;
4...&lt;br /&gt;
3...&lt;br /&gt;
2...&lt;br /&gt;
1...&lt;br /&gt;
Blast off!!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Question One!''' Create a guessing game. Your program will have a global variable that represents the number the user is trying to guess. Use recursion to write a procedure called ''guess''. When run, ''guess'' prompts the user to input a number. If the guessed number is equal to ''the'' number, a winning message is displayed. Otherwise, the user is told whether his/her guess was too high or too low, and is prompted to guess another number.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Two!''' Define a function called ''add'' that takes two natural numbers and returns their sum. There's a catch, though. You're not allowed to use the + operator. However, you are allowed to use these two functions that I supply you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function add1 (n : int) : int&lt;br /&gt;
    result n + 1&lt;br /&gt;
end add1&lt;br /&gt;
function sub1 (n : int) : int&lt;br /&gt;
    result n - 1&lt;br /&gt;
end sub1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Three''' Define a function called ''multiply'' that takes two natural numbers and returns their product. Once again, there's a catch. You're not allowed to use the built in * operator. (You are allowed to use the + operator though.) You can, however, use the following two functions that I provide you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function double (n : int) : int&lt;br /&gt;
    result n + n&lt;br /&gt;
end double&lt;br /&gt;
&lt;br /&gt;
% The halve function performs integer division by 2.&lt;br /&gt;
% Examples: halve (8) -&amp;gt; 4&lt;br /&gt;
%           halve (17) -&amp;gt; 8&lt;br /&gt;
function halve (n : int) : int&lt;br /&gt;
    result floor (n / 2)&lt;br /&gt;
end halve&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Hint: You should use the binary definition of a natural number. That is, a natural number is one of&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*2*k where k is a natural number&lt;br /&gt;
*2*k + 1 where k is a natural number&lt;br /&gt;
&lt;br /&gt;
That is why I gave you the double and halve functions. It's possible to write a solution using the unary definition of a natural number (sort of like in the solution to problem two), but it will be much slower than the solution using the binary definition.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
I hope you find these exercises in recursion interesting. At this point, we've learned a small amount of the Turing language, but you see we can do some pretty powerful things using this concept of recursion.&lt;br /&gt;
&lt;br /&gt;
Even if we don't take advantage of recursion, however, the topics covered in this tutorial still give us considerable power. They allow us to organize our code. We can save ourselves from repeating ourselves. We can generalize by using parameters. Indeed, the rather simple idea of factoring out common code has led to powerful advances.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
&lt;br /&gt;
===Problem One===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var the_num := Rand.Int (1, 100)&lt;br /&gt;
&lt;br /&gt;
proc guess&lt;br /&gt;
    put &amp;quot;Please input a guess: &amp;quot; ..&lt;br /&gt;
    var g : int&lt;br /&gt;
    get g&lt;br /&gt;
    if g = the_num then&lt;br /&gt;
        put &amp;quot;You've got it!&amp;quot;&lt;br /&gt;
    elsif g &amp;lt; the_num then&lt;br /&gt;
        put &amp;quot;You've guessed too low. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    else&lt;br /&gt;
        put &amp;quot;You've guessed too high. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    end if&lt;br /&gt;
end guess&lt;br /&gt;
&lt;br /&gt;
guess&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Two===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by increasing n2 by one as we &lt;br /&gt;
% decrease n1 by one. If n2 = 0, then we just return n1.&lt;br /&gt;
% Essentially, it works on the following identity:&lt;br /&gt;
%    n1 + n2 = (n1 + 1) + (n2 - 1)&lt;br /&gt;
function add (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 0 then&lt;br /&gt;
        result n1&lt;br /&gt;
    else&lt;br /&gt;
        result add (add1 (n1), sub1 (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end add&lt;br /&gt;
&lt;br /&gt;
% Note that this function could be made more efficient if, instead &lt;br /&gt;
% of blindly choosing to decrease n2 and increase n1, we chose to&lt;br /&gt;
% decrease the smaller of n1 and n2 and increase the larger of n1 and n2.&lt;br /&gt;
% Try writing another function that calls this add function in that way.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Three===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by decreasing n2 and increasing n1.&lt;br /&gt;
% It works on the following identity:&lt;br /&gt;
% If n2 is even:  n1 * n2 = double (n1) * halve (n2)&lt;br /&gt;
% If n2 is odd:   n1 * n2 = n1 + double (n1) * halve (n2)&lt;br /&gt;
function multiply (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 1 then&lt;br /&gt;
        result n1&lt;br /&gt;
    elsif n2 mod 2 = 0 then              % n2 is even&lt;br /&gt;
        result multiply (double (n1), halve (n2))&lt;br /&gt;
    else                                 % n2 is odd&lt;br /&gt;
        result n1 + multiply (double (n1), halve (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end multiply&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Credits==&lt;br /&gt;
Authour: [[Cervantes]]&lt;br /&gt;
&lt;br /&gt;
Added by: [[Saad]]&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures</id>
		<title>Turing Functions and Procedures</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures"/>
				<updated>2008-10-12T19:36:39Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: /* Credits */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Functions and Procedures==&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Thus far, we've learned enough of the Turing language to be able to write some decently large programs. We've learned some fundamentals of Turing -- variables, basic input/output, and if statements. Indeed, these concepts translate relatively smoothly to most any other programming language you might learn. So, having mastered these basics, what's next? Well, having a way to organize our code would be good. Right now, whatever code you write runs linearly, from top to bottom. If you want to run a section of code twice, you have to type it out twice. Wouldn't it be great if we could factor out the commonalities in our code, so we can write less? In truth, we haven't learned very much, so it might be difficult to see the advantage of this. Just bear with me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing the Procedure==&lt;br /&gt;
&lt;br /&gt;
At its simplest, a procedure is just some lines of Turing code. We wrap some special syntax around our lines of Turing code and think up a name for them. After we have defined our procedure, we can ''call'' the procedure by saying its name. When we do this, we run those lines of Turing code that are wrapped inside the procedure definition. This is easiest to see with an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So now the single line of code, ''put &amp;quot;Hello world!&amp;quot;'', can be executed any time we call the ''greet'' procedure. The above code is identical to the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
All I've done is substituted the code inside the ''greet'' procedure for the call to ''greet'' in the last line of the original program.&lt;br /&gt;
&lt;br /&gt;
So, why is this useful? It's useful because now whenever we want to output &amp;quot;Hello world!&amp;quot;, we don't have to type, ''put &amp;quot;Hello world!&amp;quot;'', but only ''greet''. Okay, so we saved on typing fourteen charactes. Not a big deal, right? But what if we wrote a program that said &amp;quot;Hello world!&amp;quot; a lot. Our program said hello to the world ten times during the course of its execution. Now, we're looking back at our program and thinking, &amp;quot;You know, 'Hello world!' isn't really what we want to say. We really are only talking to one person--George. We should instead say 'Hello George!'.&amp;quot; So, now we've got to go into our code and change it. If we used a ''greet'' procedure, this change involves changing the definition of the procedure so that it becomes the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello George!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we hadn't used this procedure, we would have to go through our code, searching for any time we said &amp;quot;Hello world!&amp;quot; and change it to be &amp;quot;Hello George!&amp;quot;. That's a lot of unnecessary work, relative to how effective our ''greet'' solution is.&lt;br /&gt;
&lt;br /&gt;
But now, what if we aren't always talking to George?&lt;br /&gt;
&lt;br /&gt;
==Procedures with Parameters==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure to greet to George and another procedure to greet to Phil and another to greet to Anthony, we should try to write a procedure that can greet to anyone. Here's one possibility:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name : string&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;George&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Phil&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Anthony&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This works, but it's clunky. We've got that variable up there, ''name''. It's what we call a '''global variable'''. It's called that because it is global to the whole program. No matter where we are in our code, we can always see the ''name'' variable. Really, we don't need to keep track of ''name''. We only need it ''inside the greet procedure''. Here's where '''parameters''' are used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Phil&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Anthony&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the definition of our greet procedure, I added that extra bit at the end--the stuff in parentheses. That says that when we call the greet procedure, we must give it one '''argument''', and that argument must be a string. It also says that for all code inside the procedure, there exists a '''constant''' called &amp;quot;name&amp;quot; that has the value we give it when we call the procedure. So, when we call ''greet (&amp;quot;George&amp;quot;)'', we are setting ''name'' to be &amp;quot;George&amp;quot;, and we execute the code inside the procedure. The following two pieces of code are identical:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, &amp;quot;George&amp;quot;, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I mentioned earlier that inside the ''greet'' procedure there exists this constant called name. There are two things we need to understand about it. First, it is constant. We can't change the value of name. Let's get some example code and figure out why:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This code fails because, inside ''greet'', ''name'' is really just a ''reference to &amp;quot;George&amp;quot;''. When we try to redefine name, we're really trying to redefine the string literal &amp;quot;George&amp;quot;. Of course, that's impossible. What if we did this, though?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now you might think that, inside ''greet'', ''name'' is a reference to ''person'', which is a variable, so attempting to redefine ''name'' is really redefining ''person'', and that's okay. However, it's not quite that simple. See, ''greet'' expects a string as its argument, not a variable. In ''greet (person)'', ''person'' is an expression. It gets evaluated down to a string value; it gets evaluated to &amp;quot;George&amp;quot;. Now, once again, we're passing the string literal &amp;quot;George&amp;quot; into ''greet'', and we're back at our first situation.&lt;br /&gt;
&lt;br /&gt;
The second thing we need to understand about ''name'' is that it's '''local'''. It exists only within the procedure. The following code fails:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
put name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''name'' doesn't exist outside of the ''greet'' procedure. It's '''local'''. It's '''scope''' is the ''greet'' procedure. You should also note that each time we call ''greet'' we are creating a new ''name'' constant.&lt;br /&gt;
&lt;br /&gt;
What if we wanted to write a procedure that introduces two people to each other? We need a procedure that can two arguments, not one.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Procedures with Multiple Parameters==&lt;br /&gt;
&lt;br /&gt;
I will now define a procedure, called ''introduce'', that introduces two people to each other:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1 : string, person2 : string)&lt;br /&gt;
    put person1, &amp;quot;, this is &amp;quot;, person2, &amp;quot;.&amp;quot;&lt;br /&gt;
    put person2, &amp;quot;, meet &amp;quot;, person1, &amp;quot;.&amp;quot;&lt;br /&gt;
end introduce&lt;br /&gt;
&lt;br /&gt;
% Call the procedure&lt;br /&gt;
introduce (&amp;quot;Jackie&amp;quot;, &amp;quot;Bruce&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output of this program will be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Jackie, this is Bruce.&lt;br /&gt;
Bruce, meet Jackie.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, reversing the order of the names results in a different output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
introduce (&amp;quot;Bruce&amp;quot;, &amp;quot;Jackie&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bruce, this is Jackie.&lt;br /&gt;
Jackie, meet Bruce.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You should be able to see how this works. When I call ''introduce'', person1 references the first name I give, and person2 references the second name I give. It's based on the ''order'' of the parameters/arguments. (Note some terminology here: person1 and person2 are the parameters; &amp;quot;Bruce&amp;quot; and &amp;quot;Jackie&amp;quot; are the arguments.)&lt;br /&gt;
&lt;br /&gt;
Here's a little, unimportant shortcut. We can declare more than one variable on the same line, provided they are of the same type:&lt;br /&gt;
&amp;lt;pre&amp;gt;var name1, name2 : string&amp;lt;/pre&amp;gt;&lt;br /&gt;
We can do the same with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1, person2 : string)&lt;br /&gt;
    %...&lt;br /&gt;
end introduce&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, I'll give another trivial example. This one will involve writing a procedure that takes two numbers and outputs their product.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure put_product (num1, num2 : real)&lt;br /&gt;
    put num1 * num2&lt;br /&gt;
end put_product&lt;br /&gt;
&lt;br /&gt;
put_product (4, 6)  % Outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, what we're doing is rather silly, isn't it? I mean, aside from the fact that we're defining a procedure to do multiplication, which we can do very quickly anyways. What's silly about this is that we've cornered ourselves. This procedure forces us to send the product to the standard output. What if we wanted to store it in a variable, or draw it in a pretty font, or pass it to some other procedure? Take a minute and think about how we might solve this problem. We want to be able to generalize, here. We want to write something that can multiply two numbers we give it, but still give us the flexibility to do what we want with the answer.&lt;br /&gt;
&lt;br /&gt;
If you're thinking about passing in another parameter that says what we are to do with the prodcut we calculate, then you're thinking way ahead of the game. It's possible, but not easy. There's an easier way.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing Functions==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure that outputs the product of two numbers, we'll write a function that computes the prodcut of two numbers and ''returns that value''. Then we can decide what to do with that number. That's what a function is: it's the same as a procedure, except it returns a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function product (num1, num2 : real) : real&lt;br /&gt;
    result num1 * num2&lt;br /&gt;
end product&lt;br /&gt;
&lt;br /&gt;
put product (4, 6)  % outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There's some new syntax there. First, we're using the keyword, &amp;quot;function&amp;quot;, instead of &amp;quot;procedure&amp;quot;. Next, there's a &amp;quot;: real&amp;quot; after our parameter list. That signifies what type of value our function will return. Also, we've used the '''result''' keyword. Earlier I said that functions return a value. The value returned is given by what is immediately after the '''result''' keyword.&lt;br /&gt;
&lt;br /&gt;
Using a function gives us more flexibility. We don't have to send our answer to the standard output. We could store it in a variable, for instance.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : real := product (1.5, 3)   % n := 4.5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Then we could use that elsewhere:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (n, 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or perhaps better yet:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (product 1.5, 3) 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's now look a little closer at '''result'''. It has an interesting behaviour that we have yet to examine.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Closer Look at 'result'==&lt;br /&gt;
&lt;br /&gt;
A function's sole purpose is to compute and return a value. With that in mind, let's write a ficticious function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function ficticious (number : int) : int&lt;br /&gt;
    result number + 1&lt;br /&gt;
    put &amp;quot;Yarr, it be a cold day.&amp;quot;&lt;br /&gt;
end ficticious&lt;br /&gt;
&lt;br /&gt;
% Call our function and store its answer in a variable&lt;br /&gt;
var n : int := ficticious (4)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
What has happened here? Surely, ''ficticious (4)'' returns the value 5. So ''n'' should be 5. What about that crazy pirate message? Well, as soon as the function computed its value, it returned that value and completed execution. The &amp;quot;Yarr...&amp;quot; message was never output; that line of code was never reached.&lt;br /&gt;
&lt;br /&gt;
This behaviour is a bit irratic. It can make it difficult to understand how a function is working. At the same time, it can be quite useful in shortening code. You've got a find a balance between short code and understandable code.&lt;br /&gt;
&lt;br /&gt;
Soon we will compare and contrast procedures and functions. Before we can do that, however, we need to re-examine what I said earlier about our parameter being constant.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable Parameters==&lt;br /&gt;
&lt;br /&gt;
Let's go back to our first example with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I had argued that ''name'' is constant. We cannot change its value. Indeed, we cannot. However, we can change this code so that ''name'' is in fact variable. All we have to do is specify that the ''name'' parameter is not simply a string, but is a variable that is a string. We do this by using the '''var''' keyword.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (var name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
put &amp;quot;After calling greet, the value of person is now &amp;quot;, person&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Hello George!&lt;br /&gt;
And hello to Freddy as well!&lt;br /&gt;
After calling greet, the value of person is now Freddy&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The difference here is that ''greet'' is expecting to take a string variable, instead of just a string. So when we pass ''person'' to it, ''name'' actually becomes a reference to the variable ''person''. Then, changing the value of ''name'' changes the value of ''person''.&lt;br /&gt;
&lt;br /&gt;
Now, we're familiar with procedures and functions. We now know enough to start thinking about when we should use a procedure and when we should use a function.&lt;br /&gt;
&lt;br /&gt;
==Functions vs. Procedures==&lt;br /&gt;
&lt;br /&gt;
Both functions and procedures are what some call &amp;quot;''subroutines''&amp;quot;--they are something we can use to store code and call at a later point. A function computes and returns a value, whereas a procedure just runs some code. We need to get an understanding of when to use a function and when to use a procedure. Let's look at a pretty famous case study.&lt;br /&gt;
&lt;br /&gt;
In Turing, there are two ways of generating a random integer. The first way is to use the built-in function, ''Rand.Int''. (This is a function just like what we've been writing so far. Don't let the fact that it has a period in it's name fool you.) ''Rand.Int'' takes two numbers, a low and a high; it produces a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
n := Rand.Int (4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now n is a random integer between 4 and 11.&lt;br /&gt;
&lt;br /&gt;
The second way to produce random integers is to use the procedure, ''randint''. This procedure first takes a variable (like our ''greet'' procedure did in the last section) and also takes two numbers, a low and a high. ''randint'' takes the variable you give it and sets it to a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, which is better: ''Rand.Int'' or ''randint''? The correct answer is ''Rand.Int''. Here's why:&lt;br /&gt;
&lt;br /&gt;
First, ''Rand.Int'' is more flexible. Say you wrote a function called &amp;quot;squeeze&amp;quot; that took in an integer. You want to give it a random integer between 1 and 6. Let's look at the two options:&lt;br /&gt;
&amp;lt;pre&amp;gt;squeeze (Rand.Int (1, 6))&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 1, 6)&lt;br /&gt;
squeeze (n)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Clearly the ''Rand.Int'' version is cleaner. There's no polluting the global namespace with useless variables like &amp;quot;''n''&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The second reason is a bit more theoretical, but is of equal importance. ''randint'' takes in a variable and does something to it. We don't really know what. We have trust the documentation. Now, we've also got to trust the documentation of ''Rand.Int''. However, there's an important difference. With ''Rand.Int'', we assign to our variable the value returned by the function. With ''randint'', we have given our precious variable to the procedure, along with all the permissions necessary for the procedure to slaughter our poor variable. It has complete control. It can write whatever it wants in there. We've suddenly given away control of the ''state'' of our program. (State is a term that collectively means the values of all our variables and also our current location in the execution of the code.) The fewer people we give this control to, the better.&lt;br /&gt;
&lt;br /&gt;
If you're not yet convinced, take a look at this. I'll code a version of ''randint'' for you.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure my_randint (var num : int, low, high : int)&lt;br /&gt;
    if num mod 2 = 0&lt;br /&gt;
        num := low + (num * 2) mod (high - low + 1)&lt;br /&gt;
    else&lt;br /&gt;
        num := low + (num + 2) mod (high - low + 1)&lt;br /&gt;
    end if&lt;br /&gt;
end my_randint&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So ''my_randint'' isn't really random, but it might fool you. Take a look at some sample data:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% I've taken a shortcut here. When I say&lt;br /&gt;
%   my_randint (0, 5, 10)&lt;br /&gt;
% I really mean&lt;br /&gt;
%   var n : int := 0&lt;br /&gt;
%   my_randint (n, 5, 10)&lt;br /&gt;
&lt;br /&gt;
my_randint (0, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (1, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (2, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (3, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (4, 5, 10)  %-&amp;gt; 7&lt;br /&gt;
my_randint (5, 5, 10)  %-&amp;gt; 6&lt;br /&gt;
my_randint (6, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (7, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (8, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (9, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (10, 5, 10) %-&amp;gt; 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I could not have done a similar thing if I were using a function, because I was never given a value for ''num''. (I know what some of you are thinking: &amp;quot;What if I give an uninitialized variable to my_randint? It'll crash.&amp;quot; It's possible that I could hack something together that could take care of this, but that's not the point.) The point of this is to show you that by giving the procedure control of your variable, it can use it that variable for evil purposes, as I have done with this ''my_randint'' procedure.&lt;br /&gt;
&lt;br /&gt;
I sincerely hope this has convinced you that functions are superior to procedures. I'm not saying that functions should be used exclusively. There are times when procedures are needed. Rather, I'm just saying that when you have a choice of using a function or using a procedure, choose the function. Nice functions are ones that compute a value based soley on the parameters they take. They don't rely on any global variables. These are functions that are easily documented. At the other extreme are procedures that use global variables inside their definition and also take in variables and modify them. These procedures are ''dangerous!'' and very hard to document.&lt;br /&gt;
&lt;br /&gt;
There is one last thing for us to discuss. It's a very powerful concept known as &amp;quot;recursion&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Recursion==&lt;br /&gt;
&lt;br /&gt;
I'd like to start this section off with a quotation:&lt;br /&gt;
&amp;lt;pre&amp;gt;To understand recursion, you must first understand recursion.&amp;lt;/pre&amp;gt;&lt;br /&gt;
A recursive subroutine is a subroutine that calls itself. One of the most famous and basic examples is the factorial function. The factorial function takes in a number and returns the product of that number and the factorial of the number minus one. Also, the factorial of one is defined to be one. This is the recursive definition of the function. It translates very easily into code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function factorial (n : int) : int&lt;br /&gt;
    if n = 1 then&lt;br /&gt;
        result 1&lt;br /&gt;
    else&lt;br /&gt;
        result n * factorial (n - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end factorial&lt;br /&gt;
&lt;br /&gt;
put factorial (3)  % Outputs &amp;quot;6&amp;quot;. Note 6 = 3*2*1&lt;br /&gt;
put factorial (5)  % Outputs &amp;quot;120&amp;quot;. Note 120 = 5*4*3*2*1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I will give a condensed trace of the factorial function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
factorial (5)&lt;br /&gt;
(5 * factorial (4))&lt;br /&gt;
(5 * (4 * factorial (3)))&lt;br /&gt;
(5 * (4 * (3 * factorial (2))))&lt;br /&gt;
(5 * (4 * (3 * (2 * factorial (1)))))&lt;br /&gt;
(5 * (4 * (3 * (2 * 1))))&lt;br /&gt;
(5 * (4 * (3 * 2)))&lt;br /&gt;
(5 * (4 * 6))&lt;br /&gt;
(5 * 24)&lt;br /&gt;
120&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I've given the recursive definition of the factorial function. A non-recursive definition is to say that factorial (n) = n * (n-1) * (n-2) * ... * 3 * 2 * 1. if you know about loops, you can code this definition of the factorial function. You can then compare and contrast the two definitions and see which you prefer. I hope you'll prefer this version, but that's for time to decide.&lt;br /&gt;
&lt;br /&gt;
As a segue into the next tutorial (which will be a tutorial on looping constructs), I will now present a recursive procedure that counts down to 1, and then displays, &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure countdown (high : int)&lt;br /&gt;
    if high = 0 then&lt;br /&gt;
        put &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
    else&lt;br /&gt;
        put high, &amp;quot;...&amp;quot;&lt;br /&gt;
        countdown (high - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end countdown&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Calling ''countdown (5)'' produces:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
5...&lt;br /&gt;
4...&lt;br /&gt;
3...&lt;br /&gt;
2...&lt;br /&gt;
1...&lt;br /&gt;
Blast off!!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Question One!''' Create a guessing game. Your program will have a global variable that represents the number the user is trying to guess. Use recursion to write a procedure called ''guess''. When run, ''guess'' prompts the user to input a number. If the guessed number is equal to ''the'' number, a winning message is displayed. Otherwise, the user is told whether his/her guess was too high or too low, and is prompted to guess another number.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Two!''' Define a function called ''add'' that takes two natural numbers and returns their sum. There's a catch, though. You're not allowed to use the + operator. However, you are allowed to use these two functions that I supply you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function add1 (n : int) : int&lt;br /&gt;
    result n + 1&lt;br /&gt;
end add1&lt;br /&gt;
function sub1 (n : int) : int&lt;br /&gt;
    result n - 1&lt;br /&gt;
end sub1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Three''' Define a function called ''multiply'' that takes two natural numbers and returns their product. Once again, there's a catch. You're not allowed to use the built in * operator. (You are allowed to use the + operator though.) You can, however, use the following two functions that I provide you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function double (n : int) : int&lt;br /&gt;
    result n + n&lt;br /&gt;
end double&lt;br /&gt;
&lt;br /&gt;
% The halve function performs integer division by 2.&lt;br /&gt;
% Examples: halve (8) -&amp;gt; 4&lt;br /&gt;
%           halve (17) -&amp;gt; 8&lt;br /&gt;
function halve (n : int) : int&lt;br /&gt;
    result floor (n / 2)&lt;br /&gt;
end halve&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Hint: You should use the binary definition of a natural number. That is, a natural number is one of&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*2*k where k is a natural number&lt;br /&gt;
*2*k + 1 where k is a natural number&lt;br /&gt;
&lt;br /&gt;
That is why I gave you the double and halve functions. It's possible to write a solution using the unary definition of a natural number (sort of like in the solution to problem two), but it will be much slower than the solution using the binary definition.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
I hope you find these exercises in recursion interesting. At this point, we've learned a small amount of the Turing language, but you see we can do some pretty powerful things using this concept of recursion.&lt;br /&gt;
&lt;br /&gt;
Even if we don't take advantage of recursion, however, the topics covered in this tutorial still give us considerable power. They allow us to organize our code. We can save ourselves from repeating ourselves. We can generalize by using parameters. Indeed, the rather simple idea of factoring out common code has led to powerful advances.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
&lt;br /&gt;
===Problem One===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var the_num := Rand.Int (1, 100)&lt;br /&gt;
&lt;br /&gt;
proc guess&lt;br /&gt;
    put &amp;quot;Please input a guess: &amp;quot; ..&lt;br /&gt;
    var g : int&lt;br /&gt;
    get g&lt;br /&gt;
    if g = the_num then&lt;br /&gt;
        put &amp;quot;You've got it!&amp;quot;&lt;br /&gt;
    elsif g &amp;lt; the_num then&lt;br /&gt;
        put &amp;quot;You've guessed too low. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    else&lt;br /&gt;
        put &amp;quot;You've guessed too high. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    end if&lt;br /&gt;
end guess&lt;br /&gt;
&lt;br /&gt;
guess&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Two===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by increasing n2 by one as we &lt;br /&gt;
% decrease n1 by one. If n2 = 0, then we just return n1.&lt;br /&gt;
% Essentially, it works on the following identity:&lt;br /&gt;
%    n1 + n2 = (n1 + 1) + (n2 - 1)&lt;br /&gt;
function add (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 0 then&lt;br /&gt;
        result n1&lt;br /&gt;
    else&lt;br /&gt;
        result add (add1 (n1), sub1 (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end add&lt;br /&gt;
&lt;br /&gt;
% Note that this function could be made more efficient if, instead &lt;br /&gt;
% of blindly choosing to decrease n2 and increase n1, we chose to&lt;br /&gt;
% decrease the smaller of n1 and n2 and increase the larger of n1 and n2.&lt;br /&gt;
% Try writing another function that calls this add function in that way.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Three===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by decreasing n2 and increasing n1.&lt;br /&gt;
% It works on the following identity:&lt;br /&gt;
% If n2 is even:  n1 * n2 = double (n1) * halve (n2)&lt;br /&gt;
% If n2 is odd:   n1 * n2 = n1 + double (n1) * halve (n2)&lt;br /&gt;
function multiply (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 1 then&lt;br /&gt;
        result n1&lt;br /&gt;
    elsif n2 mod 2 = 0 then              % n2 is even&lt;br /&gt;
        result multiply (double (n1), halve (n2))&lt;br /&gt;
    else                                 % n2 is odd&lt;br /&gt;
        result n1 + multiply (double (n1), halve (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end multiply&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Credits==&lt;br /&gt;
Authour: [[Cervantes]]&lt;br /&gt;
&lt;br /&gt;
Added by: [[Saad]]&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_String_Manipulation</id>
		<title>Turing String Manipulation</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_String_Manipulation"/>
				<updated>2008-10-12T19:36:18Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: /* Credits */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==String Manipulation In Turing==&lt;br /&gt;
&lt;br /&gt;
Hello thrill-seekers! So you've decided delve into the terrible and mystifying world of string manipulation, aye? For the next half-hour, you will be my slave. Do what I tell you, and you will learn well. Do otherwise, and you shall... not learn well!&lt;br /&gt;
Alright, enough of that. Let's start.&lt;br /&gt;
===What is String Manipulation and why do we care?===&lt;br /&gt;
String manipulation means, quite simply, manipulating strings. For example, if we take a string that the user inputs (such as Paul Simon, as their user name) we might want to greet the user by saying &amp;quot;Hello &amp;quot;, usersFirstName (in this case, Paul). Or what if we want to prevent the program from crashing when the user does something stupid like enter &amp;quot;qr7&amp;quot; when we want them to input an integer?&lt;br /&gt;
&lt;br /&gt;
==Search for a space==&lt;br /&gt;
Strings allow us to do stuff with a specific character from them. For example,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put &amp;quot;Paul Simon&amp;quot; (1 .. 4)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
would output Paul. Or I could do stuff like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if myString (5) = &amp;quot; &amp;quot; then&lt;br /&gt;
      put &amp;quot;I found a space!&amp;quot;&lt;br /&gt;
end if&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
But how do we know that the 5th character of myString is a space? We don't. If we want to find one, we can run through a for loop:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i : 1 .. length (myString)&lt;br /&gt;
      if myString (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
            put &amp;quot;Space found at position &amp;quot;, i&lt;br /&gt;
      end if&lt;br /&gt;
end for&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice the use of the length function. Length returns the length of the string, quite simply. In other words, it returns the number of characters in the string. So length (&amp;quot;a&amp;quot;) would be 1, and length (&amp;quot;12345&amp;quot;) would be 5.&lt;br /&gt;
&lt;br /&gt;
===Find the user's first name===&lt;br /&gt;
Excellent. So now let's say hello to Mr. Simon, only a little less formally:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Simon&amp;quot;&lt;br /&gt;
var firstName : string&lt;br /&gt;
for i : 1 .. length (name)&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
        firstName := name (1 .. i - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, firstName, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We go through the whole string searching for a space. As soon as we find one, we set firstName to be everything before that space, then exit the for loop.&lt;br /&gt;
But what if the user enters his/her full name, ie. Paul Frederic Simon. The program will output &amp;quot;Hello Paul Frederic!&amp;quot; That's not what we want. This is easily fixed, though. We'll just add an exit after we've decided where the first space is.&lt;br /&gt;
What if the user enters only his first name (ie. &amp;quot;Paul&amp;quot;)? The program will crash, because firstName was never assigned anything. We can fix this problem by setting firstName to equal name at the beginning of the program. So, if name contains any spaces, it will chomp them off, otherwise it will just (effectively) use name as the output.&lt;br /&gt;
Final code looks like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Simon&amp;quot;&lt;br /&gt;
var firstName := name&lt;br /&gt;
for i : 1 .. length (name)&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
        firstName := name (1 .. i - 1)&lt;br /&gt;
        exit&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, firstName, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Find the users last name===&lt;br /&gt;
This is easy enough: it's just a small modification on the last code.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Simon&amp;quot;&lt;br /&gt;
var lastName := name&lt;br /&gt;
for i : 1 .. length (name)&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
        lastName := name (i + 1 .. *)&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put &amp;quot;Hello Mr. &amp;quot;, lastName, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note the user of the asterisk (*). The asterisk represents the last character of the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Simon&amp;quot;&lt;br /&gt;
put name (*)&lt;br /&gt;
put name (length(name))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Both of these methods output the same thing. The * is just shorter. Smile&lt;br /&gt;
Also, note that I removed the exit. This way, if the user's name is set to &amp;quot;Paul Frederic Simon&amp;quot;, it will output &amp;quot;Simon&amp;quot; not &amp;quot;Frederic Simon&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===Finding the middle name===&lt;br /&gt;
This one's a little bit trickier.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Frederic Simon&amp;quot;&lt;br /&gt;
var middleName := name&lt;br /&gt;
var firstSpace := 0&lt;br /&gt;
for i : 1 .. length (name)&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; and firstSpace ~= 0 then&lt;br /&gt;
        middleName := name (firstSpace + 1 .. i - 1)&lt;br /&gt;
    end if&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
        firstSpace := i&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put &amp;quot;Hello Mr. &amp;quot;, middleName, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this one, I've used a variable to store the positin of the first space. Then, we continue on looking for the next space. When we find it, we make everything after the first space but before the current space equal to middleName. Also, note the positions of the if statements. Were they to be in reverse order, as soon as a space is found, firstSpace would be given a new value and then the next if statement would be true because firstSpace is no longer 0. So we would end up with an error.&lt;br /&gt;
&lt;br /&gt;
Note that all these things could also be done using the index function.&lt;br /&gt;
&lt;br /&gt;
==Converting variable types==&lt;br /&gt;
This next part is really useful for error-traping. But it's also useful for other things, such as drawing text on the screen using Font.Draw (where you must use a string)&lt;br /&gt;
The Functions&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
strint (s : string [base : int]) : int&lt;br /&gt;
intstr (i : int [, width : int [, base : int]]) : string&lt;br /&gt;
strreal (s : string) : real&lt;br /&gt;
realstr (r : real, width : int) : string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A couple things you need to know. First, these are functions. They return a value. That's what the last : typeSpec means: it tells us what kind of value the function will return. Thus, the strint function returns an integer, whereas the intstr function returns a string. Next, anything inside brackets ( () ) are your parameters. These are the things that you pass into the function. In the strreal function, s : string means that I must pass a string into the function. Anything inside square brackets ( [] ) is optional.&lt;br /&gt;
&lt;br /&gt;
===Error Proofing===&lt;br /&gt;
We'll error proof our integer input. The first and basic way to do integer input is like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var num : int&lt;br /&gt;
get num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
But that crashes if the user enters &amp;quot;y&amp;quot;. So let's fix it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var input : string&lt;br /&gt;
var num : int&lt;br /&gt;
get input&lt;br /&gt;
num := strint (input)&lt;br /&gt;
put num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whoo-whee! It runs! Sure, but we've still got the same problem. The program will still crash if the user enters &amp;quot;y&amp;quot;. Why? Basically, strint cannot turn a &amp;quot;y&amp;quot; into a number. strint must return an integer (we know this because of the last : int). If it can't, it halts the program.&lt;br /&gt;
So, how do we fix this? Well, here's some more nice functions!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
strintok (s : string [, base : int]) : boolean&lt;br /&gt;
strrealok (s : string) : boolean&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
These functions return boolean values: true or false. They return true if the string can be successfully changed into an integer (or real, in the case of the second function) and false if they cannot. No halting. So let's use them.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var input : string&lt;br /&gt;
var num : int&lt;br /&gt;
get input&lt;br /&gt;
if strintok (input) then&lt;br /&gt;
    num := strint (input)&lt;br /&gt;
    put num&lt;br /&gt;
end if&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And there we have it. It's error proofed. (Well, close enough. The user can still crash the program by inputting lots and lots (255, is it?) of characters.)&lt;br /&gt;
The next thing to do would be to force the user to input an integer. We can use a loop and an exit statement for this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var input : string&lt;br /&gt;
var num : int&lt;br /&gt;
loop&lt;br /&gt;
    cls&lt;br /&gt;
    locate (1, 1)&lt;br /&gt;
    put &amp;quot;Enter an integer: &amp;quot; ..&lt;br /&gt;
    get input&lt;br /&gt;
    if strintok (input) then&lt;br /&gt;
        num := strint (input)&lt;br /&gt;
        put num&lt;br /&gt;
        exit&lt;br /&gt;
    else&lt;br /&gt;
        put &amp;quot;That's not a number, silly!&amp;quot;&lt;br /&gt;
        delay (1000)&lt;br /&gt;
    end if&lt;br /&gt;
end loop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We only want to exit when the user enters a integer, like they were told.&lt;br /&gt;
&lt;br /&gt;
===Font.Drawing an integer===&lt;br /&gt;
After error-proofing, this should be really easy.&lt;br /&gt;
If we want to use Font.Draw to put a number on the screen, we have to first convert it to a string. Why? Because Font.Draw expects it's first parameter to be a string. We know this because:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Font.Draw (textStr : string, x, y, fontID, Colour : int)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Alright, so say we want to draw a number on the screen.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var font := Font.New (&amp;quot;Garamond:26:bold&amp;quot;)&lt;br /&gt;
var num : int&lt;br /&gt;
get num %error proof this!&lt;br /&gt;
Font.Draw (intstr (num), 100, 100, font, black)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Easy enough. We could also have simply done&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Font.Draw (intstr (10), 100, 100, font, black)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
But the first way gets you to error proof it for me. Mwahaha!&lt;br /&gt;
&lt;br /&gt;
==ord and chr==&lt;br /&gt;
Next up, we learn about two new functions.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ord (ch : char) : int&lt;br /&gt;
chr (i : int) : char&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
ord takes a single character and returns an integer (that is specific to that character).&lt;br /&gt;
chr takes an integer and returns a single character (that is specific to that integer).&lt;br /&gt;
To use these, we need to haul out our ASCII chart. So, open Turing, open the Turing Help Manual (that's code for &amp;quot;press F10&amp;quot;), expand Turing Language, select Keystroke Codes.&lt;br /&gt;
Find the letter &amp;quot;A&amp;quot;. It's ordinal value is 65. So&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put ord (&amp;quot;A&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
would output 65. Similarly,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put chr (65)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
would output the letter &amp;quot;A&amp;quot; (without the quotes).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var s : char := &amp;quot;A&amp;quot;&lt;br /&gt;
put chr (ord (s))&lt;br /&gt;
&lt;br /&gt;
var i : int := 65&lt;br /&gt;
put ord (chr (i))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For any character, s, chr (ord(s)) = s. Also, for any integer, i, ord (chr (i)) = i.&lt;br /&gt;
These functions are useful for a variety of things. For example, say you want to loop until the user presses Ctrl + Backspace:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var input : string (1)&lt;br /&gt;
loop&lt;br /&gt;
    getch (input)&lt;br /&gt;
    exit when ord (input) = 127 %crtl + backspace&lt;br /&gt;
end loop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or, say you want to output the alphabet to the screen, in lower case letters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i : 97 .. 122&lt;br /&gt;
    put chr (i), &amp;quot; &amp;quot; ..&lt;br /&gt;
end for&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
How about converting a string from upper case to lower case?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var upperCaseString := &amp;quot;ROOOOAAAR&amp;quot;&lt;br /&gt;
var lowerCaseString := &amp;quot;&amp;quot;&lt;br /&gt;
for i : 1 .. length (upperCaseString)&lt;br /&gt;
    lowerCaseString += chr (ord (upperCaseString (i)) + 32)&lt;br /&gt;
end for&lt;br /&gt;
put lowerCaseString&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don't already know what += means, it simply incriments the variable by whatever is after it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
num += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
is the same as&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
num := num + 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
When using strings, it just adds the string (or character) to the end of my string.&lt;br /&gt;
Note that the ordinal value of any lower case letter is equal to the ordinal value of any upper case letter + 32.&lt;br /&gt;
Note that this doesn't quite work if your string contains things other than numbers. To fix this:&lt;br /&gt;
Turing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var upperCaseString := &amp;quot;RAWR!!  HERE ME ROAR!!&amp;quot;&lt;br /&gt;
var lowerCaseString := &amp;quot;&amp;quot;&lt;br /&gt;
for i : 1 .. length (upperCaseString)&lt;br /&gt;
    if ord (upperCaseString (i)) &amp;gt;= 65 and ord (upperCaseString (i)) &amp;lt;= 90 then %if it is a capital letter&lt;br /&gt;
        lowerCaseString += chr (ord (upperCaseString (i)) + 32)&lt;br /&gt;
    else&lt;br /&gt;
        lowerCaseString += upperCaseString (i)&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put lowerCaseString&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Yay! Next up, let's try outputting &amp;quot;Aa Bb Cc ... Yy Zz&amp;quot;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i : 65 .. 90&lt;br /&gt;
    put chr (i), chr (i + 32), &amp;quot; &amp;quot; ..&lt;br /&gt;
end for&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
So there you have it, string manipulation in a really big nutshell that took a long time to write. Aah, let's let Asian sum things up for me, my fingers are tired:&lt;br /&gt;
AsianSensation wrote:&lt;br /&gt;
&lt;br /&gt;
Know this though, string manipulation comes with practice, and problem solving is a big part of this. So don't assume knowing all about index will make sure you will be able to solve a question. Index is merely a tool, not the solution.&lt;br /&gt;
&lt;br /&gt;
Replace the word &amp;quot;index&amp;quot; with the words &amp;quot;the stuff covered in this tutorial, whatever that may be&amp;quot; and heed his advice (or wisdom).&lt;br /&gt;
&lt;br /&gt;
Happy string manipulating&lt;br /&gt;
&lt;br /&gt;
==Credits==&lt;br /&gt;
Authour: [[Cervantes]]&lt;br /&gt;
&lt;br /&gt;
Added by: [[Saad]]&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_String_Manipulation</id>
		<title>Turing String Manipulation</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_String_Manipulation"/>
				<updated>2008-10-12T19:35:42Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==String Manipulation In Turing==&lt;br /&gt;
&lt;br /&gt;
Hello thrill-seekers! So you've decided delve into the terrible and mystifying world of string manipulation, aye? For the next half-hour, you will be my slave. Do what I tell you, and you will learn well. Do otherwise, and you shall... not learn well!&lt;br /&gt;
Alright, enough of that. Let's start.&lt;br /&gt;
===What is String Manipulation and why do we care?===&lt;br /&gt;
String manipulation means, quite simply, manipulating strings. For example, if we take a string that the user inputs (such as Paul Simon, as their user name) we might want to greet the user by saying &amp;quot;Hello &amp;quot;, usersFirstName (in this case, Paul). Or what if we want to prevent the program from crashing when the user does something stupid like enter &amp;quot;qr7&amp;quot; when we want them to input an integer?&lt;br /&gt;
&lt;br /&gt;
==Search for a space==&lt;br /&gt;
Strings allow us to do stuff with a specific character from them. For example,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put &amp;quot;Paul Simon&amp;quot; (1 .. 4)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
would output Paul. Or I could do stuff like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if myString (5) = &amp;quot; &amp;quot; then&lt;br /&gt;
      put &amp;quot;I found a space!&amp;quot;&lt;br /&gt;
end if&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
But how do we know that the 5th character of myString is a space? We don't. If we want to find one, we can run through a for loop:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i : 1 .. length (myString)&lt;br /&gt;
      if myString (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
            put &amp;quot;Space found at position &amp;quot;, i&lt;br /&gt;
      end if&lt;br /&gt;
end for&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice the use of the length function. Length returns the length of the string, quite simply. In other words, it returns the number of characters in the string. So length (&amp;quot;a&amp;quot;) would be 1, and length (&amp;quot;12345&amp;quot;) would be 5.&lt;br /&gt;
&lt;br /&gt;
===Find the user's first name===&lt;br /&gt;
Excellent. So now let's say hello to Mr. Simon, only a little less formally:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Simon&amp;quot;&lt;br /&gt;
var firstName : string&lt;br /&gt;
for i : 1 .. length (name)&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
        firstName := name (1 .. i - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, firstName, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We go through the whole string searching for a space. As soon as we find one, we set firstName to be everything before that space, then exit the for loop.&lt;br /&gt;
But what if the user enters his/her full name, ie. Paul Frederic Simon. The program will output &amp;quot;Hello Paul Frederic!&amp;quot; That's not what we want. This is easily fixed, though. We'll just add an exit after we've decided where the first space is.&lt;br /&gt;
What if the user enters only his first name (ie. &amp;quot;Paul&amp;quot;)? The program will crash, because firstName was never assigned anything. We can fix this problem by setting firstName to equal name at the beginning of the program. So, if name contains any spaces, it will chomp them off, otherwise it will just (effectively) use name as the output.&lt;br /&gt;
Final code looks like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Simon&amp;quot;&lt;br /&gt;
var firstName := name&lt;br /&gt;
for i : 1 .. length (name)&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
        firstName := name (1 .. i - 1)&lt;br /&gt;
        exit&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, firstName, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Find the users last name===&lt;br /&gt;
This is easy enough: it's just a small modification on the last code.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Simon&amp;quot;&lt;br /&gt;
var lastName := name&lt;br /&gt;
for i : 1 .. length (name)&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
        lastName := name (i + 1 .. *)&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put &amp;quot;Hello Mr. &amp;quot;, lastName, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note the user of the asterisk (*). The asterisk represents the last character of the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Simon&amp;quot;&lt;br /&gt;
put name (*)&lt;br /&gt;
put name (length(name))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Both of these methods output the same thing. The * is just shorter. Smile&lt;br /&gt;
Also, note that I removed the exit. This way, if the user's name is set to &amp;quot;Paul Frederic Simon&amp;quot;, it will output &amp;quot;Simon&amp;quot; not &amp;quot;Frederic Simon&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===Finding the middle name===&lt;br /&gt;
This one's a little bit trickier.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Frederic Simon&amp;quot;&lt;br /&gt;
var middleName := name&lt;br /&gt;
var firstSpace := 0&lt;br /&gt;
for i : 1 .. length (name)&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; and firstSpace ~= 0 then&lt;br /&gt;
        middleName := name (firstSpace + 1 .. i - 1)&lt;br /&gt;
    end if&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
        firstSpace := i&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put &amp;quot;Hello Mr. &amp;quot;, middleName, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this one, I've used a variable to store the positin of the first space. Then, we continue on looking for the next space. When we find it, we make everything after the first space but before the current space equal to middleName. Also, note the positions of the if statements. Were they to be in reverse order, as soon as a space is found, firstSpace would be given a new value and then the next if statement would be true because firstSpace is no longer 0. So we would end up with an error.&lt;br /&gt;
&lt;br /&gt;
Note that all these things could also be done using the index function.&lt;br /&gt;
&lt;br /&gt;
==Converting variable types==&lt;br /&gt;
This next part is really useful for error-traping. But it's also useful for other things, such as drawing text on the screen using Font.Draw (where you must use a string)&lt;br /&gt;
The Functions&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
strint (s : string [base : int]) : int&lt;br /&gt;
intstr (i : int [, width : int [, base : int]]) : string&lt;br /&gt;
strreal (s : string) : real&lt;br /&gt;
realstr (r : real, width : int) : string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A couple things you need to know. First, these are functions. They return a value. That's what the last : typeSpec means: it tells us what kind of value the function will return. Thus, the strint function returns an integer, whereas the intstr function returns a string. Next, anything inside brackets ( () ) are your parameters. These are the things that you pass into the function. In the strreal function, s : string means that I must pass a string into the function. Anything inside square brackets ( [] ) is optional.&lt;br /&gt;
&lt;br /&gt;
===Error Proofing===&lt;br /&gt;
We'll error proof our integer input. The first and basic way to do integer input is like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var num : int&lt;br /&gt;
get num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
But that crashes if the user enters &amp;quot;y&amp;quot;. So let's fix it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var input : string&lt;br /&gt;
var num : int&lt;br /&gt;
get input&lt;br /&gt;
num := strint (input)&lt;br /&gt;
put num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whoo-whee! It runs! Sure, but we've still got the same problem. The program will still crash if the user enters &amp;quot;y&amp;quot;. Why? Basically, strint cannot turn a &amp;quot;y&amp;quot; into a number. strint must return an integer (we know this because of the last : int). If it can't, it halts the program.&lt;br /&gt;
So, how do we fix this? Well, here's some more nice functions!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
strintok (s : string [, base : int]) : boolean&lt;br /&gt;
strrealok (s : string) : boolean&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
These functions return boolean values: true or false. They return true if the string can be successfully changed into an integer (or real, in the case of the second function) and false if they cannot. No halting. So let's use them.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var input : string&lt;br /&gt;
var num : int&lt;br /&gt;
get input&lt;br /&gt;
if strintok (input) then&lt;br /&gt;
    num := strint (input)&lt;br /&gt;
    put num&lt;br /&gt;
end if&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And there we have it. It's error proofed. (Well, close enough. The user can still crash the program by inputting lots and lots (255, is it?) of characters.)&lt;br /&gt;
The next thing to do would be to force the user to input an integer. We can use a loop and an exit statement for this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var input : string&lt;br /&gt;
var num : int&lt;br /&gt;
loop&lt;br /&gt;
    cls&lt;br /&gt;
    locate (1, 1)&lt;br /&gt;
    put &amp;quot;Enter an integer: &amp;quot; ..&lt;br /&gt;
    get input&lt;br /&gt;
    if strintok (input) then&lt;br /&gt;
        num := strint (input)&lt;br /&gt;
        put num&lt;br /&gt;
        exit&lt;br /&gt;
    else&lt;br /&gt;
        put &amp;quot;That's not a number, silly!&amp;quot;&lt;br /&gt;
        delay (1000)&lt;br /&gt;
    end if&lt;br /&gt;
end loop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We only want to exit when the user enters a integer, like they were told.&lt;br /&gt;
&lt;br /&gt;
===Font.Drawing an integer===&lt;br /&gt;
After error-proofing, this should be really easy.&lt;br /&gt;
If we want to use Font.Draw to put a number on the screen, we have to first convert it to a string. Why? Because Font.Draw expects it's first parameter to be a string. We know this because:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Font.Draw (textStr : string, x, y, fontID, Colour : int)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Alright, so say we want to draw a number on the screen.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var font := Font.New (&amp;quot;Garamond:26:bold&amp;quot;)&lt;br /&gt;
var num : int&lt;br /&gt;
get num %error proof this!&lt;br /&gt;
Font.Draw (intstr (num), 100, 100, font, black)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Easy enough. We could also have simply done&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Font.Draw (intstr (10), 100, 100, font, black)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
But the first way gets you to error proof it for me. Mwahaha!&lt;br /&gt;
&lt;br /&gt;
==ord and chr==&lt;br /&gt;
Next up, we learn about two new functions.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ord (ch : char) : int&lt;br /&gt;
chr (i : int) : char&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
ord takes a single character and returns an integer (that is specific to that character).&lt;br /&gt;
chr takes an integer and returns a single character (that is specific to that integer).&lt;br /&gt;
To use these, we need to haul out our ASCII chart. So, open Turing, open the Turing Help Manual (that's code for &amp;quot;press F10&amp;quot;), expand Turing Language, select Keystroke Codes.&lt;br /&gt;
Find the letter &amp;quot;A&amp;quot;. It's ordinal value is 65. So&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put ord (&amp;quot;A&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
would output 65. Similarly,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put chr (65)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
would output the letter &amp;quot;A&amp;quot; (without the quotes).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var s : char := &amp;quot;A&amp;quot;&lt;br /&gt;
put chr (ord (s))&lt;br /&gt;
&lt;br /&gt;
var i : int := 65&lt;br /&gt;
put ord (chr (i))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For any character, s, chr (ord(s)) = s. Also, for any integer, i, ord (chr (i)) = i.&lt;br /&gt;
These functions are useful for a variety of things. For example, say you want to loop until the user presses Ctrl + Backspace:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var input : string (1)&lt;br /&gt;
loop&lt;br /&gt;
    getch (input)&lt;br /&gt;
    exit when ord (input) = 127 %crtl + backspace&lt;br /&gt;
end loop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or, say you want to output the alphabet to the screen, in lower case letters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i : 97 .. 122&lt;br /&gt;
    put chr (i), &amp;quot; &amp;quot; ..&lt;br /&gt;
end for&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
How about converting a string from upper case to lower case?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var upperCaseString := &amp;quot;ROOOOAAAR&amp;quot;&lt;br /&gt;
var lowerCaseString := &amp;quot;&amp;quot;&lt;br /&gt;
for i : 1 .. length (upperCaseString)&lt;br /&gt;
    lowerCaseString += chr (ord (upperCaseString (i)) + 32)&lt;br /&gt;
end for&lt;br /&gt;
put lowerCaseString&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don't already know what += means, it simply incriments the variable by whatever is after it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
num += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
is the same as&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
num := num + 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
When using strings, it just adds the string (or character) to the end of my string.&lt;br /&gt;
Note that the ordinal value of any lower case letter is equal to the ordinal value of any upper case letter + 32.&lt;br /&gt;
Note that this doesn't quite work if your string contains things other than numbers. To fix this:&lt;br /&gt;
Turing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var upperCaseString := &amp;quot;RAWR!!  HERE ME ROAR!!&amp;quot;&lt;br /&gt;
var lowerCaseString := &amp;quot;&amp;quot;&lt;br /&gt;
for i : 1 .. length (upperCaseString)&lt;br /&gt;
    if ord (upperCaseString (i)) &amp;gt;= 65 and ord (upperCaseString (i)) &amp;lt;= 90 then %if it is a capital letter&lt;br /&gt;
        lowerCaseString += chr (ord (upperCaseString (i)) + 32)&lt;br /&gt;
    else&lt;br /&gt;
        lowerCaseString += upperCaseString (i)&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put lowerCaseString&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Yay! Next up, let's try outputting &amp;quot;Aa Bb Cc ... Yy Zz&amp;quot;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i : 65 .. 90&lt;br /&gt;
    put chr (i), chr (i + 32), &amp;quot; &amp;quot; ..&lt;br /&gt;
end for&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
So there you have it, string manipulation in a really big nutshell that took a long time to write. Aah, let's let Asian sum things up for me, my fingers are tired:&lt;br /&gt;
AsianSensation wrote:&lt;br /&gt;
&lt;br /&gt;
Know this though, string manipulation comes with practice, and problem solving is a big part of this. So don't assume knowing all about index will make sure you will be able to solve a question. Index is merely a tool, not the solution.&lt;br /&gt;
&lt;br /&gt;
Replace the word &amp;quot;index&amp;quot; with the words &amp;quot;the stuff covered in this tutorial, whatever that may be&amp;quot; and heed his advice (or wisdom).&lt;br /&gt;
&lt;br /&gt;
Happy string manipulating&lt;br /&gt;
&lt;br /&gt;
==Credits==&lt;br /&gt;
Authour: [[Cervantes]]&lt;br /&gt;
Added by: [[Saad]]&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures</id>
		<title>Turing Functions and Procedures</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures"/>
				<updated>2008-10-12T19:35:20Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Functions and Procedures==&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Thus far, we've learned enough of the Turing language to be able to write some decently large programs. We've learned some fundamentals of Turing -- variables, basic input/output, and if statements. Indeed, these concepts translate relatively smoothly to most any other programming language you might learn. So, having mastered these basics, what's next? Well, having a way to organize our code would be good. Right now, whatever code you write runs linearly, from top to bottom. If you want to run a section of code twice, you have to type it out twice. Wouldn't it be great if we could factor out the commonalities in our code, so we can write less? In truth, we haven't learned very much, so it might be difficult to see the advantage of this. Just bear with me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing the Procedure==&lt;br /&gt;
&lt;br /&gt;
At its simplest, a procedure is just some lines of Turing code. We wrap some special syntax around our lines of Turing code and think up a name for them. After we have defined our procedure, we can ''call'' the procedure by saying its name. When we do this, we run those lines of Turing code that are wrapped inside the procedure definition. This is easiest to see with an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So now the single line of code, ''put &amp;quot;Hello world!&amp;quot;'', can be executed any time we call the ''greet'' procedure. The above code is identical to the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
All I've done is substituted the code inside the ''greet'' procedure for the call to ''greet'' in the last line of the original program.&lt;br /&gt;
&lt;br /&gt;
So, why is this useful? It's useful because now whenever we want to output &amp;quot;Hello world!&amp;quot;, we don't have to type, ''put &amp;quot;Hello world!&amp;quot;'', but only ''greet''. Okay, so we saved on typing fourteen charactes. Not a big deal, right? But what if we wrote a program that said &amp;quot;Hello world!&amp;quot; a lot. Our program said hello to the world ten times during the course of its execution. Now, we're looking back at our program and thinking, &amp;quot;You know, 'Hello world!' isn't really what we want to say. We really are only talking to one person--George. We should instead say 'Hello George!'.&amp;quot; So, now we've got to go into our code and change it. If we used a ''greet'' procedure, this change involves changing the definition of the procedure so that it becomes the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello George!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we hadn't used this procedure, we would have to go through our code, searching for any time we said &amp;quot;Hello world!&amp;quot; and change it to be &amp;quot;Hello George!&amp;quot;. That's a lot of unnecessary work, relative to how effective our ''greet'' solution is.&lt;br /&gt;
&lt;br /&gt;
But now, what if we aren't always talking to George?&lt;br /&gt;
&lt;br /&gt;
==Procedures with Parameters==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure to greet to George and another procedure to greet to Phil and another to greet to Anthony, we should try to write a procedure that can greet to anyone. Here's one possibility:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name : string&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;George&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Phil&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Anthony&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This works, but it's clunky. We've got that variable up there, ''name''. It's what we call a '''global variable'''. It's called that because it is global to the whole program. No matter where we are in our code, we can always see the ''name'' variable. Really, we don't need to keep track of ''name''. We only need it ''inside the greet procedure''. Here's where '''parameters''' are used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Phil&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Anthony&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the definition of our greet procedure, I added that extra bit at the end--the stuff in parentheses. That says that when we call the greet procedure, we must give it one '''argument''', and that argument must be a string. It also says that for all code inside the procedure, there exists a '''constant''' called &amp;quot;name&amp;quot; that has the value we give it when we call the procedure. So, when we call ''greet (&amp;quot;George&amp;quot;)'', we are setting ''name'' to be &amp;quot;George&amp;quot;, and we execute the code inside the procedure. The following two pieces of code are identical:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, &amp;quot;George&amp;quot;, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I mentioned earlier that inside the ''greet'' procedure there exists this constant called name. There are two things we need to understand about it. First, it is constant. We can't change the value of name. Let's get some example code and figure out why:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This code fails because, inside ''greet'', ''name'' is really just a ''reference to &amp;quot;George&amp;quot;''. When we try to redefine name, we're really trying to redefine the string literal &amp;quot;George&amp;quot;. Of course, that's impossible. What if we did this, though?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now you might think that, inside ''greet'', ''name'' is a reference to ''person'', which is a variable, so attempting to redefine ''name'' is really redefining ''person'', and that's okay. However, it's not quite that simple. See, ''greet'' expects a string as its argument, not a variable. In ''greet (person)'', ''person'' is an expression. It gets evaluated down to a string value; it gets evaluated to &amp;quot;George&amp;quot;. Now, once again, we're passing the string literal &amp;quot;George&amp;quot; into ''greet'', and we're back at our first situation.&lt;br /&gt;
&lt;br /&gt;
The second thing we need to understand about ''name'' is that it's '''local'''. It exists only within the procedure. The following code fails:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
put name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''name'' doesn't exist outside of the ''greet'' procedure. It's '''local'''. It's '''scope''' is the ''greet'' procedure. You should also note that each time we call ''greet'' we are creating a new ''name'' constant.&lt;br /&gt;
&lt;br /&gt;
What if we wanted to write a procedure that introduces two people to each other? We need a procedure that can two arguments, not one.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Procedures with Multiple Parameters==&lt;br /&gt;
&lt;br /&gt;
I will now define a procedure, called ''introduce'', that introduces two people to each other:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1 : string, person2 : string)&lt;br /&gt;
    put person1, &amp;quot;, this is &amp;quot;, person2, &amp;quot;.&amp;quot;&lt;br /&gt;
    put person2, &amp;quot;, meet &amp;quot;, person1, &amp;quot;.&amp;quot;&lt;br /&gt;
end introduce&lt;br /&gt;
&lt;br /&gt;
% Call the procedure&lt;br /&gt;
introduce (&amp;quot;Jackie&amp;quot;, &amp;quot;Bruce&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output of this program will be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Jackie, this is Bruce.&lt;br /&gt;
Bruce, meet Jackie.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, reversing the order of the names results in a different output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
introduce (&amp;quot;Bruce&amp;quot;, &amp;quot;Jackie&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bruce, this is Jackie.&lt;br /&gt;
Jackie, meet Bruce.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You should be able to see how this works. When I call ''introduce'', person1 references the first name I give, and person2 references the second name I give. It's based on the ''order'' of the parameters/arguments. (Note some terminology here: person1 and person2 are the parameters; &amp;quot;Bruce&amp;quot; and &amp;quot;Jackie&amp;quot; are the arguments.)&lt;br /&gt;
&lt;br /&gt;
Here's a little, unimportant shortcut. We can declare more than one variable on the same line, provided they are of the same type:&lt;br /&gt;
&amp;lt;pre&amp;gt;var name1, name2 : string&amp;lt;/pre&amp;gt;&lt;br /&gt;
We can do the same with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1, person2 : string)&lt;br /&gt;
    %...&lt;br /&gt;
end introduce&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, I'll give another trivial example. This one will involve writing a procedure that takes two numbers and outputs their product.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure put_product (num1, num2 : real)&lt;br /&gt;
    put num1 * num2&lt;br /&gt;
end put_product&lt;br /&gt;
&lt;br /&gt;
put_product (4, 6)  % Outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, what we're doing is rather silly, isn't it? I mean, aside from the fact that we're defining a procedure to do multiplication, which we can do very quickly anyways. What's silly about this is that we've cornered ourselves. This procedure forces us to send the product to the standard output. What if we wanted to store it in a variable, or draw it in a pretty font, or pass it to some other procedure? Take a minute and think about how we might solve this problem. We want to be able to generalize, here. We want to write something that can multiply two numbers we give it, but still give us the flexibility to do what we want with the answer.&lt;br /&gt;
&lt;br /&gt;
If you're thinking about passing in another parameter that says what we are to do with the prodcut we calculate, then you're thinking way ahead of the game. It's possible, but not easy. There's an easier way.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing Functions==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure that outputs the product of two numbers, we'll write a function that computes the prodcut of two numbers and ''returns that value''. Then we can decide what to do with that number. That's what a function is: it's the same as a procedure, except it returns a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function product (num1, num2 : real) : real&lt;br /&gt;
    result num1 * num2&lt;br /&gt;
end product&lt;br /&gt;
&lt;br /&gt;
put product (4, 6)  % outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There's some new syntax there. First, we're using the keyword, &amp;quot;function&amp;quot;, instead of &amp;quot;procedure&amp;quot;. Next, there's a &amp;quot;: real&amp;quot; after our parameter list. That signifies what type of value our function will return. Also, we've used the '''result''' keyword. Earlier I said that functions return a value. The value returned is given by what is immediately after the '''result''' keyword.&lt;br /&gt;
&lt;br /&gt;
Using a function gives us more flexibility. We don't have to send our answer to the standard output. We could store it in a variable, for instance.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : real := product (1.5, 3)   % n := 4.5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Then we could use that elsewhere:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (n, 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or perhaps better yet:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (product 1.5, 3) 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's now look a little closer at '''result'''. It has an interesting behaviour that we have yet to examine.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Closer Look at 'result'==&lt;br /&gt;
&lt;br /&gt;
A function's sole purpose is to compute and return a value. With that in mind, let's write a ficticious function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function ficticious (number : int) : int&lt;br /&gt;
    result number + 1&lt;br /&gt;
    put &amp;quot;Yarr, it be a cold day.&amp;quot;&lt;br /&gt;
end ficticious&lt;br /&gt;
&lt;br /&gt;
% Call our function and store its answer in a variable&lt;br /&gt;
var n : int := ficticious (4)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
What has happened here? Surely, ''ficticious (4)'' returns the value 5. So ''n'' should be 5. What about that crazy pirate message? Well, as soon as the function computed its value, it returned that value and completed execution. The &amp;quot;Yarr...&amp;quot; message was never output; that line of code was never reached.&lt;br /&gt;
&lt;br /&gt;
This behaviour is a bit irratic. It can make it difficult to understand how a function is working. At the same time, it can be quite useful in shortening code. You've got a find a balance between short code and understandable code.&lt;br /&gt;
&lt;br /&gt;
Soon we will compare and contrast procedures and functions. Before we can do that, however, we need to re-examine what I said earlier about our parameter being constant.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable Parameters==&lt;br /&gt;
&lt;br /&gt;
Let's go back to our first example with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I had argued that ''name'' is constant. We cannot change its value. Indeed, we cannot. However, we can change this code so that ''name'' is in fact variable. All we have to do is specify that the ''name'' parameter is not simply a string, but is a variable that is a string. We do this by using the '''var''' keyword.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (var name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
put &amp;quot;After calling greet, the value of person is now &amp;quot;, person&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Hello George!&lt;br /&gt;
And hello to Freddy as well!&lt;br /&gt;
After calling greet, the value of person is now Freddy&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The difference here is that ''greet'' is expecting to take a string variable, instead of just a string. So when we pass ''person'' to it, ''name'' actually becomes a reference to the variable ''person''. Then, changing the value of ''name'' changes the value of ''person''.&lt;br /&gt;
&lt;br /&gt;
Now, we're familiar with procedures and functions. We now know enough to start thinking about when we should use a procedure and when we should use a function.&lt;br /&gt;
&lt;br /&gt;
==Functions vs. Procedures==&lt;br /&gt;
&lt;br /&gt;
Both functions and procedures are what some call &amp;quot;''subroutines''&amp;quot;--they are something we can use to store code and call at a later point. A function computes and returns a value, whereas a procedure just runs some code. We need to get an understanding of when to use a function and when to use a procedure. Let's look at a pretty famous case study.&lt;br /&gt;
&lt;br /&gt;
In Turing, there are two ways of generating a random integer. The first way is to use the built-in function, ''Rand.Int''. (This is a function just like what we've been writing so far. Don't let the fact that it has a period in it's name fool you.) ''Rand.Int'' takes two numbers, a low and a high; it produces a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
n := Rand.Int (4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now n is a random integer between 4 and 11.&lt;br /&gt;
&lt;br /&gt;
The second way to produce random integers is to use the procedure, ''randint''. This procedure first takes a variable (like our ''greet'' procedure did in the last section) and also takes two numbers, a low and a high. ''randint'' takes the variable you give it and sets it to a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, which is better: ''Rand.Int'' or ''randint''? The correct answer is ''Rand.Int''. Here's why:&lt;br /&gt;
&lt;br /&gt;
First, ''Rand.Int'' is more flexible. Say you wrote a function called &amp;quot;squeeze&amp;quot; that took in an integer. You want to give it a random integer between 1 and 6. Let's look at the two options:&lt;br /&gt;
&amp;lt;pre&amp;gt;squeeze (Rand.Int (1, 6))&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 1, 6)&lt;br /&gt;
squeeze (n)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Clearly the ''Rand.Int'' version is cleaner. There's no polluting the global namespace with useless variables like &amp;quot;''n''&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The second reason is a bit more theoretical, but is of equal importance. ''randint'' takes in a variable and does something to it. We don't really know what. We have trust the documentation. Now, we've also got to trust the documentation of ''Rand.Int''. However, there's an important difference. With ''Rand.Int'', we assign to our variable the value returned by the function. With ''randint'', we have given our precious variable to the procedure, along with all the permissions necessary for the procedure to slaughter our poor variable. It has complete control. It can write whatever it wants in there. We've suddenly given away control of the ''state'' of our program. (State is a term that collectively means the values of all our variables and also our current location in the execution of the code.) The fewer people we give this control to, the better.&lt;br /&gt;
&lt;br /&gt;
If you're not yet convinced, take a look at this. I'll code a version of ''randint'' for you.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure my_randint (var num : int, low, high : int)&lt;br /&gt;
    if num mod 2 = 0&lt;br /&gt;
        num := low + (num * 2) mod (high - low + 1)&lt;br /&gt;
    else&lt;br /&gt;
        num := low + (num + 2) mod (high - low + 1)&lt;br /&gt;
    end if&lt;br /&gt;
end my_randint&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So ''my_randint'' isn't really random, but it might fool you. Take a look at some sample data:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% I've taken a shortcut here. When I say&lt;br /&gt;
%   my_randint (0, 5, 10)&lt;br /&gt;
% I really mean&lt;br /&gt;
%   var n : int := 0&lt;br /&gt;
%   my_randint (n, 5, 10)&lt;br /&gt;
&lt;br /&gt;
my_randint (0, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (1, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (2, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (3, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (4, 5, 10)  %-&amp;gt; 7&lt;br /&gt;
my_randint (5, 5, 10)  %-&amp;gt; 6&lt;br /&gt;
my_randint (6, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (7, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (8, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (9, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (10, 5, 10) %-&amp;gt; 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I could not have done a similar thing if I were using a function, because I was never given a value for ''num''. (I know what some of you are thinking: &amp;quot;What if I give an uninitialized variable to my_randint? It'll crash.&amp;quot; It's possible that I could hack something together that could take care of this, but that's not the point.) The point of this is to show you that by giving the procedure control of your variable, it can use it that variable for evil purposes, as I have done with this ''my_randint'' procedure.&lt;br /&gt;
&lt;br /&gt;
I sincerely hope this has convinced you that functions are superior to procedures. I'm not saying that functions should be used exclusively. There are times when procedures are needed. Rather, I'm just saying that when you have a choice of using a function or using a procedure, choose the function. Nice functions are ones that compute a value based soley on the parameters they take. They don't rely on any global variables. These are functions that are easily documented. At the other extreme are procedures that use global variables inside their definition and also take in variables and modify them. These procedures are ''dangerous!'' and very hard to document.&lt;br /&gt;
&lt;br /&gt;
There is one last thing for us to discuss. It's a very powerful concept known as &amp;quot;recursion&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Recursion==&lt;br /&gt;
&lt;br /&gt;
I'd like to start this section off with a quotation:&lt;br /&gt;
&amp;lt;pre&amp;gt;To understand recursion, you must first understand recursion.&amp;lt;/pre&amp;gt;&lt;br /&gt;
A recursive subroutine is a subroutine that calls itself. One of the most famous and basic examples is the factorial function. The factorial function takes in a number and returns the product of that number and the factorial of the number minus one. Also, the factorial of one is defined to be one. This is the recursive definition of the function. It translates very easily into code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function factorial (n : int) : int&lt;br /&gt;
    if n = 1 then&lt;br /&gt;
        result 1&lt;br /&gt;
    else&lt;br /&gt;
        result n * factorial (n - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end factorial&lt;br /&gt;
&lt;br /&gt;
put factorial (3)  % Outputs &amp;quot;6&amp;quot;. Note 6 = 3*2*1&lt;br /&gt;
put factorial (5)  % Outputs &amp;quot;120&amp;quot;. Note 120 = 5*4*3*2*1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I will give a condensed trace of the factorial function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
factorial (5)&lt;br /&gt;
(5 * factorial (4))&lt;br /&gt;
(5 * (4 * factorial (3)))&lt;br /&gt;
(5 * (4 * (3 * factorial (2))))&lt;br /&gt;
(5 * (4 * (3 * (2 * factorial (1)))))&lt;br /&gt;
(5 * (4 * (3 * (2 * 1))))&lt;br /&gt;
(5 * (4 * (3 * 2)))&lt;br /&gt;
(5 * (4 * 6))&lt;br /&gt;
(5 * 24)&lt;br /&gt;
120&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I've given the recursive definition of the factorial function. A non-recursive definition is to say that factorial (n) = n * (n-1) * (n-2) * ... * 3 * 2 * 1. if you know about loops, you can code this definition of the factorial function. You can then compare and contrast the two definitions and see which you prefer. I hope you'll prefer this version, but that's for time to decide.&lt;br /&gt;
&lt;br /&gt;
As a segue into the next tutorial (which will be a tutorial on looping constructs), I will now present a recursive procedure that counts down to 1, and then displays, &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure countdown (high : int)&lt;br /&gt;
    if high = 0 then&lt;br /&gt;
        put &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
    else&lt;br /&gt;
        put high, &amp;quot;...&amp;quot;&lt;br /&gt;
        countdown (high - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end countdown&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Calling ''countdown (5)'' produces:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
5...&lt;br /&gt;
4...&lt;br /&gt;
3...&lt;br /&gt;
2...&lt;br /&gt;
1...&lt;br /&gt;
Blast off!!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Question One!''' Create a guessing game. Your program will have a global variable that represents the number the user is trying to guess. Use recursion to write a procedure called ''guess''. When run, ''guess'' prompts the user to input a number. If the guessed number is equal to ''the'' number, a winning message is displayed. Otherwise, the user is told whether his/her guess was too high or too low, and is prompted to guess another number.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Two!''' Define a function called ''add'' that takes two natural numbers and returns their sum. There's a catch, though. You're not allowed to use the + operator. However, you are allowed to use these two functions that I supply you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function add1 (n : int) : int&lt;br /&gt;
    result n + 1&lt;br /&gt;
end add1&lt;br /&gt;
function sub1 (n : int) : int&lt;br /&gt;
    result n - 1&lt;br /&gt;
end sub1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Three''' Define a function called ''multiply'' that takes two natural numbers and returns their product. Once again, there's a catch. You're not allowed to use the built in * operator. (You are allowed to use the + operator though.) You can, however, use the following two functions that I provide you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function double (n : int) : int&lt;br /&gt;
    result n + n&lt;br /&gt;
end double&lt;br /&gt;
&lt;br /&gt;
% The halve function performs integer division by 2.&lt;br /&gt;
% Examples: halve (8) -&amp;gt; 4&lt;br /&gt;
%           halve (17) -&amp;gt; 8&lt;br /&gt;
function halve (n : int) : int&lt;br /&gt;
    result floor (n / 2)&lt;br /&gt;
end halve&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Hint: You should use the binary definition of a natural number. That is, a natural number is one of&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*2*k where k is a natural number&lt;br /&gt;
*2*k + 1 where k is a natural number&lt;br /&gt;
&lt;br /&gt;
That is why I gave you the double and halve functions. It's possible to write a solution using the unary definition of a natural number (sort of like in the solution to problem two), but it will be much slower than the solution using the binary definition.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
I hope you find these exercises in recursion interesting. At this point, we've learned a small amount of the Turing language, but you see we can do some pretty powerful things using this concept of recursion.&lt;br /&gt;
&lt;br /&gt;
Even if we don't take advantage of recursion, however, the topics covered in this tutorial still give us considerable power. They allow us to organize our code. We can save ourselves from repeating ourselves. We can generalize by using parameters. Indeed, the rather simple idea of factoring out common code has led to powerful advances.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
&lt;br /&gt;
===Problem One===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var the_num := Rand.Int (1, 100)&lt;br /&gt;
&lt;br /&gt;
proc guess&lt;br /&gt;
    put &amp;quot;Please input a guess: &amp;quot; ..&lt;br /&gt;
    var g : int&lt;br /&gt;
    get g&lt;br /&gt;
    if g = the_num then&lt;br /&gt;
        put &amp;quot;You've got it!&amp;quot;&lt;br /&gt;
    elsif g &amp;lt; the_num then&lt;br /&gt;
        put &amp;quot;You've guessed too low. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    else&lt;br /&gt;
        put &amp;quot;You've guessed too high. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    end if&lt;br /&gt;
end guess&lt;br /&gt;
&lt;br /&gt;
guess&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Two===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by increasing n2 by one as we &lt;br /&gt;
% decrease n1 by one. If n2 = 0, then we just return n1.&lt;br /&gt;
% Essentially, it works on the following identity:&lt;br /&gt;
%    n1 + n2 = (n1 + 1) + (n2 - 1)&lt;br /&gt;
function add (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 0 then&lt;br /&gt;
        result n1&lt;br /&gt;
    else&lt;br /&gt;
        result add (add1 (n1), sub1 (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end add&lt;br /&gt;
&lt;br /&gt;
% Note that this function could be made more efficient if, instead &lt;br /&gt;
% of blindly choosing to decrease n2 and increase n1, we chose to&lt;br /&gt;
% decrease the smaller of n1 and n2 and increase the larger of n1 and n2.&lt;br /&gt;
% Try writing another function that calls this add function in that way.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Three===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by decreasing n2 and increasing n1.&lt;br /&gt;
% It works on the following identity:&lt;br /&gt;
% If n2 is even:  n1 * n2 = double (n1) * halve (n2)&lt;br /&gt;
% If n2 is odd:   n1 * n2 = n1 + double (n1) * halve (n2)&lt;br /&gt;
function multiply (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 1 then&lt;br /&gt;
        result n1&lt;br /&gt;
    elsif n2 mod 2 = 0 then              % n2 is even&lt;br /&gt;
        result multiply (double (n1), halve (n2))&lt;br /&gt;
    else                                 % n2 is odd&lt;br /&gt;
        result n1 + multiply (double (n1), halve (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end multiply&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Credits==&lt;br /&gt;
Authour: [[Cervantes]]&lt;br /&gt;
Added by: [[Saad]]&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures</id>
		<title>Turing Functions and Procedures</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures"/>
				<updated>2008-10-12T19:33:59Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: /* Recursion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Functions and Procedures==&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Thus far, we've learned enough of the Turing language to be able to write some decently large programs. We've learned some fundamentals of Turing -- variables, basic input/output, and if statements. Indeed, these concepts translate relatively smoothly to most any other programming language you might learn. So, having mastered these basics, what's next? Well, having a way to organize our code would be good. Right now, whatever code you write runs linearly, from top to bottom. If you want to run a section of code twice, you have to type it out twice. Wouldn't it be great if we could factor out the commonalities in our code, so we can write less? In truth, we haven't learned very much, so it might be difficult to see the advantage of this. Just bear with me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing the Procedure==&lt;br /&gt;
&lt;br /&gt;
At its simplest, a procedure is just some lines of Turing code. We wrap some special syntax around our lines of Turing code and think up a name for them. After we have defined our procedure, we can ''call'' the procedure by saying its name. When we do this, we run those lines of Turing code that are wrapped inside the procedure definition. This is easiest to see with an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So now the single line of code, ''put &amp;quot;Hello world!&amp;quot;'', can be executed any time we call the ''greet'' procedure. The above code is identical to the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
All I've done is substituted the code inside the ''greet'' procedure for the call to ''greet'' in the last line of the original program.&lt;br /&gt;
&lt;br /&gt;
So, why is this useful? It's useful because now whenever we want to output &amp;quot;Hello world!&amp;quot;, we don't have to type, ''put &amp;quot;Hello world!&amp;quot;'', but only ''greet''. Okay, so we saved on typing fourteen charactes. Not a big deal, right? But what if we wrote a program that said &amp;quot;Hello world!&amp;quot; a lot. Our program said hello to the world ten times during the course of its execution. Now, we're looking back at our program and thinking, &amp;quot;You know, 'Hello world!' isn't really what we want to say. We really are only talking to one person--George. We should instead say 'Hello George!'.&amp;quot; So, now we've got to go into our code and change it. If we used a ''greet'' procedure, this change involves changing the definition of the procedure so that it becomes the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello George!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we hadn't used this procedure, we would have to go through our code, searching for any time we said &amp;quot;Hello world!&amp;quot; and change it to be &amp;quot;Hello George!&amp;quot;. That's a lot of unnecessary work, relative to how effective our ''greet'' solution is.&lt;br /&gt;
&lt;br /&gt;
But now, what if we aren't always talking to George?&lt;br /&gt;
&lt;br /&gt;
==Procedures with Parameters==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure to greet to George and another procedure to greet to Phil and another to greet to Anthony, we should try to write a procedure that can greet to anyone. Here's one possibility:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name : string&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;George&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Phil&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Anthony&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This works, but it's clunky. We've got that variable up there, ''name''. It's what we call a '''global variable'''. It's called that because it is global to the whole program. No matter where we are in our code, we can always see the ''name'' variable. Really, we don't need to keep track of ''name''. We only need it ''inside the greet procedure''. Here's where '''parameters''' are used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Phil&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Anthony&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the definition of our greet procedure, I added that extra bit at the end--the stuff in parentheses. That says that when we call the greet procedure, we must give it one '''argument''', and that argument must be a string. It also says that for all code inside the procedure, there exists a '''constant''' called &amp;quot;name&amp;quot; that has the value we give it when we call the procedure. So, when we call ''greet (&amp;quot;George&amp;quot;)'', we are setting ''name'' to be &amp;quot;George&amp;quot;, and we execute the code inside the procedure. The following two pieces of code are identical:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, &amp;quot;George&amp;quot;, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I mentioned earlier that inside the ''greet'' procedure there exists this constant called name. There are two things we need to understand about it. First, it is constant. We can't change the value of name. Let's get some example code and figure out why:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This code fails because, inside ''greet'', ''name'' is really just a ''reference to &amp;quot;George&amp;quot;''. When we try to redefine name, we're really trying to redefine the string literal &amp;quot;George&amp;quot;. Of course, that's impossible. What if we did this, though?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now you might think that, inside ''greet'', ''name'' is a reference to ''person'', which is a variable, so attempting to redefine ''name'' is really redefining ''person'', and that's okay. However, it's not quite that simple. See, ''greet'' expects a string as its argument, not a variable. In ''greet (person)'', ''person'' is an expression. It gets evaluated down to a string value; it gets evaluated to &amp;quot;George&amp;quot;. Now, once again, we're passing the string literal &amp;quot;George&amp;quot; into ''greet'', and we're back at our first situation.&lt;br /&gt;
&lt;br /&gt;
The second thing we need to understand about ''name'' is that it's '''local'''. It exists only within the procedure. The following code fails:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
put name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''name'' doesn't exist outside of the ''greet'' procedure. It's '''local'''. It's '''scope''' is the ''greet'' procedure. You should also note that each time we call ''greet'' we are creating a new ''name'' constant.&lt;br /&gt;
&lt;br /&gt;
What if we wanted to write a procedure that introduces two people to each other? We need a procedure that can two arguments, not one.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Procedures with Multiple Parameters==&lt;br /&gt;
&lt;br /&gt;
I will now define a procedure, called ''introduce'', that introduces two people to each other:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1 : string, person2 : string)&lt;br /&gt;
    put person1, &amp;quot;, this is &amp;quot;, person2, &amp;quot;.&amp;quot;&lt;br /&gt;
    put person2, &amp;quot;, meet &amp;quot;, person1, &amp;quot;.&amp;quot;&lt;br /&gt;
end introduce&lt;br /&gt;
&lt;br /&gt;
% Call the procedure&lt;br /&gt;
introduce (&amp;quot;Jackie&amp;quot;, &amp;quot;Bruce&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output of this program will be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Jackie, this is Bruce.&lt;br /&gt;
Bruce, meet Jackie.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, reversing the order of the names results in a different output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
introduce (&amp;quot;Bruce&amp;quot;, &amp;quot;Jackie&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bruce, this is Jackie.&lt;br /&gt;
Jackie, meet Bruce.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You should be able to see how this works. When I call ''introduce'', person1 references the first name I give, and person2 references the second name I give. It's based on the ''order'' of the parameters/arguments. (Note some terminology here: person1 and person2 are the parameters; &amp;quot;Bruce&amp;quot; and &amp;quot;Jackie&amp;quot; are the arguments.)&lt;br /&gt;
&lt;br /&gt;
Here's a little, unimportant shortcut. We can declare more than one variable on the same line, provided they are of the same type:&lt;br /&gt;
&amp;lt;pre&amp;gt;var name1, name2 : string&amp;lt;/pre&amp;gt;&lt;br /&gt;
We can do the same with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1, person2 : string)&lt;br /&gt;
    %...&lt;br /&gt;
end introduce&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, I'll give another trivial example. This one will involve writing a procedure that takes two numbers and outputs their product.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure put_product (num1, num2 : real)&lt;br /&gt;
    put num1 * num2&lt;br /&gt;
end put_product&lt;br /&gt;
&lt;br /&gt;
put_product (4, 6)  % Outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, what we're doing is rather silly, isn't it? I mean, aside from the fact that we're defining a procedure to do multiplication, which we can do very quickly anyways. What's silly about this is that we've cornered ourselves. This procedure forces us to send the product to the standard output. What if we wanted to store it in a variable, or draw it in a pretty font, or pass it to some other procedure? Take a minute and think about how we might solve this problem. We want to be able to generalize, here. We want to write something that can multiply two numbers we give it, but still give us the flexibility to do what we want with the answer.&lt;br /&gt;
&lt;br /&gt;
If you're thinking about passing in another parameter that says what we are to do with the prodcut we calculate, then you're thinking way ahead of the game. It's possible, but not easy. There's an easier way.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing Functions==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure that outputs the product of two numbers, we'll write a function that computes the prodcut of two numbers and ''returns that value''. Then we can decide what to do with that number. That's what a function is: it's the same as a procedure, except it returns a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function product (num1, num2 : real) : real&lt;br /&gt;
    result num1 * num2&lt;br /&gt;
end product&lt;br /&gt;
&lt;br /&gt;
put product (4, 6)  % outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There's some new syntax there. First, we're using the keyword, &amp;quot;function&amp;quot;, instead of &amp;quot;procedure&amp;quot;. Next, there's a &amp;quot;: real&amp;quot; after our parameter list. That signifies what type of value our function will return. Also, we've used the '''result''' keyword. Earlier I said that functions return a value. The value returned is given by what is immediately after the '''result''' keyword.&lt;br /&gt;
&lt;br /&gt;
Using a function gives us more flexibility. We don't have to send our answer to the standard output. We could store it in a variable, for instance.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : real := product (1.5, 3)   % n := 4.5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Then we could use that elsewhere:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (n, 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or perhaps better yet:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (product 1.5, 3) 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's now look a little closer at '''result'''. It has an interesting behaviour that we have yet to examine.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Closer Look at 'result'==&lt;br /&gt;
&lt;br /&gt;
A function's sole purpose is to compute and return a value. With that in mind, let's write a ficticious function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function ficticious (number : int) : int&lt;br /&gt;
    result number + 1&lt;br /&gt;
    put &amp;quot;Yarr, it be a cold day.&amp;quot;&lt;br /&gt;
end ficticious&lt;br /&gt;
&lt;br /&gt;
% Call our function and store its answer in a variable&lt;br /&gt;
var n : int := ficticious (4)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
What has happened here? Surely, ''ficticious (4)'' returns the value 5. So ''n'' should be 5. What about that crazy pirate message? Well, as soon as the function computed its value, it returned that value and completed execution. The &amp;quot;Yarr...&amp;quot; message was never output; that line of code was never reached.&lt;br /&gt;
&lt;br /&gt;
This behaviour is a bit irratic. It can make it difficult to understand how a function is working. At the same time, it can be quite useful in shortening code. You've got a find a balance between short code and understandable code.&lt;br /&gt;
&lt;br /&gt;
Soon we will compare and contrast procedures and functions. Before we can do that, however, we need to re-examine what I said earlier about our parameter being constant.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable Parameters==&lt;br /&gt;
&lt;br /&gt;
Let's go back to our first example with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I had argued that ''name'' is constant. We cannot change its value. Indeed, we cannot. However, we can change this code so that ''name'' is in fact variable. All we have to do is specify that the ''name'' parameter is not simply a string, but is a variable that is a string. We do this by using the '''var''' keyword.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (var name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
put &amp;quot;After calling greet, the value of person is now &amp;quot;, person&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Hello George!&lt;br /&gt;
And hello to Freddy as well!&lt;br /&gt;
After calling greet, the value of person is now Freddy&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The difference here is that ''greet'' is expecting to take a string variable, instead of just a string. So when we pass ''person'' to it, ''name'' actually becomes a reference to the variable ''person''. Then, changing the value of ''name'' changes the value of ''person''.&lt;br /&gt;
&lt;br /&gt;
Now, we're familiar with procedures and functions. We now know enough to start thinking about when we should use a procedure and when we should use a function.&lt;br /&gt;
&lt;br /&gt;
==Functions vs. Procedures==&lt;br /&gt;
&lt;br /&gt;
Both functions and procedures are what some call &amp;quot;''subroutines''&amp;quot;--they are something we can use to store code and call at a later point. A function computes and returns a value, whereas a procedure just runs some code. We need to get an understanding of when to use a function and when to use a procedure. Let's look at a pretty famous case study.&lt;br /&gt;
&lt;br /&gt;
In Turing, there are two ways of generating a random integer. The first way is to use the built-in function, ''Rand.Int''. (This is a function just like what we've been writing so far. Don't let the fact that it has a period in it's name fool you.) ''Rand.Int'' takes two numbers, a low and a high; it produces a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
n := Rand.Int (4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now n is a random integer between 4 and 11.&lt;br /&gt;
&lt;br /&gt;
The second way to produce random integers is to use the procedure, ''randint''. This procedure first takes a variable (like our ''greet'' procedure did in the last section) and also takes two numbers, a low and a high. ''randint'' takes the variable you give it and sets it to a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, which is better: ''Rand.Int'' or ''randint''? The correct answer is ''Rand.Int''. Here's why:&lt;br /&gt;
&lt;br /&gt;
First, ''Rand.Int'' is more flexible. Say you wrote a function called &amp;quot;squeeze&amp;quot; that took in an integer. You want to give it a random integer between 1 and 6. Let's look at the two options:&lt;br /&gt;
&amp;lt;pre&amp;gt;squeeze (Rand.Int (1, 6))&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 1, 6)&lt;br /&gt;
squeeze (n)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Clearly the ''Rand.Int'' version is cleaner. There's no polluting the global namespace with useless variables like &amp;quot;''n''&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The second reason is a bit more theoretical, but is of equal importance. ''randint'' takes in a variable and does something to it. We don't really know what. We have trust the documentation. Now, we've also got to trust the documentation of ''Rand.Int''. However, there's an important difference. With ''Rand.Int'', we assign to our variable the value returned by the function. With ''randint'', we have given our precious variable to the procedure, along with all the permissions necessary for the procedure to slaughter our poor variable. It has complete control. It can write whatever it wants in there. We've suddenly given away control of the ''state'' of our program. (State is a term that collectively means the values of all our variables and also our current location in the execution of the code.) The fewer people we give this control to, the better.&lt;br /&gt;
&lt;br /&gt;
If you're not yet convinced, take a look at this. I'll code a version of ''randint'' for you.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure my_randint (var num : int, low, high : int)&lt;br /&gt;
    if num mod 2 = 0&lt;br /&gt;
        num := low + (num * 2) mod (high - low + 1)&lt;br /&gt;
    else&lt;br /&gt;
        num := low + (num + 2) mod (high - low + 1)&lt;br /&gt;
    end if&lt;br /&gt;
end my_randint&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So ''my_randint'' isn't really random, but it might fool you. Take a look at some sample data:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% I've taken a shortcut here. When I say&lt;br /&gt;
%   my_randint (0, 5, 10)&lt;br /&gt;
% I really mean&lt;br /&gt;
%   var n : int := 0&lt;br /&gt;
%   my_randint (n, 5, 10)&lt;br /&gt;
&lt;br /&gt;
my_randint (0, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (1, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (2, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (3, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (4, 5, 10)  %-&amp;gt; 7&lt;br /&gt;
my_randint (5, 5, 10)  %-&amp;gt; 6&lt;br /&gt;
my_randint (6, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (7, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (8, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (9, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (10, 5, 10) %-&amp;gt; 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I could not have done a similar thing if I were using a function, because I was never given a value for ''num''. (I know what some of you are thinking: &amp;quot;What if I give an uninitialized variable to my_randint? It'll crash.&amp;quot; It's possible that I could hack something together that could take care of this, but that's not the point.) The point of this is to show you that by giving the procedure control of your variable, it can use it that variable for evil purposes, as I have done with this ''my_randint'' procedure.&lt;br /&gt;
&lt;br /&gt;
I sincerely hope this has convinced you that functions are superior to procedures. I'm not saying that functions should be used exclusively. There are times when procedures are needed. Rather, I'm just saying that when you have a choice of using a function or using a procedure, choose the function. Nice functions are ones that compute a value based soley on the parameters they take. They don't rely on any global variables. These are functions that are easily documented. At the other extreme are procedures that use global variables inside their definition and also take in variables and modify them. These procedures are ''dangerous!'' and very hard to document.&lt;br /&gt;
&lt;br /&gt;
There is one last thing for us to discuss. It's a very powerful concept known as &amp;quot;recursion&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Recursion==&lt;br /&gt;
&lt;br /&gt;
I'd like to start this section off with a quotation:&lt;br /&gt;
&amp;lt;pre&amp;gt;To understand recursion, you must first understand recursion.&amp;lt;/pre&amp;gt;&lt;br /&gt;
A recursive subroutine is a subroutine that calls itself. One of the most famous and basic examples is the factorial function. The factorial function takes in a number and returns the product of that number and the factorial of the number minus one. Also, the factorial of one is defined to be one. This is the recursive definition of the function. It translates very easily into code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function factorial (n : int) : int&lt;br /&gt;
    if n = 1 then&lt;br /&gt;
        result 1&lt;br /&gt;
    else&lt;br /&gt;
        result n * factorial (n - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end factorial&lt;br /&gt;
&lt;br /&gt;
put factorial (3)  % Outputs &amp;quot;6&amp;quot;. Note 6 = 3*2*1&lt;br /&gt;
put factorial (5)  % Outputs &amp;quot;120&amp;quot;. Note 120 = 5*4*3*2*1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I will give a condensed trace of the factorial function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
factorial (5)&lt;br /&gt;
(5 * factorial (4))&lt;br /&gt;
(5 * (4 * factorial (3)))&lt;br /&gt;
(5 * (4 * (3 * factorial (2))))&lt;br /&gt;
(5 * (4 * (3 * (2 * factorial (1)))))&lt;br /&gt;
(5 * (4 * (3 * (2 * 1))))&lt;br /&gt;
(5 * (4 * (3 * 2)))&lt;br /&gt;
(5 * (4 * 6))&lt;br /&gt;
(5 * 24)&lt;br /&gt;
120&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I've given the recursive definition of the factorial function. A non-recursive definition is to say that factorial (n) = n * (n-1) * (n-2) * ... * 3 * 2 * 1. if you know about loops, you can code this definition of the factorial function. You can then compare and contrast the two definitions and see which you prefer. I hope you'll prefer this version, but that's for time to decide.&lt;br /&gt;
&lt;br /&gt;
As a segue into the next tutorial (which will be a tutorial on looping constructs), I will now present a recursive procedure that counts down to 1, and then displays, &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure countdown (high : int)&lt;br /&gt;
    if high = 0 then&lt;br /&gt;
        put &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
    else&lt;br /&gt;
        put high, &amp;quot;...&amp;quot;&lt;br /&gt;
        countdown (high - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end countdown&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Calling ''countdown (5)'' produces:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
5...&lt;br /&gt;
4...&lt;br /&gt;
3...&lt;br /&gt;
2...&lt;br /&gt;
1...&lt;br /&gt;
Blast off!!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Question One!''' Create a guessing game. Your program will have a global variable that represents the number the user is trying to guess. Use recursion to write a procedure called ''guess''. When run, ''guess'' prompts the user to input a number. If the guessed number is equal to ''the'' number, a winning message is displayed. Otherwise, the user is told whether his/her guess was too high or too low, and is prompted to guess another number.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Two!''' Define a function called ''add'' that takes two natural numbers and returns their sum. There's a catch, though. You're not allowed to use the + operator. However, you are allowed to use these two functions that I supply you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function add1 (n : int) : int&lt;br /&gt;
    result n + 1&lt;br /&gt;
end add1&lt;br /&gt;
function sub1 (n : int) : int&lt;br /&gt;
    result n - 1&lt;br /&gt;
end sub1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Three''' Define a function called ''multiply'' that takes two natural numbers and returns their product. Once again, there's a catch. You're not allowed to use the built in * operator. (You are allowed to use the + operator though.) You can, however, use the following two functions that I provide you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function double (n : int) : int&lt;br /&gt;
    result n + n&lt;br /&gt;
end double&lt;br /&gt;
&lt;br /&gt;
% The halve function performs integer division by 2.&lt;br /&gt;
% Examples: halve (8) -&amp;gt; 4&lt;br /&gt;
%           halve (17) -&amp;gt; 8&lt;br /&gt;
function halve (n : int) : int&lt;br /&gt;
    result floor (n / 2)&lt;br /&gt;
end halve&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Hint: You should use the binary definition of a natural number. That is, a natural number is one of&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*2*k where k is a natural number&lt;br /&gt;
*2*k + 1 where k is a natural number&lt;br /&gt;
&lt;br /&gt;
That is why I gave you the double and halve functions. It's possible to write a solution using the unary definition of a natural number (sort of like in the solution to problem two), but it will be much slower than the solution using the binary definition.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
I hope you find these exercises in recursion interesting. At this point, we've learned a small amount of the Turing language, but you see we can do some pretty powerful things using this concept of recursion.&lt;br /&gt;
&lt;br /&gt;
Even if we don't take advantage of recursion, however, the topics covered in this tutorial still give us considerable power. They allow us to organize our code. We can save ourselves from repeating ourselves. We can generalize by using parameters. Indeed, the rather simple idea of factoring out common code has led to powerful advances.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
&lt;br /&gt;
===Problem One===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var the_num := Rand.Int (1, 100)&lt;br /&gt;
&lt;br /&gt;
proc guess&lt;br /&gt;
    put &amp;quot;Please input a guess: &amp;quot; ..&lt;br /&gt;
    var g : int&lt;br /&gt;
    get g&lt;br /&gt;
    if g = the_num then&lt;br /&gt;
        put &amp;quot;You've got it!&amp;quot;&lt;br /&gt;
    elsif g &amp;lt; the_num then&lt;br /&gt;
        put &amp;quot;You've guessed too low. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    else&lt;br /&gt;
        put &amp;quot;You've guessed too high. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    end if&lt;br /&gt;
end guess&lt;br /&gt;
&lt;br /&gt;
guess&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Two===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by increasing n2 by one as we &lt;br /&gt;
% decrease n1 by one. If n2 = 0, then we just return n1.&lt;br /&gt;
% Essentially, it works on the following identity:&lt;br /&gt;
%    n1 + n2 = (n1 + 1) + (n2 - 1)&lt;br /&gt;
function add (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 0 then&lt;br /&gt;
        result n1&lt;br /&gt;
    else&lt;br /&gt;
        result add (add1 (n1), sub1 (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end add&lt;br /&gt;
&lt;br /&gt;
% Note that this function could be made more efficient if, instead &lt;br /&gt;
% of blindly choosing to decrease n2 and increase n1, we chose to&lt;br /&gt;
% decrease the smaller of n1 and n2 and increase the larger of n1 and n2.&lt;br /&gt;
% Try writing another function that calls this add function in that way.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Three===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by decreasing n2 and increasing n1.&lt;br /&gt;
% It works on the following identity:&lt;br /&gt;
% If n2 is even:  n1 * n2 = double (n1) * halve (n2)&lt;br /&gt;
% If n2 is odd:   n1 * n2 = n1 + double (n1) * halve (n2)&lt;br /&gt;
function multiply (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 1 then&lt;br /&gt;
        result n1&lt;br /&gt;
    elsif n2 mod 2 = 0 then              % n2 is even&lt;br /&gt;
        result multiply (double (n1), halve (n2))&lt;br /&gt;
    else                                 % n2 is odd&lt;br /&gt;
        result n1 + multiply (double (n1), halve (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end multiply&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Credits==&lt;br /&gt;
Authour: [[Cervantes]]&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_Walkthrough</id>
		<title>Turing Walkthrough</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_Walkthrough"/>
				<updated>2008-10-12T19:31:54Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: /* Essentials */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Essentials ==&lt;br /&gt;
&lt;br /&gt;
* '''[[The Start: Introduction, Comments, Output, Variables, Input (Turing)|The Basics]]''' - Introduction to programming, commenting, basic output, variables, and basic input ''by Cervantes''&lt;br /&gt;
* '''If structures''' - Write code that can make decisions ''by Cervantes''&lt;br /&gt;
* '''[[Turing_Functions_and_Procedures|Functions and Procedures]]''' - Organize your code into sections and call them at will; introduction to recursion ''by Cervantes''&lt;br /&gt;
* '''Loops and For loops''' - To repeat sections of code over and over ''by Cervantes''&lt;br /&gt;
* '''[[Turing_String_Manipulation|String Manipulation]]''' ''by Cervantes''&lt;br /&gt;
* '''Index''' - searching through a string ''by AsianSensation''&lt;br /&gt;
* '''Records and Types''' - Create new, compound data types ''by AsianSensation''&lt;br /&gt;
* '''Arrays''' - Mass variables ''by Clayton and Cervantes''&lt;br /&gt;
* '''Enumerated Types''' - Create an indexing system that is readable and understandable ''by Clayton''&lt;br /&gt;
* '''[[Turing_File_IO|File Input/Output]]''' - Reading and writing data from/to text files ''by Clayton''&lt;br /&gt;
* '''Bitwise Operators''' - Experiment with binary, getting closer to lowlevel programming ''by Zylum''&lt;br /&gt;
* '''Recursion''' - Subroutines that call themselves can achieve amazing results ''by Zylum''&lt;br /&gt;
* '''Modules''' - Collections of procedures and functions ''by Delos''&lt;br /&gt;
* '''Pointers''' - Referencing memory and creating linked lists ''by lyam_kaskade''.&lt;br /&gt;
* '''Classes Part I''' - Make your own objects ''by Cervantes''&lt;br /&gt;
* '''Classes Part II''' - Create objects that interact with each other ''by Cervantes''&lt;br /&gt;
* '''Classes Part III''' - Inheritance and Polymorphism to create a network of objects ''by Cervantes''&lt;br /&gt;
* '''Functional Programming (in Turing!)''' - Passing and returning anonymous functions ''by Cervantes''&lt;br /&gt;
&lt;br /&gt;
== Extras ==&lt;br /&gt;
&lt;br /&gt;
=== Graphics ===&lt;br /&gt;
&lt;br /&gt;
* '''Basic Graphics''' - Make your program look nice ''by Asok''&lt;br /&gt;
* '''Fonts and locating put''' - Make your text look nice ''by basketball4ever''&lt;br /&gt;
* '''[[Turing_Eliminate_Flickering|View.Set and View.Update]]''' - Configuring the run window and eliminating animation flickering ''by Clayton''&lt;br /&gt;
* '''Windows''' - Managing the window(s) your program run(s) in ''by Delos''&lt;br /&gt;
* '''Pictures''' - Import your own .bmp's and .jpg's ''by Hacker Dan''&lt;br /&gt;
* '''RGB''' - Create your own colours as you need them ''by TheZsterBunny''&lt;br /&gt;
* '''Intro to 3D''' - Project a 3D world onto the 2D computer screen ''by Windsurfer''&lt;br /&gt;
* '''GUI (Graphical User Interface)''' - Make clickable buttons ''by Recneps''&lt;br /&gt;
&lt;br /&gt;
=== Advanced Input ===&lt;br /&gt;
&lt;br /&gt;
* '''More advanced input''' - getch, Input.KeyDown, Mouse.Where ''by DanShadow''&lt;br /&gt;
* '''Input.KeyDown''' - in more detail ''by Tony''&lt;br /&gt;
* '''Mouse.Where''' - some more detail ''by Hacker Dan''&lt;br /&gt;
&lt;br /&gt;
=== Other Stuff ===&lt;br /&gt;
&lt;br /&gt;
* '''The Dir Module''' - Traverse and manipulate directories ''by jamonathin''&lt;br /&gt;
* '''The Net Module''' - Use Turing and the internet together by ''DanShadow''&lt;br /&gt;
* '''Collision Detection''' - Determine when two objects collide--a necessity for games ''by richcash''&lt;br /&gt;
* '''Whatdotcolour''' - Another approach to collision detection and other useful things&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
Original walkthrough by [[Cervantes]].&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures</id>
		<title>Turing Functions and Procedures</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures"/>
				<updated>2008-10-12T19:31:00Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Functions and Procedures==&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Thus far, we've learned enough of the Turing language to be able to write some decently large programs. We've learned some fundamentals of Turing -- variables, basic input/output, and if statements. Indeed, these concepts translate relatively smoothly to most any other programming language you might learn. So, having mastered these basics, what's next? Well, having a way to organize our code would be good. Right now, whatever code you write runs linearly, from top to bottom. If you want to run a section of code twice, you have to type it out twice. Wouldn't it be great if we could factor out the commonalities in our code, so we can write less? In truth, we haven't learned very much, so it might be difficult to see the advantage of this. Just bear with me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing the Procedure==&lt;br /&gt;
&lt;br /&gt;
At its simplest, a procedure is just some lines of Turing code. We wrap some special syntax around our lines of Turing code and think up a name for them. After we have defined our procedure, we can ''call'' the procedure by saying its name. When we do this, we run those lines of Turing code that are wrapped inside the procedure definition. This is easiest to see with an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So now the single line of code, ''put &amp;quot;Hello world!&amp;quot;'', can be executed any time we call the ''greet'' procedure. The above code is identical to the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% Define the greet procedure&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
% Call the greet procedure&lt;br /&gt;
put &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
All I've done is substituted the code inside the ''greet'' procedure for the call to ''greet'' in the last line of the original program.&lt;br /&gt;
&lt;br /&gt;
So, why is this useful? It's useful because now whenever we want to output &amp;quot;Hello world!&amp;quot;, we don't have to type, ''put &amp;quot;Hello world!&amp;quot;'', but only ''greet''. Okay, so we saved on typing fourteen charactes. Not a big deal, right? But what if we wrote a program that said &amp;quot;Hello world!&amp;quot; a lot. Our program said hello to the world ten times during the course of its execution. Now, we're looking back at our program and thinking, &amp;quot;You know, 'Hello world!' isn't really what we want to say. We really are only talking to one person--George. We should instead say 'Hello George!'.&amp;quot; So, now we've got to go into our code and change it. If we used a ''greet'' procedure, this change involves changing the definition of the procedure so that it becomes the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello George!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we hadn't used this procedure, we would have to go through our code, searching for any time we said &amp;quot;Hello world!&amp;quot; and change it to be &amp;quot;Hello George!&amp;quot;. That's a lot of unnecessary work, relative to how effective our ''greet'' solution is.&lt;br /&gt;
&lt;br /&gt;
But now, what if we aren't always talking to George?&lt;br /&gt;
&lt;br /&gt;
==Procedures with Parameters==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure to greet to George and another procedure to greet to Phil and another to greet to Anthony, we should try to write a procedure that can greet to anyone. Here's one possibility:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name : string&lt;br /&gt;
procedure greet&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;George&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Phil&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&lt;br /&gt;
name := &amp;quot;Anthony&amp;quot;&lt;br /&gt;
greet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This works, but it's clunky. We've got that variable up there, ''name''. It's what we call a '''global variable'''. It's called that because it is global to the whole program. No matter where we are in our code, we can always see the ''name'' variable. Really, we don't need to keep track of ''name''. We only need it ''inside the greet procedure''. Here's where '''parameters''' are used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Phil&amp;quot;)&lt;br /&gt;
greet (&amp;quot;Anthony&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the definition of our greet procedure, I added that extra bit at the end--the stuff in parentheses. That says that when we call the greet procedure, we must give it one '''argument''', and that argument must be a string. It also says that for all code inside the procedure, there exists a '''constant''' called &amp;quot;name&amp;quot; that has the value we give it when we call the procedure. So, when we call ''greet (&amp;quot;George&amp;quot;)'', we are setting ''name'' to be &amp;quot;George&amp;quot;, and we execute the code inside the procedure. The following two pieces of code are identical:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, &amp;quot;George&amp;quot;, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I mentioned earlier that inside the ''greet'' procedure there exists this constant called name. There are two things we need to understand about it. First, it is constant. We can't change the value of name. Let's get some example code and figure out why:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This code fails because, inside ''greet'', ''name'' is really just a ''reference to &amp;quot;George&amp;quot;''. When we try to redefine name, we're really trying to redefine the string literal &amp;quot;George&amp;quot;. Of course, that's impossible. What if we did this, though?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now you might think that, inside ''greet'', ''name'' is a reference to ''person'', which is a variable, so attempting to redefine ''name'' is really redefining ''person'', and that's okay. However, it's not quite that simple. See, ''greet'' expects a string as its argument, not a variable. In ''greet (person)'', ''person'' is an expression. It gets evaluated down to a string value; it gets evaluated to &amp;quot;George&amp;quot;. Now, once again, we're passing the string literal &amp;quot;George&amp;quot; into ''greet'', and we're back at our first situation.&lt;br /&gt;
&lt;br /&gt;
The second thing we need to understand about ''name'' is that it's '''local'''. It exists only within the procedure. The following code fails:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
put name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''name'' doesn't exist outside of the ''greet'' procedure. It's '''local'''. It's '''scope''' is the ''greet'' procedure. You should also note that each time we call ''greet'' we are creating a new ''name'' constant.&lt;br /&gt;
&lt;br /&gt;
What if we wanted to write a procedure that introduces two people to each other? We need a procedure that can two arguments, not one.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Procedures with Multiple Parameters==&lt;br /&gt;
&lt;br /&gt;
I will now define a procedure, called ''introduce'', that introduces two people to each other:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1 : string, person2 : string)&lt;br /&gt;
    put person1, &amp;quot;, this is &amp;quot;, person2, &amp;quot;.&amp;quot;&lt;br /&gt;
    put person2, &amp;quot;, meet &amp;quot;, person1, &amp;quot;.&amp;quot;&lt;br /&gt;
end introduce&lt;br /&gt;
&lt;br /&gt;
% Call the procedure&lt;br /&gt;
introduce (&amp;quot;Jackie&amp;quot;, &amp;quot;Bruce&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output of this program will be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Jackie, this is Bruce.&lt;br /&gt;
Bruce, meet Jackie.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, reversing the order of the names results in a different output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
introduce (&amp;quot;Bruce&amp;quot;, &amp;quot;Jackie&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Bruce, this is Jackie.&lt;br /&gt;
Jackie, meet Bruce.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You should be able to see how this works. When I call ''introduce'', person1 references the first name I give, and person2 references the second name I give. It's based on the ''order'' of the parameters/arguments. (Note some terminology here: person1 and person2 are the parameters; &amp;quot;Bruce&amp;quot; and &amp;quot;Jackie&amp;quot; are the arguments.)&lt;br /&gt;
&lt;br /&gt;
Here's a little, unimportant shortcut. We can declare more than one variable on the same line, provided they are of the same type:&lt;br /&gt;
&amp;lt;pre&amp;gt;var name1, name2 : string&amp;lt;/pre&amp;gt;&lt;br /&gt;
We can do the same with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure introduce (person1, person2 : string)&lt;br /&gt;
    %...&lt;br /&gt;
end introduce&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, I'll give another trivial example. This one will involve writing a procedure that takes two numbers and outputs their product.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure put_product (num1, num2 : real)&lt;br /&gt;
    put num1 * num2&lt;br /&gt;
end put_product&lt;br /&gt;
&lt;br /&gt;
put_product (4, 6)  % Outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, what we're doing is rather silly, isn't it? I mean, aside from the fact that we're defining a procedure to do multiplication, which we can do very quickly anyways. What's silly about this is that we've cornered ourselves. This procedure forces us to send the product to the standard output. What if we wanted to store it in a variable, or draw it in a pretty font, or pass it to some other procedure? Take a minute and think about how we might solve this problem. We want to be able to generalize, here. We want to write something that can multiply two numbers we give it, but still give us the flexibility to do what we want with the answer.&lt;br /&gt;
&lt;br /&gt;
If you're thinking about passing in another parameter that says what we are to do with the prodcut we calculate, then you're thinking way ahead of the game. It's possible, but not easy. There's an easier way.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introducing Functions==&lt;br /&gt;
&lt;br /&gt;
Instead of writing a procedure that outputs the product of two numbers, we'll write a function that computes the prodcut of two numbers and ''returns that value''. Then we can decide what to do with that number. That's what a function is: it's the same as a procedure, except it returns a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function product (num1, num2 : real) : real&lt;br /&gt;
    result num1 * num2&lt;br /&gt;
end product&lt;br /&gt;
&lt;br /&gt;
put product (4, 6)  % outputs 24&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There's some new syntax there. First, we're using the keyword, &amp;quot;function&amp;quot;, instead of &amp;quot;procedure&amp;quot;. Next, there's a &amp;quot;: real&amp;quot; after our parameter list. That signifies what type of value our function will return. Also, we've used the '''result''' keyword. Earlier I said that functions return a value. The value returned is given by what is immediately after the '''result''' keyword.&lt;br /&gt;
&lt;br /&gt;
Using a function gives us more flexibility. We don't have to send our answer to the standard output. We could store it in a variable, for instance.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : real := product (1.5, 3)   % n := 4.5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Then we could use that elsewhere:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (n, 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or perhaps better yet:&lt;br /&gt;
&amp;lt;pre&amp;gt;put product (product 1.5, 3) 2)   % outputs 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's now look a little closer at '''result'''. It has an interesting behaviour that we have yet to examine.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A Closer Look at 'result'==&lt;br /&gt;
&lt;br /&gt;
A function's sole purpose is to compute and return a value. With that in mind, let's write a ficticious function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function ficticious (number : int) : int&lt;br /&gt;
    result number + 1&lt;br /&gt;
    put &amp;quot;Yarr, it be a cold day.&amp;quot;&lt;br /&gt;
end ficticious&lt;br /&gt;
&lt;br /&gt;
% Call our function and store its answer in a variable&lt;br /&gt;
var n : int := ficticious (4)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
What has happened here? Surely, ''ficticious (4)'' returns the value 5. So ''n'' should be 5. What about that crazy pirate message? Well, as soon as the function computed its value, it returned that value and completed execution. The &amp;quot;Yarr...&amp;quot; message was never output; that line of code was never reached.&lt;br /&gt;
&lt;br /&gt;
This behaviour is a bit irratic. It can make it difficult to understand how a function is working. At the same time, it can be quite useful in shortening code. You've got a find a balance between short code and understandable code.&lt;br /&gt;
&lt;br /&gt;
Soon we will compare and contrast procedures and functions. Before we can do that, however, we need to re-examine what I said earlier about our parameter being constant.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable Parameters==&lt;br /&gt;
&lt;br /&gt;
Let's go back to our first example with parameters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
greet (&amp;quot;George&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I had argued that ''name'' is constant. We cannot change its value. Indeed, we cannot. However, we can change this code so that ''name'' is in fact variable. All we have to do is specify that the ''name'' parameter is not simply a string, but is a variable that is a string. We do this by using the '''var''' keyword.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure greet (var name : string)&lt;br /&gt;
    put &amp;quot;Hello &amp;quot;, name, &amp;quot;!&amp;quot;&lt;br /&gt;
    name := &amp;quot;Freddy&amp;quot;&lt;br /&gt;
    put &amp;quot;And hello to &amp;quot;, name, &amp;quot; as well!&amp;quot;&lt;br /&gt;
end greet&lt;br /&gt;
&lt;br /&gt;
var person : string := &amp;quot;George&amp;quot;&lt;br /&gt;
greet (person)&lt;br /&gt;
put &amp;quot;After calling greet, the value of person is now &amp;quot;, person&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Hello George!&lt;br /&gt;
And hello to Freddy as well!&lt;br /&gt;
After calling greet, the value of person is now Freddy&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The difference here is that ''greet'' is expecting to take a string variable, instead of just a string. So when we pass ''person'' to it, ''name'' actually becomes a reference to the variable ''person''. Then, changing the value of ''name'' changes the value of ''person''.&lt;br /&gt;
&lt;br /&gt;
Now, we're familiar with procedures and functions. We now know enough to start thinking about when we should use a procedure and when we should use a function.&lt;br /&gt;
&lt;br /&gt;
==Functions vs. Procedures==&lt;br /&gt;
&lt;br /&gt;
Both functions and procedures are what some call &amp;quot;''subroutines''&amp;quot;--they are something we can use to store code and call at a later point. A function computes and returns a value, whereas a procedure just runs some code. We need to get an understanding of when to use a function and when to use a procedure. Let's look at a pretty famous case study.&lt;br /&gt;
&lt;br /&gt;
In Turing, there are two ways of generating a random integer. The first way is to use the built-in function, ''Rand.Int''. (This is a function just like what we've been writing so far. Don't let the fact that it has a period in it's name fool you.) ''Rand.Int'' takes two numbers, a low and a high; it produces a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
n := Rand.Int (4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now n is a random integer between 4 and 11.&lt;br /&gt;
&lt;br /&gt;
The second way to produce random integers is to use the procedure, ''randint''. This procedure first takes a variable (like our ''greet'' procedure did in the last section) and also takes two numbers, a low and a high. ''randint'' takes the variable you give it and sets it to a random integer between the low and the high. Here's an example of it in use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 4, 11)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, which is better: ''Rand.Int'' or ''randint''? The correct answer is ''Rand.Int''. Here's why:&lt;br /&gt;
&lt;br /&gt;
First, ''Rand.Int'' is more flexible. Say you wrote a function called &amp;quot;squeeze&amp;quot; that took in an integer. You want to give it a random integer between 1 and 6. Let's look at the two options:&lt;br /&gt;
&amp;lt;pre&amp;gt;squeeze (Rand.Int (1, 6))&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var n : int&lt;br /&gt;
randint (n, 1, 6)&lt;br /&gt;
squeeze (n)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Clearly the ''Rand.Int'' version is cleaner. There's no polluting the global namespace with useless variables like &amp;quot;''n''&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The second reason is a bit more theoretical, but is of equal importance. ''randint'' takes in a variable and does something to it. We don't really know what. We have trust the documentation. Now, we've also got to trust the documentation of ''Rand.Int''. However, there's an important difference. With ''Rand.Int'', we assign to our variable the value returned by the function. With ''randint'', we have given our precious variable to the procedure, along with all the permissions necessary for the procedure to slaughter our poor variable. It has complete control. It can write whatever it wants in there. We've suddenly given away control of the ''state'' of our program. (State is a term that collectively means the values of all our variables and also our current location in the execution of the code.) The fewer people we give this control to, the better.&lt;br /&gt;
&lt;br /&gt;
If you're not yet convinced, take a look at this. I'll code a version of ''randint'' for you.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure my_randint (var num : int, low, high : int)&lt;br /&gt;
    if num mod 2 = 0&lt;br /&gt;
        num := low + (num * 2) mod (high - low + 1)&lt;br /&gt;
    else&lt;br /&gt;
        num := low + (num + 2) mod (high - low + 1)&lt;br /&gt;
    end if&lt;br /&gt;
end my_randint&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
So ''my_randint'' isn't really random, but it might fool you. Take a look at some sample data:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% I've taken a shortcut here. When I say&lt;br /&gt;
%   my_randint (0, 5, 10)&lt;br /&gt;
% I really mean&lt;br /&gt;
%   var n : int := 0&lt;br /&gt;
%   my_randint (n, 5, 10)&lt;br /&gt;
&lt;br /&gt;
my_randint (0, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (1, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (2, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (3, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (4, 5, 10)  %-&amp;gt; 7&lt;br /&gt;
my_randint (5, 5, 10)  %-&amp;gt; 6&lt;br /&gt;
my_randint (6, 5, 10)  %-&amp;gt; 5&lt;br /&gt;
my_randint (7, 5, 10)  %-&amp;gt; 8&lt;br /&gt;
my_randint (8, 5, 10)  %-&amp;gt; 9&lt;br /&gt;
my_randint (9, 5, 10)  %-&amp;gt; 10&lt;br /&gt;
my_randint (10, 5, 10) %-&amp;gt; 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I could not have done a similar thing if I were using a function, because I was never given a value for ''num''. (I know what some of you are thinking: &amp;quot;What if I give an uninitialized variable to my_randint? It'll crash.&amp;quot; It's possible that I could hack something together that could take care of this, but that's not the point.) The point of this is to show you that by giving the procedure control of your variable, it can use it that variable for evil purposes, as I have done with this ''my_randint'' procedure.&lt;br /&gt;
&lt;br /&gt;
I sincerely hope this has convinced you that functions are superior to procedures. I'm not saying that functions should be used exclusively. There are times when procedures are needed. Rather, I'm just saying that when you have a choice of using a function or using a procedure, choose the function. Nice functions are ones that compute a value based soley on the parameters they take. They don't rely on any global variables. These are functions that are easily documented. At the other extreme are procedures that use global variables inside their definition and also take in variables and modify them. These procedures are ''dangerous!'' and very hard to document.&lt;br /&gt;
&lt;br /&gt;
There is one last thing for us to discuss. It's a very powerful concept known as &amp;quot;recursion&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Recursion==&lt;br /&gt;
&lt;br /&gt;
I'd like to start this section off with a quotation:&lt;br /&gt;
&amp;lt;pre&amp;gt;To understand recursion, you must first understand recursion.&amp;lt;/pre&amp;gt;&lt;br /&gt;
A recursive subroutine is a subroutine that calls itself. One of the most famous and basic examples is the factorial function. The factorial function takes in a number and returns the product of that number and the factorial of the number minus one. Also, the factorial of one is defined to be one. This is the recursive definition of the function. It translates very easily into code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function factorial (n : int) : int&lt;br /&gt;
    if n = 1 then&lt;br /&gt;
        result 1&lt;br /&gt;
    else&lt;br /&gt;
        result n * factorial (n - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end factorial&lt;br /&gt;
&lt;br /&gt;
put factorial (3)  % Outputs &amp;quot;6&amp;quot;. Note 6 = 3*2*1&lt;br /&gt;
put factorial (5)  % Outputs &amp;quot;120&amp;quot;. Note 120 = 5*4*3*2*1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I will give a condensed trace of the factorial function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
factorial (5)&lt;br /&gt;
(5 * factorial (4))&lt;br /&gt;
(5 * (4 * factorial (3)))&lt;br /&gt;
(5 * (4 * (3 * factorial (2))))&lt;br /&gt;
(5 * (4 * (3 * (2 * factorial (1)))))&lt;br /&gt;
(5 * (4 * (3 * (2 * 1))))&lt;br /&gt;
(5 * (4 * (3 * 2)))&lt;br /&gt;
(5 * (4 * 6))&lt;br /&gt;
(5 * 24)&lt;br /&gt;
120&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I've given the recursive definition of the factorial function. A non-recursive definition is to say that factorial (n) = n * (n-1) * (n-2) * ... * 3 * 2 * 1. Once you've learned about '''loops[''', you can code this definition of the factorial function. You can then compare and contrast the two definitions and see which you prefer. I hope you'll prefer this version, but that's for time to decide.&lt;br /&gt;
&lt;br /&gt;
As a segue into the next tutorial (which will be a tutorial on looping constructs), I will now present a recursive procedure that counts down to 1, and then displays, &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure countdown (high : int)&lt;br /&gt;
    if high = 0 then&lt;br /&gt;
        put &amp;quot;Blast off!!&amp;quot;&lt;br /&gt;
    else&lt;br /&gt;
        put high, &amp;quot;...&amp;quot;&lt;br /&gt;
        countdown (high - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end countdown&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Calling ''countdown (5)'' produces:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
5...&lt;br /&gt;
4...&lt;br /&gt;
3...&lt;br /&gt;
2...&lt;br /&gt;
1...&lt;br /&gt;
Blast off!!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Question One!''' Create a guessing game. Your program will have a global variable that represents the number the user is trying to guess. Use recursion to write a procedure called ''guess''. When run, ''guess'' prompts the user to input a number. If the guessed number is equal to ''the'' number, a winning message is displayed. Otherwise, the user is told whether his/her guess was too high or too low, and is prompted to guess another number.&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Two!''' Define a function called ''add'' that takes two natural numbers and returns their sum. There's a catch, though. You're not allowed to use the + operator. However, you are allowed to use these two functions that I supply you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function add1 (n : int) : int&lt;br /&gt;
    result n + 1&lt;br /&gt;
end add1&lt;br /&gt;
function sub1 (n : int) : int&lt;br /&gt;
    result n - 1&lt;br /&gt;
end sub1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A solution to this problem can be found at the end of the tutorial.&lt;br /&gt;
&lt;br /&gt;
'''Question Three''' Define a function called ''multiply'' that takes two natural numbers and returns their product. Once again, there's a catch. You're not allowed to use the built in * operator. (You are allowed to use the + operator though.) You can, however, use the following two functions that I provide you with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function double (n : int) : int&lt;br /&gt;
    result n + n&lt;br /&gt;
end double&lt;br /&gt;
&lt;br /&gt;
% The halve function performs integer division by 2.&lt;br /&gt;
% Examples: halve (8) -&amp;gt; 4&lt;br /&gt;
%           halve (17) -&amp;gt; 8&lt;br /&gt;
function halve (n : int) : int&lt;br /&gt;
    result floor (n / 2)&lt;br /&gt;
end halve&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Hint: You should use the binary definition of a natural number. That is, a natural number is one of&lt;br /&gt;
&lt;br /&gt;
*1&lt;br /&gt;
*2*k where k is a natural number&lt;br /&gt;
*2*k + 1 where k is a natural number&lt;br /&gt;
&lt;br /&gt;
That is why I gave you the double and halve functions. It's possible to write a solution using the unary definition of a natural number (sort of like in the solution to problem two), but it will be much slower than the solution using the binary definition.&lt;br /&gt;
[size=10]A solution to this problem can be found at the end of the tutorial.[/size]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
I hope you find these exercises in recursion interesting. At this point, we've learned a small amount of the Turing language, but you see we can do some pretty powerful things using this concept of recursion.&lt;br /&gt;
&lt;br /&gt;
Even if we don't take advantage of recursion, however, the topics covered in this tutorial still give us considerable power. They allow us to organize our code. We can save ourselves from repeating ourselves. We can generalize by using parameters. Indeed, the rather simple idea of factoring out common code has led to powerful advances.&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
&lt;br /&gt;
===Problem One===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var the_num := Rand.Int (1, 100)&lt;br /&gt;
&lt;br /&gt;
proc guess&lt;br /&gt;
    put &amp;quot;Please input a guess: &amp;quot; ..&lt;br /&gt;
    var g : int&lt;br /&gt;
    get g&lt;br /&gt;
    if g = the_num then&lt;br /&gt;
        put &amp;quot;You've got it!&amp;quot;&lt;br /&gt;
    elsif g &amp;lt; the_num then&lt;br /&gt;
        put &amp;quot;You've guessed too low. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    else&lt;br /&gt;
        put &amp;quot;You've guessed too high. Try again.&amp;quot;&lt;br /&gt;
        guess&lt;br /&gt;
    end if&lt;br /&gt;
end guess&lt;br /&gt;
&lt;br /&gt;
guess&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Two===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by increasing n2 by one as we &lt;br /&gt;
% decrease n1 by one. If n2 = 0, then we just return n1.&lt;br /&gt;
% Essentially, it works on the following identity:&lt;br /&gt;
%    n1 + n2 = (n1 + 1) + (n2 - 1)&lt;br /&gt;
function add (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 0 then&lt;br /&gt;
        result n1&lt;br /&gt;
    else&lt;br /&gt;
        result add (add1 (n1), sub1 (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end add&lt;br /&gt;
&lt;br /&gt;
% Note that this function could be made more efficient if, instead &lt;br /&gt;
% of blindly choosing to decrease n2 and increase n1, we chose to&lt;br /&gt;
% decrease the smaller of n1 and n2 and increase the larger of n1 and n2.&lt;br /&gt;
% Try writing another function that calls this add function in that way.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Problem Three===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% This function works by decreasing n2 and increasing n1.&lt;br /&gt;
% It works on the following identity:&lt;br /&gt;
% If n2 is even:  n1 * n2 = double (n1) * halve (n2)&lt;br /&gt;
% If n2 is odd:   n1 * n2 = n1 + double (n1) * halve (n2)&lt;br /&gt;
function multiply (n1, n2 : nat) : nat&lt;br /&gt;
    if n2 = 1 then&lt;br /&gt;
        result n1&lt;br /&gt;
    elsif n2 mod 2 = 0 then              % n2 is even&lt;br /&gt;
        result multiply (double (n1), halve (n2))&lt;br /&gt;
    else                                 % n2 is odd&lt;br /&gt;
        result n1 + multiply (double (n1), halve (n2))&lt;br /&gt;
    end if&lt;br /&gt;
end multiply&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Credits==&lt;br /&gt;
Authour: [[Cervantes]]&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_Walkthrough</id>
		<title>Turing Walkthrough</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_Walkthrough"/>
				<updated>2008-10-12T14:24:23Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: /* Essentials */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Essentials ==&lt;br /&gt;
&lt;br /&gt;
* '''The Basics''' - Introduction to programming, commenting, basic output, variables, and basic input ''by Cervantes''&lt;br /&gt;
* '''If structures''' - Write code that can make decisions ''by Cervantes''&lt;br /&gt;
* '''Functions and Procedures''' - Organize your code into sections and call them at will; introduction to recursion ''by Cervantes''&lt;br /&gt;
* '''Loops and For loops''' - To repeat sections of code over and over ''by Cervantes''&lt;br /&gt;
* '''[[Turing_String_Manipulation|String Manipulation]]''' ''by Cervantes''&lt;br /&gt;
* '''Index''' - searching through a string ''by AsianSensation''&lt;br /&gt;
* '''Records and Types''' - Create new, compound data types ''by AsianSensation''&lt;br /&gt;
* '''Arrays''' - Mass variables ''by Freakman and Cervantes''&lt;br /&gt;
* '''Enumerated Types''' - Create an indexing system that is readable and understandable ''by Freakman''&lt;br /&gt;
* '''[[Turing_File_IO|File Input/Output]]''' - Reading and writing data from/to text files ''by Freakman''&lt;br /&gt;
* '''Bitwise Operators''' - Experiment with binary, getting closer to lowlevel programming ''by Zylum''&lt;br /&gt;
* '''Recursion''' - Subroutines that call themselves can achieve amazing results ''by Zylum''&lt;br /&gt;
* '''Modules''' - Collections of procedures and functions ''by Delos''&lt;br /&gt;
* '''Pointers''' - Referencing memory and creating linked lists ''by lyam_kaskade''.&lt;br /&gt;
* '''Classes Part I''' - Make your own objects ''by Cervantes''&lt;br /&gt;
* '''Classes Part II''' - Create objects that interact with each other ''by Cervantes''&lt;br /&gt;
* '''Classes Part III''' - Inheritance and Polymorphism to create a network of objects ''by Cervantes''&lt;br /&gt;
* '''Functional Programming (in Turing!)''' - Passing and returning anonymous functions ''by Cervantes''&lt;br /&gt;
&lt;br /&gt;
== Extras ==&lt;br /&gt;
&lt;br /&gt;
=== Graphics ===&lt;br /&gt;
&lt;br /&gt;
* '''Basic Graphics''' - Make your program look nice ''by Asok''&lt;br /&gt;
* '''Fonts and locating put''' - Make your text look nice ''by basketball4ever''&lt;br /&gt;
* '''View.Set and View.Update''' - Configuring the run window and eliminating animation flickering ''by Freakman''&lt;br /&gt;
* '''Windows''' - Managing the window(s) your program run(s) in ''by Delos''&lt;br /&gt;
* '''Pictures''' - Import your own .bmp's and .jpg's ''by Hacker Dan''&lt;br /&gt;
* '''RGB''' - Create your own colours as you need them ''by TheZsterBunny''&lt;br /&gt;
* '''Intro to 3D''' - Project a 3D world onto the 2D computer screen ''by Windsurfer''&lt;br /&gt;
* '''GUI (Graphical User Interface)''' - Make clickable buttons ''by Recneps''&lt;br /&gt;
&lt;br /&gt;
=== Advanced Input ===&lt;br /&gt;
&lt;br /&gt;
* '''More advanced input''' - getch, Input.KeyDown, Mouse.Where ''by DanShadow''&lt;br /&gt;
* '''Input.KeyDown''' - in more detail ''by Tony''&lt;br /&gt;
* '''Mouse.Where''' - some more detail ''by Hacker Dan''&lt;br /&gt;
&lt;br /&gt;
=== Other Stuff ===&lt;br /&gt;
&lt;br /&gt;
* '''The Dir Module''' - Traverse and manipulate directories ''by jamonathin''&lt;br /&gt;
* '''The Net Module''' - Use Turing and the internet together by ''DanShadow''&lt;br /&gt;
* '''Collision Detection''' - Determine when two objects collide--a necessity for games ''by richcash''&lt;br /&gt;
* '''Whatdotcolour''' - Another approach to collision detection and other useful things&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
Original walkthrough by [[Cervantes]].&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Turing_String_Manipulation</id>
		<title>Turing String Manipulation</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Turing_String_Manipulation"/>
				<updated>2008-10-12T14:22:46Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==String Manipulation In Turing==&lt;br /&gt;
&lt;br /&gt;
Hello thrill-seekers! So you've decided delve into the terrible and mystifying world of string manipulation, aye? For the next half-hour, you will be my slave. Do what I tell you, and you will learn well. Do otherwise, and you shall... not learn well!&lt;br /&gt;
Alright, enough of that. Let's start.&lt;br /&gt;
===What is String Manipulation and why do we care?===&lt;br /&gt;
String manipulation means, quite simply, manipulating strings. For example, if we take a string that the user inputs (such as Paul Simon, as their user name) we might want to greet the user by saying &amp;quot;Hello &amp;quot;, usersFirstName (in this case, Paul). Or what if we want to prevent the program from crashing when the user does something stupid like enter &amp;quot;qr7&amp;quot; when we want them to input an integer?&lt;br /&gt;
&lt;br /&gt;
==Search for a space==&lt;br /&gt;
Strings allow us to do stuff with a specific character from them. For example,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put &amp;quot;Paul Simon&amp;quot; (1 .. 4)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
would output Paul. Or I could do stuff like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if myString (5) = &amp;quot; &amp;quot; then&lt;br /&gt;
      put &amp;quot;I found a space!&amp;quot;&lt;br /&gt;
end if&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
But how do we know that the 5th character of myString is a space? We don't. If we want to find one, we can run through a for loop:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i : 1 .. length (myString)&lt;br /&gt;
      if myString (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
            put &amp;quot;Space found at position &amp;quot;, i&lt;br /&gt;
      end if&lt;br /&gt;
end for&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice the use of the length function. Length returns the length of the string, quite simply. In other words, it returns the number of characters in the string. So length (&amp;quot;a&amp;quot;) would be 1, and length (&amp;quot;12345&amp;quot;) would be 5.&lt;br /&gt;
&lt;br /&gt;
===Find the user's first name===&lt;br /&gt;
Excellent. So now let's say hello to Mr. Simon, only a little less formally:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Simon&amp;quot;&lt;br /&gt;
var firstName : string&lt;br /&gt;
for i : 1 .. length (name)&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
        firstName := name (1 .. i - 1)&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, firstName, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We go through the whole string searching for a space. As soon as we find one, we set firstName to be everything before that space, then exit the for loop.&lt;br /&gt;
But what if the user enters his/her full name, ie. Paul Frederic Simon. The program will output &amp;quot;Hello Paul Frederic!&amp;quot; That's not what we want. This is easily fixed, though. We'll just add an exit after we've decided where the first space is.&lt;br /&gt;
What if the user enters only his first name (ie. &amp;quot;Paul&amp;quot;)? The program will crash, because firstName was never assigned anything. We can fix this problem by setting firstName to equal name at the beginning of the program. So, if name contains any spaces, it will chomp them off, otherwise it will just (effectively) use name as the output.&lt;br /&gt;
Final code looks like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Simon&amp;quot;&lt;br /&gt;
var firstName := name&lt;br /&gt;
for i : 1 .. length (name)&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
        firstName := name (1 .. i - 1)&lt;br /&gt;
        exit&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put &amp;quot;Hello &amp;quot;, firstName, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Find the users last name===&lt;br /&gt;
This is easy enough: it's just a small modification on the last code.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Simon&amp;quot;&lt;br /&gt;
var lastName := name&lt;br /&gt;
for i : 1 .. length (name)&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
        lastName := name (i + 1 .. *)&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put &amp;quot;Hello Mr. &amp;quot;, lastName, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note the user of the asterisk (*). The asterisk represents the last character of the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Simon&amp;quot;&lt;br /&gt;
put name (*)&lt;br /&gt;
put name (length(name))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Both of these methods output the same thing. The * is just shorter. Smile&lt;br /&gt;
Also, note that I removed the exit. This way, if the user's name is set to &amp;quot;Paul Frederic Simon&amp;quot;, it will output &amp;quot;Simon&amp;quot; not &amp;quot;Frederic Simon&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===Finding the middle name===&lt;br /&gt;
This one's a little bit trickier.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var name := &amp;quot;Paul Frederic Simon&amp;quot;&lt;br /&gt;
var middleName := name&lt;br /&gt;
var firstSpace := 0&lt;br /&gt;
for i : 1 .. length (name)&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; and firstSpace ~= 0 then&lt;br /&gt;
        middleName := name (firstSpace + 1 .. i - 1)&lt;br /&gt;
    end if&lt;br /&gt;
    if name (i) = &amp;quot; &amp;quot; then&lt;br /&gt;
        firstSpace := i&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put &amp;quot;Hello Mr. &amp;quot;, middleName, &amp;quot;!&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this one, I've used a variable to store the positin of the first space. Then, we continue on looking for the next space. When we find it, we make everything after the first space but before the current space equal to middleName. Also, note the positions of the if statements. Were they to be in reverse order, as soon as a space is found, firstSpace would be given a new value and then the next if statement would be true because firstSpace is no longer 0. So we would end up with an error.&lt;br /&gt;
&lt;br /&gt;
Note that all these things could also be done using the index function.&lt;br /&gt;
&lt;br /&gt;
==Converting variable types==&lt;br /&gt;
This next part is really useful for error-traping. But it's also useful for other things, such as drawing text on the screen using Font.Draw (where you must use a string)&lt;br /&gt;
The Functions&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
strint (s : string [base : int]) : int&lt;br /&gt;
intstr (i : int [, width : int [, base : int]]) : string&lt;br /&gt;
strreal (s : string) : real&lt;br /&gt;
realstr (r : real, width : int) : string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A couple things you need to know. First, these are functions. They return a value. That's what the last : typeSpec means: it tells us what kind of value the function will return. Thus, the strint function returns an integer, whereas the intstr function returns a string. Next, anything inside brackets ( () ) are your parameters. These are the things that you pass into the function. In the strreal function, s : string means that I must pass a string into the function. Anything inside square brackets ( [] ) is optional.&lt;br /&gt;
&lt;br /&gt;
===Error Proofing===&lt;br /&gt;
We'll error proof our integer input. The first and basic way to do integer input is like so:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var num : int&lt;br /&gt;
get num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
But that crashes if the user enters &amp;quot;y&amp;quot;. So let's fix it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var input : string&lt;br /&gt;
var num : int&lt;br /&gt;
get input&lt;br /&gt;
num := strint (input)&lt;br /&gt;
put num&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whoo-whee! It runs! Sure, but we've still got the same problem. The program will still crash if the user enters &amp;quot;y&amp;quot;. Why? Basically, strint cannot turn a &amp;quot;y&amp;quot; into a number. strint must return an integer (we know this because of the last : int). If it can't, it halts the program.&lt;br /&gt;
So, how do we fix this? Well, here's some more nice functions!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
strintok (s : string [, base : int]) : boolean&lt;br /&gt;
strrealok (s : string) : boolean&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
These functions return boolean values: true or false. They return true if the string can be successfully changed into an integer (or real, in the case of the second function) and false if they cannot. No halting. So let's use them.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var input : string&lt;br /&gt;
var num : int&lt;br /&gt;
get input&lt;br /&gt;
if strintok (input) then&lt;br /&gt;
    num := strint (input)&lt;br /&gt;
    put num&lt;br /&gt;
end if&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And there we have it. It's error proofed. (Well, close enough. The user can still crash the program by inputting lots and lots (255, is it?) of characters.)&lt;br /&gt;
The next thing to do would be to force the user to input an integer. We can use a loop and an exit statement for this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var input : string&lt;br /&gt;
var num : int&lt;br /&gt;
loop&lt;br /&gt;
    cls&lt;br /&gt;
    locate (1, 1)&lt;br /&gt;
    put &amp;quot;Enter an integer: &amp;quot; ..&lt;br /&gt;
    get input&lt;br /&gt;
    if strintok (input) then&lt;br /&gt;
        num := strint (input)&lt;br /&gt;
        put num&lt;br /&gt;
        exit&lt;br /&gt;
    else&lt;br /&gt;
        put &amp;quot;That's not a number, silly!&amp;quot;&lt;br /&gt;
        delay (1000)&lt;br /&gt;
    end if&lt;br /&gt;
end loop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We only want to exit when the user enters a integer, like they were told.&lt;br /&gt;
&lt;br /&gt;
===Font.Drawing an integer===&lt;br /&gt;
After error-proofing, this should be really easy.&lt;br /&gt;
If we want to use Font.Draw to put a number on the screen, we have to first convert it to a string. Why? Because Font.Draw expects it's first parameter to be a string. We know this because:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Font.Draw (textStr : string, x, y, fontID, Colour : int)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Alright, so say we want to draw a number on the screen.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var font := Font.New (&amp;quot;Garamond:26:bold&amp;quot;)&lt;br /&gt;
var num : int&lt;br /&gt;
get num %error proof this!&lt;br /&gt;
Font.Draw (intstr (num), 100, 100, font, black)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Easy enough. We could also have simply done&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Font.Draw (intstr (10), 100, 100, font, black)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
But the first way gets you to error proof it for me. Mwahaha!&lt;br /&gt;
&lt;br /&gt;
==ord and chr==&lt;br /&gt;
Next up, we learn about two new functions.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ord (ch : char) : int&lt;br /&gt;
chr (i : int) : char&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
ord takes a single character and returns an integer (that is specific to that character).&lt;br /&gt;
chr takes an integer and returns a single character (that is specific to that integer).&lt;br /&gt;
To use these, we need to haul out our ASCII chart. So, open Turing, open the Turing Help Manual (that's code for &amp;quot;press F10&amp;quot;), expand Turing Language, select Keystroke Codes.&lt;br /&gt;
Find the letter &amp;quot;A&amp;quot;. It's ordinal value is 65. So&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put ord (&amp;quot;A&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
would output 65. Similarly,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put chr (65)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
would output the letter &amp;quot;A&amp;quot; (without the quotes).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var s : char := &amp;quot;A&amp;quot;&lt;br /&gt;
put chr (ord (s))&lt;br /&gt;
&lt;br /&gt;
var i : int := 65&lt;br /&gt;
put ord (chr (i))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For any character, s, chr (ord(s)) = s. Also, for any integer, i, ord (chr (i)) = i.&lt;br /&gt;
These functions are useful for a variety of things. For example, say you want to loop until the user presses Ctrl + Backspace:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var input : string (1)&lt;br /&gt;
loop&lt;br /&gt;
    getch (input)&lt;br /&gt;
    exit when ord (input) = 127 %crtl + backspace&lt;br /&gt;
end loop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or, say you want to output the alphabet to the screen, in lower case letters:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i : 97 .. 122&lt;br /&gt;
    put chr (i), &amp;quot; &amp;quot; ..&lt;br /&gt;
end for&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
How about converting a string from upper case to lower case?&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var upperCaseString := &amp;quot;ROOOOAAAR&amp;quot;&lt;br /&gt;
var lowerCaseString := &amp;quot;&amp;quot;&lt;br /&gt;
for i : 1 .. length (upperCaseString)&lt;br /&gt;
    lowerCaseString += chr (ord (upperCaseString (i)) + 32)&lt;br /&gt;
end for&lt;br /&gt;
put lowerCaseString&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don't already know what += means, it simply incriments the variable by whatever is after it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
num += 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
is the same as&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
num := num + 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
When using strings, it just adds the string (or character) to the end of my string.&lt;br /&gt;
Note that the ordinal value of any lower case letter is equal to the ordinal value of any upper case letter + 32.&lt;br /&gt;
Note that this doesn't quite work if your string contains things other than numbers. To fix this:&lt;br /&gt;
Turing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var upperCaseString := &amp;quot;RAWR!!  HERE ME ROAR!!&amp;quot;&lt;br /&gt;
var lowerCaseString := &amp;quot;&amp;quot;&lt;br /&gt;
for i : 1 .. length (upperCaseString)&lt;br /&gt;
    if ord (upperCaseString (i)) &amp;gt;= 65 and ord (upperCaseString (i)) &amp;lt;= 90 then %if it is a capital letter&lt;br /&gt;
        lowerCaseString += chr (ord (upperCaseString (i)) + 32)&lt;br /&gt;
    else&lt;br /&gt;
        lowerCaseString += upperCaseString (i)&lt;br /&gt;
    end if&lt;br /&gt;
end for&lt;br /&gt;
put lowerCaseString&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Yay! Next up, let's try outputting &amp;quot;Aa Bb Cc ... Yy Zz&amp;quot;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i : 65 .. 90&lt;br /&gt;
    put chr (i), chr (i + 32), &amp;quot; &amp;quot; ..&lt;br /&gt;
end for&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
So there you have it, string manipulation in a really big nutshell that took a long time to write. Aah, let's let Asian sum things up for me, my fingers are tired:&lt;br /&gt;
AsianSensation wrote:&lt;br /&gt;
&lt;br /&gt;
Know this though, string manipulation comes with practice, and problem solving is a big part of this. So don't assume knowing all about index will make sure you will be able to solve a question. Index is merely a tool, not the solution.&lt;br /&gt;
&lt;br /&gt;
Replace the word &amp;quot;index&amp;quot; with the words &amp;quot;the stuff covered in this tutorial, whatever that may be&amp;quot; and heed his advice (or wisdom).&lt;br /&gt;
&lt;br /&gt;
Happy string manipulating&lt;br /&gt;
&lt;br /&gt;
==Credits==&lt;br /&gt;
Authour: [[Cervantes]]&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=IRC_channel</id>
		<title>IRC channel</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=IRC_channel"/>
				<updated>2008-08-24T21:18:57Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: /* IRC Names to CompSci.ca User Names */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[The]] #compsci.ca channel on IRC is where all the cool members are hanging out. Chances are, if you're not there, you're missing something exciting and important.&lt;br /&gt;
&lt;br /&gt;
To join the CompSci.ca IRC community, you need an [[IRC Client]].  We recommend [[X-Chat]]. Join any of the servers and channels described below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Networks==&lt;br /&gt;
&lt;br /&gt;
===CompSci.ca community on AfterNET===&lt;br /&gt;
&lt;br /&gt;
'''Server:'''  irc.afternet.org or irc.compsci.ca&lt;br /&gt;
&lt;br /&gt;
'''Main Channel:''' #compsci.ca&lt;br /&gt;
&lt;br /&gt;
'''Self-taught Programming Channel:''' #exile-cafe&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
===Of The Main Channel===&lt;br /&gt;
&lt;br /&gt;
Much of #compsci.ca's early history is shrouded in mystery. Nobody is quite sure the exact time when it was created, but rumour has it that [[Coutsos]] was its founder, although he did not begin to regularly attend it until quite some time later. Its first regular inhabitants were [[wtd]] and a mysterious lurker named [[Ultrahex]], who though a member of compsci does not seem to ever post on the forums. Things took off sometime in early 2005, when a wave of members, including [[Hikaru79]], [[Coutsos]], [[Cervantes]], and [[Gandalf]] started regularly attending the channel. At present, #compsci.ca has a regular population of about ten, give or take, although it is subject to occasional flurries of activity, often when the moon is full.&lt;br /&gt;
&lt;br /&gt;
As of April 15th, 2006, #compsci.ca is for [[CompSci.ca]] and programming related discussion.  Excessive off topic discussion is to be held elsewhere.&lt;br /&gt;
&lt;br /&gt;
''Please note: The above rule of 'off topic discussion' rarely holds, and don't think you've gotta be a stickler. That doesn't mean you can be a jackass, though. And there often is a lot of programming talk, or otherwise geeky conversations, usually [[wtd]] dispensing out some parcel of [[wtd's infinite wisdom]].&lt;br /&gt;
&lt;br /&gt;
===Of The Self-taught Programming Channel===&lt;br /&gt;
This channel was thought of on the [http://www.compsci.ca/v2/viewtopic.php?t=10613 forums] by [[wtd]] on December 14, 2005. The main interest of this new channel is [[Ruby]] and [[O'Caml]]. This channel is not for the people who are taught programming in high school but for the people who self-teach it. When [[wtd]] announced it, there was a great cheer for [[wtd]] since he created the channel for the rest of the users to use and ask questions about.  Now that the #cs-self-taught channel has been merged into the #exile-cafe channel, all relevant discussion may proceed there.&lt;br /&gt;
&lt;br /&gt;
===Of The Compsci.ca Chat Channel===&lt;br /&gt;
Founded in April 15th, 2006 during the [[Great IRC Crackdown]] of 2006, it was designed to reduce much of the spam going on in the #compsci.ca main room.&lt;br /&gt;
&lt;br /&gt;
This new channel is a place for more relaxed chat, the old is for more [[CompSci.ca]] related discussion and computer science in general.  Programming, operating systems, etc., it's all welcome in #compsci.ca.  If you are going to spam, please join #compsci.ca-chat.  If you spam too much, you will be devoiced and/or kicked.&lt;br /&gt;
&lt;br /&gt;
As of July 2006 this channel's existance has dwindled, and it is no longer used.  General discussion may take place in #compsci.ca in a moderated fashion.  Spamming and the like are still not tolerated on the main channel.&lt;br /&gt;
&lt;br /&gt;
==Daily Life==&lt;br /&gt;
&lt;br /&gt;
Much of #compsci.ca's time is spent in utter silence (while its members spend time in deep personal reflection and thought), broken periodically by outbursts of conversation, instruction, and weirdness. Hilarity often ensues. Confusion can become rampant when [[the]] mature members take to stealing each other's nicknames (see [[Coutsos Identity Crisis]]).  On a good day, [[wtd]] can be observed teaching some interesting, mind-blowing new programming concept.&lt;br /&gt;
&lt;br /&gt;
Ocasionally if you are really lucky you'll witness something as bizzare as [http://www.compsci.ca/wiki/index.php?title=IRC_2006_01_17 this wonderful event], or the short but horrifing [[vibrator incident]].&lt;br /&gt;
&lt;br /&gt;
== IRC Names to CompSci.ca User Names ==&lt;br /&gt;
*'''[[wtd]]''' - wtd, Lotho ([http://www.afternet.org/afternet_status/?account=wtd&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[Cornflake]]''' - God, md, minor_deity, cornflake, or Thinkpod ([http://www.afternet.org/afternet_status/?account=minor_deity&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[Mazer]]''' - [niko|Stukkm] ([http://www.afternet.org/afternet_status/?account=ncout1&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[Cervantes]]''' - Cervantes, [[Minsc]], and 100's of other variations that come up ([http://www.afternet.org/afternet_status/?account=cervantes&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[Gandalf]]''' - Gandalf, [Gandalf] ([http://www.afternet.org/afternet_status/?account=gandalf11111&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[Hikaru79]]''' - Hikaru79 ([http://www.afternet.org/afternet_status/?account=hikaru79&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[Hacker Dan]]''' - Hacker_Dan, Dan ([http://www.afternet.org/afternet_status/?account=hacker_dan&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[timmytheturtle]]''' - timmy, timmytheturtle, timmythetortoise ([http://www.afternet.org/afternet_status/?account=timmytheturtle&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[Amailer]]''' - Amailer ([http://www.afternet.org/afternet_status/?account=amailer&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[TheFerret]]''' - TheFerret, Furo ([http://www.afternet.org/afternet_status/?account=TheFerret&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[Martin]]''' - mdkess ([http://www.afternet.org/afternet_status/?account=mdkess&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[rdrake]]''' - rdrake ([http://www.afternet.org/afternet_status/?account=rdrake&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[rizzix]]''' - rizzix ([http://www.afternet.org/afternet_status/?account=rizzix&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[apomb]]''' - '''[[Apomb]]''' ([http://www.afternet.org/afternet_status/?account=compwiz333&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[Bored]]''' - Bored, Confused ([http://www.afternet.org/afternet_status/?account=bored&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[Aziz]]''' - Aziz ([http://www.afternet.org/afternet_status/?account=Aziz&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[ZeroPaladn]]''' - ZeroPaladn, ZeroWeapon&lt;br /&gt;
*'''[[StealthArcher]]''' - StealthArcher&lt;br /&gt;
*'''[[Mackie]]''' - [[Mackie]]&lt;br /&gt;
*'''[[Saad]]''' - Saad ([http://www.afternet.org/afternet_status/?account=Saad&amp;amp;small=1 Status])*&lt;br /&gt;
&lt;br /&gt;
*Please update your account= with the proper account name if necessary.&lt;br /&gt;
&lt;br /&gt;
===Statistics===&lt;br /&gt;
Statistics based on a combination of logs from many different users is available at [http://theferret.ath.cx/Logs/compsci.ca.html http://theferret.ath.cx/Logs/compsci.ca.html] They are updated daily.&lt;br /&gt;
&lt;br /&gt;
==The Future==&lt;br /&gt;
&lt;br /&gt;
The future for #compsci.ca is looking bright, as it receives more and more attention in the compsci.ca community, particularly through the siggies and evangelism of [[Cervantes]], [[Coutsos]], and [[Hikaru79]]. With any luck, it will soon be as integral a part of the compsci.ca experience as the forum has come to be. &lt;br /&gt;
&lt;br /&gt;
[[Hacker Dan]] has integrated a Java-based IRC client that will send users to the channel directly from the forums. The feature is currently included in the alpha version of [[V3]].&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=IRC_channel</id>
		<title>IRC channel</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=IRC_channel"/>
				<updated>2008-08-24T21:17:48Z</updated>
		
		<summary type="html">&lt;p&gt;Saad: /* Statistics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[The]] #compsci.ca channel on IRC is where all the cool members are hanging out. Chances are, if you're not there, you're missing something exciting and important.&lt;br /&gt;
&lt;br /&gt;
To join the CompSci.ca IRC community, you need an [[IRC Client]].  We recommend [[X-Chat]]. Join any of the servers and channels described below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Networks==&lt;br /&gt;
&lt;br /&gt;
===CompSci.ca community on AfterNET===&lt;br /&gt;
&lt;br /&gt;
'''Server:'''  irc.afternet.org or irc.compsci.ca&lt;br /&gt;
&lt;br /&gt;
'''Main Channel:''' #compsci.ca&lt;br /&gt;
&lt;br /&gt;
'''Self-taught Programming Channel:''' #exile-cafe&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
===Of The Main Channel===&lt;br /&gt;
&lt;br /&gt;
Much of #compsci.ca's early history is shrouded in mystery. Nobody is quite sure the exact time when it was created, but rumour has it that [[Coutsos]] was its founder, although he did not begin to regularly attend it until quite some time later. Its first regular inhabitants were [[wtd]] and a mysterious lurker named [[Ultrahex]], who though a member of compsci does not seem to ever post on the forums. Things took off sometime in early 2005, when a wave of members, including [[Hikaru79]], [[Coutsos]], [[Cervantes]], and [[Gandalf]] started regularly attending the channel. At present, #compsci.ca has a regular population of about ten, give or take, although it is subject to occasional flurries of activity, often when the moon is full.&lt;br /&gt;
&lt;br /&gt;
As of April 15th, 2006, #compsci.ca is for [[CompSci.ca]] and programming related discussion.  Excessive off topic discussion is to be held elsewhere.&lt;br /&gt;
&lt;br /&gt;
''Please note: The above rule of 'off topic discussion' rarely holds, and don't think you've gotta be a stickler. That doesn't mean you can be a jackass, though. And there often is a lot of programming talk, or otherwise geeky conversations, usually [[wtd]] dispensing out some parcel of [[wtd's infinite wisdom]].&lt;br /&gt;
&lt;br /&gt;
===Of The Self-taught Programming Channel===&lt;br /&gt;
This channel was thought of on the [http://www.compsci.ca/v2/viewtopic.php?t=10613 forums] by [[wtd]] on December 14, 2005. The main interest of this new channel is [[Ruby]] and [[O'Caml]]. This channel is not for the people who are taught programming in high school but for the people who self-teach it. When [[wtd]] announced it, there was a great cheer for [[wtd]] since he created the channel for the rest of the users to use and ask questions about.  Now that the #cs-self-taught channel has been merged into the #exile-cafe channel, all relevant discussion may proceed there.&lt;br /&gt;
&lt;br /&gt;
===Of The Compsci.ca Chat Channel===&lt;br /&gt;
Founded in April 15th, 2006 during the [[Great IRC Crackdown]] of 2006, it was designed to reduce much of the spam going on in the #compsci.ca main room.&lt;br /&gt;
&lt;br /&gt;
This new channel is a place for more relaxed chat, the old is for more [[CompSci.ca]] related discussion and computer science in general.  Programming, operating systems, etc., it's all welcome in #compsci.ca.  If you are going to spam, please join #compsci.ca-chat.  If you spam too much, you will be devoiced and/or kicked.&lt;br /&gt;
&lt;br /&gt;
As of July 2006 this channel's existance has dwindled, and it is no longer used.  General discussion may take place in #compsci.ca in a moderated fashion.  Spamming and the like are still not tolerated on the main channel.&lt;br /&gt;
&lt;br /&gt;
==Daily Life==&lt;br /&gt;
&lt;br /&gt;
Much of #compsci.ca's time is spent in utter silence (while its members spend time in deep personal reflection and thought), broken periodically by outbursts of conversation, instruction, and weirdness. Hilarity often ensues. Confusion can become rampant when [[the]] mature members take to stealing each other's nicknames (see [[Coutsos Identity Crisis]]).  On a good day, [[wtd]] can be observed teaching some interesting, mind-blowing new programming concept.&lt;br /&gt;
&lt;br /&gt;
Ocasionally if you are really lucky you'll witness something as bizzare as [http://www.compsci.ca/wiki/index.php?title=IRC_2006_01_17 this wonderful event], or the short but horrifing [[vibrator incident]].&lt;br /&gt;
&lt;br /&gt;
== IRC Names to CompSci.ca User Names ==&lt;br /&gt;
*'''[[wtd]]''' - wtd, Lotho ([http://www.afternet.org/afternet_status/?account=wtd&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[Cornflake]]''' - God, md, minor_deity, cornflake, or Thinkpod ([http://www.afternet.org/afternet_status/?account=minor_deity&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[Mazer]]''' - [niko|Stukkm] ([http://www.afternet.org/afternet_status/?account=ncout1&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[Cervantes]]''' - Cervantes, [[Minsc]], and 100's of other variations that come up ([http://www.afternet.org/afternet_status/?account=cervantes&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[Gandalf]]''' - Gandalf, [Gandalf] ([http://www.afternet.org/afternet_status/?account=gandalf11111&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[Hikaru79]]''' - Hikaru79 ([http://www.afternet.org/afternet_status/?account=hikaru79&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[Hacker Dan]]''' - Hacker_Dan, Dan ([http://www.afternet.org/afternet_status/?account=hacker_dan&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[timmytheturtle]]''' - timmy, timmytheturtle, timmythetortoise ([http://www.afternet.org/afternet_status/?account=timmytheturtle&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[Amailer]]''' - Amailer ([http://www.afternet.org/afternet_status/?account=amailer&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[TheFerret]]''' - TheFerret, Furo ([http://www.afternet.org/afternet_status/?account=TheFerret&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[Martin]]''' - mdkess ([http://www.afternet.org/afternet_status/?account=mdkess&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[rdrake]]''' - rdrake ([http://www.afternet.org/afternet_status/?account=rdrake&amp;amp;small=1 Status])&lt;br /&gt;
*'''[[rizzix]]''' - rizzix ([http://www.afternet.org/afternet_status/?account=rizzix&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[apomb]]''' - '''[[Apomb]]''' ([http://www.afternet.org/afternet_status/?account=compwiz333&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[Bored]]''' - Bored, Confused ([http://www.afternet.org/afternet_status/?account=bored&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[Aziz]]''' - Aziz ([http://www.afternet.org/afternet_status/?account=Aziz&amp;amp;small=1 Status])*&lt;br /&gt;
*'''[[ZeroPaladn]]''' - ZeroPaladn, ZeroWeapon&lt;br /&gt;
*'''[[StealthArcher]]''' - StealthArcher&lt;br /&gt;
*'''[[Mackie]]''' - [[Mackie]]&lt;br /&gt;
&lt;br /&gt;
*Please update your account= with the proper account name if necessary.&lt;br /&gt;
&lt;br /&gt;
===Statistics===&lt;br /&gt;
Statistics based on a combination of logs from many different users is available at [http://theferret.ath.cx/Logs/compsci.ca.html http://theferret.ath.cx/Logs/compsci.ca.html] They are updated daily.&lt;br /&gt;
&lt;br /&gt;
==The Future==&lt;br /&gt;
&lt;br /&gt;
The future for #compsci.ca is looking bright, as it receives more and more attention in the compsci.ca community, particularly through the siggies and evangelism of [[Cervantes]], [[Coutsos]], and [[Hikaru79]]. With any luck, it will soon be as integral a part of the compsci.ca experience as the forum has come to be. &lt;br /&gt;
&lt;br /&gt;
[[Hacker Dan]] has integrated a Java-based IRC client that will send users to the channel directly from the forums. The feature is currently included in the alpha version of [[V3]].&lt;/div&gt;</summary>
		<author><name>Saad</name></author>	</entry>

	</feed>