When circumstances allow, I'll be typing up bits of my lecture notes and posting them online. These may or may not bear any resemblance to the actual lectures.
to greet :name print sentence "Hi word :name "! endWrite your own procedure story that accepts a (single) name as input and outputs a simple tale about a person with that name.
? story "Heidi Heidi played with the cat.The other way to store a value in a variable is to use the command make to put the value directly into the variable. For example:
? make "value 7 ? print :valueNotice that the inputs to make are the name of the variable (as indicated by the quotation mark) and the value to be stored in that variable. On the other hand, the input to print is the value stored in the variable. Use a colon (:) before the variable name to tell Logo to use the value stored in that variable. Some programmers don't like the make command because it allows us to change the value of a variable or define a new variable mid-procedure. While it is true that programming without it is quite elegant, make can be a very useful command.
to increment :number make "number (:number + 1) output :number end
? print increment 5 6 ? print increment 1 2In algebra, we never say "x = x + 1". In computer programming, we replace a value by a larger value so often that some languages have special abbreviations to do exactly this. What procedure increment says to Logo is "look up the value stored in the variable named number, add one to it, and store the result in the variable named number." The old value in number is erased and replaced by a value one higher.
Use make and word to write a procedure plural that accepts a word as input, adds an "s" to the end of the word, and then prints the result.
Let's practice writing procedures that use input and variables! Do questions 6 and 7 on this old worksheet to get some hands-on experience with variables. As you work, keep in mind that :number is the value stored in the variable named number, "number is the name of the variable named number, and number may or may not be the name of a Logo procedure. (And that naming a variable number doesn't guarantee that the value stored in it will be a number!)
to triangle :sidelength repeat 3 [forward :sidelength right 120] endWe can use it to draw several different sizes of equilateral triangle, saving us the trouble of typing something like repeat 3 [forward 20 right 120] every time we want a differently sized triangle.
triangle 100 triangle 50 triangle 200Notice that the command repeat used in procedure triangle requires two inputs -- the first input gives the number of times to repeat the instruction, and the second input is a list of instructions to repeat. How can we write commands that accept more than one input?
You may have already guessed the answer: when you define a procedure, the variable names for the different inputs to the procedure come after the procedure name and are separated by spaces. In the example below, the first input is the number of sides of a polygon and the second input is the length of each side.
to polygon :numsides :sidelength repeat :numsides [forward :sidelength right 360/:numsides] endRead the procedure soap.opera on page 44 of Chapter 3. Can you predict what this procedure will do if you run it using the instruction:
soap.opera "Pat "Jesse "KipMake a prediction then check your work.
Try writing your own soap procedure, a procedure that draws a rectangle given a length and width, or some other procedure that requires more than one input.