 |
 |
|
|
|
|
|
| How do I use subroutines? |
| (2 ) |
100%
0%
|
Subroutines are parts of a program that only run when the green 'start subroutine' icon is used. Each subroutine is numbered, and each can be called upon to execute at any point during the program. It is important to note that as soon as the subroutine has ended, the program returns to the point in the program just after the subroutine was called upon. (In effect, the program returns to where it left off to carry out the subroutine.)
Subroutines are especially useful in making a program less cluttered. Instead of copying the same code over and over within a program, simply create a subroutine and call upon it as needed. When calling subroutines it is important to wire in the value of the subroutine being carried out.
Here is a very simple, one-subroutine example:

The following program depends on light values in choosing which subroutine to run, using a light sensor fork to this end:

It is important to note that each subroutine can be called upon as many times as necessary and at almost any point within a program. Below is a simple example to illustrate this capability:

The following is an example of how NOT to use subroutines. In this program, the same actions could be performed by simply repeating a single \"beep\" subroutine eight times, rather than using eight duplicate subroutines. Remember, the main goal of a subroutine is to simplify and clean up a program, not to add excess clutter and lots of branches.

Note: ROBOLAB does not currently allow a subroutine to be called upon from within a subroutine.
AG ses kw
|