Amstrad DDI-1 manual Procedures with parameters, To square Repeat 4 fd 60 rt 90 end

Models: DDI-1

1 103
Download 103 pages 20.5 Kb
Page 61
Image 61

4.5 DR. LOGO PROCEDURES

A procedure is a list of instructions that tells Dr. Logo how to do a task.

You will probably write your first procedures by adding to those already built into Dr. Logo, these are called 'primitives'.

fd, bk, rt and lt are all built-in primitives which you may use at any time as building blocks to write your own procedures.

Another very useful built-in primitive is cs which clears the screen and sends the turtle to its starting position.

4.5.1 WRITING A SIMPLE PROCEDURE

It is easy to visualise that if the movements fd 60 rt 90 were to be repeated 4 times each, a square with sides of 60 units would be drawn.

The same effect can be achieved by writing a simple formula:

repeat 4 [fd 60 rt 90]

Clear the screen and then try typing this in to check what happens. To make this formula into a new procedure called, 'square', type:

to square

repeat 4 [fd 60 rt 90] end

Dr. Logo will now understand 'square' and each time it encounters the word 'square' it will draw a square on the screen. We could have given this procedure any name, but we chose 'square' to remind us what it does.

Dr. Logo allows us to type in a whole set of commands together so the instructions square rt 45 square, will draw two squares, the second at a 45 degrees angle to the first.

4.6 Procedures with parameters.

It is possible. to make a procedure to which we can say 'how much' in the same way that we can say 'how much' to a built-in procedure.

To make a procedure that will draw squares of different sized sides, the definition of 'square' can be altered to:

to squareanysize :side repeat 4 [fd :side rt 90] end

This new procedure introduces the idea of a 'variable', which in this case is called : side

You will notice that the variable :side, is preceeded by a colon, this indicates to Dr. Logo that :side is a variable rather than a command.

Chapter 4.3

AMSTRAD Disc Drive & Interface DDI-1 Manual

Page 61
Image 61
Amstrad DDI-1 manual Procedures with parameters, To square Repeat 4 fd 60 rt 90 end