6.2 |
Information about SUBs
SUBs (subroutines, or often called procedures), can be thought of as a "small" module or program. Imagine first creating two games, TIC TAC TOE, and PAC-MAN. You can define two subs for this:
SUB TicTacToe
:
END SUB
SUB PacMan
:
END SUB Now, if you wanted to play Pac-Man, you can CALL the SUB simply by doing this:
CALL PacMan This will execute the code found in SUB PacMan. This is just a simple example, and not very useful.
|
6.3 |
Passing variables with SUBs
When you create a subroutine, you usually want to pass some kind of information to it. Let's think about drawing a triangle on the screen in graphics mode. What kind of information do we need passed to our SUB Triangle?
SUB Triangle(X1,Y1,X2,Y2,X3,Y3) A triangle requires 3 (X,Y) points, so the above subroutine takes 6 arguments. It's up to you how you want to interpret the data. To be absolutely sure we only want INTEGER values, we should do this:
SUB Triangle(X1%,Y1%,X2%,Y2%,X3%,Y3%)
or
SUB Triangle(X1 AS INTEGER, ... You can have a mixture of data types as arguments, they don't all have to be INTEGERs or all STRINGs, etc. Now, just call the Triangle SUB and pass the relevent data:
CALL Triangle(10,10,20,15,5,50)
CALL Triangle(100,66,55,4,150,100) The Triangle SUB is reuseable, so you can see how useful SUBs are. I'll let you complete the SUB Triangle as an exercise.
|