1 / 65

Overview

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. Overview. 1. Shorter Commands 2. Parameters 3. Rectangle

chuong
Download Presentation

Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. B.A. (Mahayana Studies)000-209 Introduction to Computer ScienceNovember 2005 - March 200613. Logo (Part 2) More complex procedures using parameters, arguments, and recursion. Local variables and conditional statements.

  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 argument holding a parameter command using the argument 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 value the name of the box forward :size use :size's value continued 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_forward procedure 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 100 sized_forward 100 :size forward :size 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, for the width and height of the rectangle rectangle :width :height code using box values 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: rectangle rectangle 100 50 :width :height code using box values • For the second call to rectangle: rectangle rectangle 10 100 :width :height code using box values 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 square squares4 50 :size 70 squares4 50 :size repeat command square :size square :size + 20 square :size + 40 square :size + 60 square is called 4 times There are two :size parameters: one in squares4, the other in square. 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 tables calls :size squares4 :size square 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 triangle hexagon 90 :size 90 hexagon 90 :sz repeat command repeat 6 [ triangle :sz rt 60 ] triangle is called 6 times 000-209 Intro to CS. 13/logo2

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

  26. Procedure Calls spiderWeb hexagon triangle 70 :sz :sz :size 70 repeat command hexagon :sz hexagon :sz+20 hexagon :sz+40 repeat 6 [ triangle :sz rt 60 ] hexagon is called 3 times spiderWeb 50 000-209 Intro to CS. 13/logo2

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

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

  29. Too Many Spider Webs continued a problem 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 draws hexagon and calls spiderWebR 40 draws hexagon and calls spiderWebR 60 draws hexagon and calls spiderWebR 80 draws hexagon and calls spiderWebR 100 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 a variable name value • 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 proclocalmake "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 add2 7 13 add2 7 13 :x :y :z localmake "z :x + :y 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 adder3 6 7 2 adder3 7 2 :a 6 :b :c :ab localmake "ab (adder :a :b) :x + :y adder :x :y :sum output :sum 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

More Related