B.A. (Mahayana Studies)000-209 Introduction to Computer Science
November 2005 - March 2006
13. Logo (Part 2)
More complex procedures using parameters, arguments, and recursion. Local variables and conditional statements.
000-209 Intro to CS. 13/logo2 2
Overview
1. Shorter Commands 2. Parameters 3. Rectangle 4. Fun with Squares 5. Fun with Triangles 6. Printing
continued
000-209 Intro to CS. 13/logo2 3
7. Local Variables 8. Procedures that Output 9. Conditional Statements 10. Stopping Recursive Calls 11. Fractals 12. More Logo Commands
000-209 Intro to CS. 13/logo2 4
1. Shorter Commands
Most commands have 2-letter short forms. Some of them:
FD = FORWARD BK = BACK RT = RIGHT LT = LEFT PU = PENUP PD = PENDOWN
I'll start using the short forms from now, since it means less typing.
000-209 Intro to CS. 13/logo2 5
2. Parameters
The numbers following command names are called parameters.
Examples: fd 50 setpencolor 15 setpos [ 50 25]
parameters
000-209 Intro to CS. 13/logo2 6
Parameters and Procedures
Parameters can be passed into a procedure and used by its commands.
The parameters are stored in arguments.
Example: to sized_forward :size
forward :size end
command usingthe argument
argument holding a parameter
000-209 Intro to CS. 13/logo2 7
An Argument is a Memory Box
An argument is a memory box inside the procedure.
Any commands inside the procedure can use the memory box value. sized_forward
:size
forward :size
valuethe name of the box
use :size's valuecontinued
000-209 Intro to CS. 13/logo2 8
A memory box is a location in RAM. It has a name and may contain a value A command accesses the box's value by using the
box's name.
The memory box only exists until the end of the procedure when the procedure finishes, the memory box is deleted
000-209 Intro to CS. 13/logo2 9
Using sized_forward
Two calls to the sized_forwardprocedure with different parameters.
000-209 Intro to CS. 13/logo2 10
Parameters and Memory Boxes
A procedure's parameter is copied into the memory box for the argument the parameter becomes the box's value
sized_forward
:size
forward :size
sized_forward 100100
use :size's value, 100
000-209 Intro to CS. 13/logo2 11
Many Arguments
Procedures can have many arguments. Draw any size of rectangle:
to rectangle :width :height fd :height right 90 fd :width right 90 fd :height right 90 fd :width end
two arguments, forthe width and height
of the rectangle
rectangle
:width
code using box values
:height
000-209 Intro to CS. 13/logo2 12
3. Rectangle
000-209 Intro to CS. 13/logo2 13
Rectangles Parameters
For the first call to rectangle:
For the second call to rectangle:
rectangle
:width
code using box values:heightrectangle 100 50
rectangle
:width
code using box values:heightrectangle 10 100
000-209 Intro to CS. 13/logo2 14
rectangle is called twice
At the end of the first call to rectangle, its width and height memory boxes (arguments) are deleted.
They are newly created again when rectangle is called the second time.
continued
000-209 Intro to CS. 13/logo2 15
4. Fun with Squares A square procedure that has a size argument:
000-209 Intro to CS. 13/logo2 16
A Squares4 Procedure
000-209 Intro to CS. 13/logo2 17
Procedure Calls
squares4
:size
square :sizesquare :size + 20square :size + 40square :size + 60
squares4 50
50square
:size
repeat command70
There are two :size parameters: one in squares4, the other in square.
square is called4 times
000-209 Intro to CS. 13/logo2 18
A Tables Procedure
000-209 Intro to CS. 13/logo2 19
A Mirror Procedure
000-209 Intro to CS. 13/logo2 20
A Mirrors Procedure
000-209 Intro to CS. 13/logo2 21
Procedure Calls
mirrors mirror calls
tables
squares4 :size
square :size
000-209 Intro to CS. 13/logo2 22
5. Fun with Triangles
000-209 Intro to CS. 13/logo2 23
A Hexagon Procedure
000-209 Intro to CS. 13/logo2 24
Procedure Calls
hexagon
:sz
repeat 6 [ triangle :sz rt 60 ]
hexagon 90
90triangle
:size
repeat command90
triangle is called6 times
000-209 Intro to CS. 13/logo2 25
A Spider Web Procedure (1)
000-209 Intro to CS. 13/logo2 26
Procedure Calls
hexagon
:sz
repeat 6 [ triangle :sz rt 60 ]
spiderWeb 50
70
triangle
:size
repeat command
70
spiderWeb
:sz
hexagon :szhexagon :sz+20hexagon :sz+40
hexagon is called3 times
000-209 Intro to CS. 13/logo2 27
A Spider Web Procedure (2)
000-209 Intro to CS. 13/logo2 28
spiderWeb 30 35:sz
hexagon :szhexagon :sz + :stephexagon :sz + (:step*2)
:step
spiderWeb
Procedure Call
000-209 Intro to CS. 13/logo2 29
Too Many Spider Webs
a problem continued
000-209 Intro to CS. 13/logo2 30
I had to press the "Halt" button to stop the execution spiderWebR will never stop on it's own
The spiderWebR procedure is recursive it calls itself
Recursion is a great technique, but I've used it incorrectly I didn't include code in spiderWebR to tell it how to stop
000-209 Intro to CS. 13/logo2 31
Recursion in Picture Form
spiderWebR 20
spiderWebR 40
draws hexagon and calls
spiderWebR 60
draws hexagon and calls
spiderWebR 80
draws hexagon and calls
spiderWebR 100
draws hexagon and calls
forever...
000-209 Intro to CS. 13/logo2 32
6. Printing
print <number or word> print the number or text in the command window. e.g. print 60
print 60 in the command window
A word must start with a quotation mark e.g. print "Andrew print Andrew in the command window
continued
000-209 Intro to CS. 13/logo2 33
For several words, put them in square brackets e.g. print [My name is Andrew]
000-209 Intro to CS. 13/logo2 34
7. Local Variables
A local variable is a memory box inside a procedure for storing numbers (or text).
Each variable has a name
:foo 90
name value
a variable
A local variable is just like a procedure argument, but can be created anywhere inside the procedure.
000-209 Intro to CS. 13/logo2 35
Creating a Local Variable
You create a local variable by giving it a name and a value: to proc
localmake "foo 90 :end
This creates a local variable called :foo, with the value 90, inside the procedure proc
proc
:foo 90
000-209 Intro to CS. 13/logo2 36
Using a Local Variable
A variable must be created before it can be used.
000-209 Intro to CS. 13/logo2 37
Procedure Call
:x :y
add2
:z
add2 7 13
localmake "z :x + :y
7 13
000-209 Intro to CS. 13/logo2 38
8. Procedures that Output
A procedure can output (return) an answer to the procedure that called it by using the output command.
000-209 Intro to CS. 13/logo2 39
Procedure Calls
:x
:x + :y
:y
adder
:sum
:a :badder3 :c :ab
adder3 6 7 2
output :sum
localmake "ab (adder :a :b)
6 7 2
000-209 Intro to CS. 13/logo2 40
9. Conditional Statements
A conditional statement is a command (or commands) which is only carried out if a test succeeds.
Examples: if (it is raining) then put up your umbrella if (7 > 5) then print a "bigger" message
The test part (e.g. 7 > 5) is called a condition. The part after the "then" is called the then part.
000-209 Intro to CS. 13/logo2 41
Conditional Statements
if ( <test>) [ <command(s)> ] Example:
000-209 Intro to CS. 13/logo2 42
The Else Part
You often want to do something if the test fails. Examples:
if (it is raining) then put up your umbrella else put on your sunglasses
if (:foo > :bar) then print a "bigger" messageelse print a "smaller" message
The part after the "else" is called the else part. It's carried out if the test fails.
000-209 Intro to CS. 13/logo2 43
More Conditionals
ifelse ( <test> ) [ <command(s)> ] [ <command(s)> ] the second […] is the else part
Example ifelse ( :foo > :bar ) [ print [foo is bigger] ]
[ print [foo is not bigger] ]
The conditional must be on one line.
continued
000-209 Intro to CS. 13/logo2 44
000-209 Intro to CS. 13/logo2 45
Better Formatting with ~
A long Logo line can be split across multiple lines by using ~'s this is useful for conditional statements
The previous example with ~'s: ifelse ( :foo > :bar ) ~
[ print [foo is bigger] ] ~ [ print [foo is not bigger] ]
Indent the two parts of the ifelse.
000-209 Intro to CS. 13/logo2 46
'If' Questions
1. Write a procedure that prints a message if two numbers are equal.
2.Write a procedure that prints "true" if the first number is less than the second, and "false" otherwise.
3.Write a procedure that prints one message if the first number is greater than the second, and a different message if it is not.
000-209 Intro to CS. 13/logo2 47
Answers
to equal :num1 :num2 if ( :num1 = :num2 ) ~ [ print [the numbers were equal] ] end
to lessThan :a :b ifelse ( :a < :b ) ~ [ print "true ] ~ [ print "false ] end
continued
000-209 Intro to CS. 13/logo2 48
Use (print …) to print arguments
000-209 Intro to CS. 13/logo2 49
10. Stopping Recursive Calls
The problem with my first version of spiderWebR was that it couldn't stop.
This can be fixed by putting the recusive call into a conditional statement.
000-209 Intro to CS. 13/logo2 50
Lots of Spider Webs
000-209 Intro to CS. 13/logo2 51
Recursion in Picture Form
spiderWebR 130
spiderWebR 150
draws hexagon and calls
spiderWebR 170
draws hexagon and calls
spiderWebR 190
draws hexagon and calls
spiderWebR 210
draws hexagon and calls
STOPS since 210 > 200
000-209 Intro to CS. 13/logo2 52
Amazing
000-209 Intro to CS. 13/logo2 53
Recursion in Picture Form
maze 5
maze 10
draws line, rotates and calls
maze 15
maze 200
STOPS since 200 = 200
draws line, rotates and calls
draws line, rotates and calls
many more maze calls...
000-209 Intro to CS. 13/logo2 54
Polygons
000-209 Intro to CS. 13/logo2 55
Recursion in Picture Form
polygon 9 80 1
polygon 10 80 1
draws line, rotates and calls
polygon 11 80 1
polygon 201 80 1
STOPS since 201 > 200
draws line, rotates and calls
draws line, rotates and calls
many more polygon calls...
000-209 Intro to CS. 13/logo2 56
Other Polygons
Try these: polygon 1 123 3
polygon 1 90 5polygon 5 144 5polygon 1 172 3
Try to create these with the polygon procedure.
000-209 Intro to CS. 13/logo2 57
11. Fractals
Fractals are curves created using recursion. Complex looking 2D and 3D shapes can be gener
ated using very small (and fast) code.
Fractals have been used in many computer-generated movies (e.g. Shrek, Toy Story) to create realistic looking backgrounds trees, clouds, water, fire, etc.
000-209 Intro to CS. 13/logo2 58
The C Curve
Now try cCurve 2 15 make sure to reset and zoom out
000-209 Intro to CS. 13/logo2 59
Procedure Calls
c 5 10
c 5 8 r c 5 8 l
c 5 9 r c 5 9 l
c 5 8 r c 5 8 l
c 5 7 r c 5 7 l c 5 7 r c 5 7 l c 5 7 r c 5 7 l c 5 7 r c 5 7 l
many, many more calls, until level = 0
c = cCurver = right 90l = left 90
000-209 Intro to CS. 13/logo2 60
The Dragon
Now try dragon 4 9 make sure to reset first
000-209 Intro to CS. 13/logo2 61
Procedure Calls
ld 10 10
ld 10 8 l rd 10 8
ld 10 9 l rd 10 9
ld 10 8 r rd 10 8
ld 10 7 l rd 10 7 ld 10 7 r rd 10 7 ld 10 7 l rd 10 7 rd 10 7 r rd 10 7
many, many more calls, until level = 0
d = dragonld = leftDragonrd = rightDragonr = right 90l = left 90
d 10 10
000-209 Intro to CS. 13/logo2 62
12. More Logo Commands
setpensize [ <width> <height> ] sets the width and height of the “pen” used by the turtl
e e.g. setpensize [10 10]
MSW Logo only uses the width value
continued
000-209 Intro to CS. 13/logo2 63
setscreencolor <colour> changes the colour of the display e.g. setscreencolor 14
setpos [<X> <Y> ] moves the turtle to the (X,Y) coordinate in the display e.g. setpos [300 300]
000-209 Intro to CS. 13/logo2 64
Example
000-209 Intro to CS. 13/logo2 65
Random
random <number> makes a random number between 0 and number e.g. random 50
makes a random number between 0 and 50
You can use random in many ways: setpencolor random 20 setscreencolor random 10 fd random 300