Presentation is loading. Please wait.

Presentation is loading. Please wait.

B.A. (Mahayana Studies) 000-209 Introduction to Computer Science November 2005 - March 2006 13. Logo (Part 2) More complex procedures using parameters,

Similar presentations


Presentation on theme: "B.A. (Mahayana Studies) 000-209 Introduction to Computer Science November 2005 - March 2006 13. Logo (Part 2) More complex procedures using parameters,"— Presentation transcript:

1 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.

2 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

3 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

4 000-209 Intro to CS. 13/logo2 4 1. Shorter Commands Most commands have 2-letter short forms. Some of them:  FD = FORWARDBK = BACK  RT = RIGHTLT = LEFT  PU = PENUPPD = PENDOWN I'll start using the short forms from now, since it means less typing.

5 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

6 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 using the argument argument holding a parameter

7 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 value the name of the box use :size's value continued

8 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

9 000-209 Intro to CS. 13/logo2 9 Using sized_forward Two calls to the sized_forward procedure with different parameters.

10 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 100 100 use :size's value, 100

11 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, for the width and height of the rectangle rectangle :width code using box values :height

12 000-209 Intro to CS. 13/logo2 12 3. Rectangle

13 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 :height rectangle 100 50 rectangle :width code using box values :height rectangle 10 100

14 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

15 000-209 Intro to CS. 13/logo2 15 4. Fun with Squares A square procedure that has a size argument:

16 000-209 Intro to CS. 13/logo2 16 A Squares4 Procedure

17 000-209 Intro to CS. 13/logo2 17 Procedure Calls squares4 :size square :size square :size + 20 square :size + 40 square :size + 60 squares4 50 50 square :size repeat command 70 There are two :size parameters: one in squares4, the other in square. square is called 4 times

18 000-209 Intro to CS. 13/logo2 18 A Tables Procedure

19 000-209 Intro to CS. 13/logo2 19 A Mirror Procedure

20 000-209 Intro to CS. 13/logo2 20 A Mirrors Procedure

21 000-209 Intro to CS. 13/logo2 21 Procedure Calls mirrorsmirror calls tables squares4 :size square :size

22 000-209 Intro to CS. 13/logo2 22 5. Fun with Triangles

23 000-209 Intro to CS. 13/logo2 23 A Hexagon Procedure

24 000-209 Intro to CS. 13/logo2 24 Procedure Calls hexagon :sz repeat 6 [ triangle :sz rt 60 ] hexagon 90 90 triangle :size repeat command 90 triangle is called 6 times

25 000-209 Intro to CS. 13/logo2 25 A Spider Web Procedure (1)

26 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 :sz hexagon :sz+20 hexagon :sz+40 hexagon is called 3 times

27 000-209 Intro to CS. 13/logo2 27 A Spider Web Procedure (2)

28 000-209 Intro to CS. 13/logo2 28 spiderWeb 30 35 :sz hexagon :sz hexagon :sz + :step hexagon :sz + (:step*2) :step spiderWeb Procedure Call

29 000-209 Intro to CS. 13/logo2 29 Too Many Spider Webs a problem continued

30 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

31 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...

32 000-209 Intro to CS. 13/logo2 32 6. Printing print  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

33 000-209 Intro to CS. 13/logo2 33 For several words, put them in square brackets  e.g. print [My name is Andrew]

34 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 :foo90 namevalue a variable A local variable is just like a procedure argument, but can be created anywhere inside the procedure.

35 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 :foo90

36 000-209 Intro to CS. 13/logo2 36 Using a Local Variable A variable must be created before it can be used.

37 000-209 Intro to CS. 13/logo2 37 Procedure Call :x:y add2 :z add2 7 13 localmake "z :x + :y 713

38 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.

39 000-209 Intro to CS. 13/logo2 39 Procedure Calls :x :x + :y :y adder :sum :a:b adder3 :c :ab adder3 6 7 2 output :sum localmake "ab (adder :a :b) 6 72

40 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.

41 000-209 Intro to CS. 13/logo2 41 Conditional Statements if ( ) [ ] Example:

42 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" message else print a "smaller" message The part after the "else" is called the else part. It's carried out if the test fails.

43 000-209 Intro to CS. 13/logo2 43 More Conditionals ifelse ( ) [ ] [ ]  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

44 000-209 Intro to CS. 13/logo2 44

45 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.

46 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.

47 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

48 000-209 Intro to CS. 13/logo2 48 Use (print …) to print arguments

49 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.

50 000-209 Intro to CS. 13/logo2 50 Lots of Spider Webs

51 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

52 000-209 Intro to CS. 13/logo2 52 Amazing

53 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 many more maze calls...

54 000-209 Intro to CS. 13/logo2 54 Polygons

55 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 many more polygon calls...

56 000-209 Intro to CS. 13/logo2 56 Other Polygons Try these:  polygon 1 123 3 polygon 1 90 5 polygon 5 144 5 polygon 1 172 3 Try to create these with the polygon procedure.

57 000-209 Intro to CS. 13/logo2 57 11. Fractals Fractals are curves created using recursion. Complex looking 2D and 3D shapes can be generated 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.

58 000-209 Intro to CS. 13/logo2 58 The C Curve Now try cCurve 2 15  make sure to reset and zoom out

59 000-209 Intro to CS. 13/logo2 59 Procedure Calls c 5 10 c 5 8 r l c 5 9 r l c 5 8 r l c 5 7 r l r l r l r l many, many more calls, until level = 0 c = cCurve r = right 90 l = left 90

60 000-209 Intro to CS. 13/logo2 60 The Dragon Now try dragon 4 9  make sure to reset first

61 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 7ld 10 7 r rd 10 7ld 10 7 l rd 10 7 r many, many more calls, until level = 0 d = dragon ld = leftDragon rd = rightDragon r = right 90 l = left 90 d 10 10

62 000-209 Intro to CS. 13/logo2 62 12. More Logo Commands setpensize [ ]  sets the width and height of the “pen” used by the turtle  e.g. setpensize [10 10] MSW Logo only uses the width value continued

63 000-209 Intro to CS. 13/logo2 63 setscreencolor  changes the colour of the display  e.g. setscreencolor 14 setpos [ ]  moves the turtle to the (X,Y) coordinate in the display  e.g. setpos [300 300]

64 000-209 Intro to CS. 13/logo2 64 Example

65 000-209 Intro to CS. 13/logo2 65 Random random  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


Download ppt "B.A. (Mahayana Studies) 000-209 Introduction to Computer Science November 2005 - March 2006 13. Logo (Part 2) More complex procedures using parameters,"

Similar presentations


Ads by Google