if you want to do the same thing several times in the same program (for example fill the screen with the words "you lose"), then you can use a gosub to save you haveing to type all the instructions over and over, for example heres some code that fills the screen with the words "you lose"
for i=1 to 20
for l=1 to 20
print "YOU LOSE!!! ";
next l
print
next i
if you wanted to use this several times in your program then you COULD do
blah program
do
if out of power
for i=1 to 20
for l=1 to 20
print "YOU LOSE!!! ";
next l
print
next i
endif
if hit by missile
for i=1 to 20
for l=1 to 20
print "YOU LOSE!!! ";
next l
print
next i
endif
if hit rocks
for i=1 to 20
for l=1 to 20
print "YOU LOSE!!! ";
next l
print
next i
endif
loop
which is very long winded and a pain to keep putting in all the code over and over, so if you put that code right past the end of the program, add a label, and then stick return on the end, you get
game_over:
for i=1 to 20
for l=1 to 20
print "YOU LOSE!!! ";
next l
print
next i
return
as a gosub, then you just say "game_over" in the main code and every time you do the computer will skip to the label game_over and run the code after it, when it gets to the return instruction it goes back to what it was doing in the 1st place, eg
blah program
do
if out of power then gosub game_over
if out of missiles then gosub game_over
if hit rocks then gosub game_over
loop
game_over:
for i=1 to 20
for l=1 to 20
print "YOU LOSE!!! ";
next l
print
next i
return
as you can see the program now just one copy of the game over routine, and every time you want to do that thing then you just call it by name, knowing that the code will return to where it came from when it finishes, hope that makes things more clear, places you could use a gosub are for example when you are creating and colouring blocks to make scenery for agame, rather than keep writing
inc a
b=rnd(55)+4
make object cube a,b
red=rnd(254)+1
green=rnd(254)+1
blue=rnd(254)+1
color object a,rgb(red,green,blue)
you can just label the routine something descriptive and then use the name to replace typing the instructions.
make_pretty_box:
inc a
b=rnd(55)+4
make object cube a,b
red=rnd(254)+1
green=rnd(254)+1
blue=rnd(254)+1
color object a,rgb(red,green,blue)
return
subroutines are nowdays mostly replaced by functions, functions work pretty much like subroutines but they have all their variables local, so if you have a variable called SCORE in a subroutine it has a different value from SCORE in any other part of the program, you can also send functions variables to work with and they return just one variable, so you can make functions that work just like the functions in darkbasic, heres an example of a "times" function
do
input a
input b
print times(a,b)
loop
function times(x,y)
answer=x*y
endfunction answer
notice that the function can have totaly different variable names, the a&b in the main loop are placed into x&y, the result "answer" is then sent back when the function ends as the functions value by adding it to the endfunction, hope that explains it a bit.
PC1:XP, P4 3ghz, 1gig mem, 3x160gig hd`s, Radeon 9800pro
PC2: Linux, AMD 2ghz, 512mb ram, Nvidia GeForce4mx
PC3: XP/Linux dual boot, laptop, intel 2.6ghz celeron, ATI 9000igp, 256mb