Thursday, June 3, 2010

Using Sub-procedures

Sub-procedures are separate procedures in your program that are called upon in the main procedure. Here is what one looks like:

to drive
ab, thisway
ab, on
wait 20
turnright ;sub-procedure is being called upon here
wait 30

to turnright ;here is the sub-procedure
b, off
wait 10
b, on

So "turnright" is a sub-procedure of "drive." "Drive" is the main procedure. In other programming languages a sub-procedure is called a function but it basically works the same.

The benefit of using a sub-procedure is that it makes your program much more efficient and can save you a lot of work.

How will you use this in your program? 
In your procedures window, add a procedure called "turnright" and type in the commands that will make your car turn right. Do the same to add another procedure called "turnleft." In your main procedure every time there is a right turn, remove those commands and just type "turnright." Do the same for left turns. You will have to test your program to make sure the turnright and turnleft sub-procedures turn the correct amount, but all of the turns are right angles, so if it works once it should work for the rest.

No comments: