Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Whats gosub?

Author
Message
Milky the purple crab
19
Years of Service
User Offline
Joined: 5th Aug 2005
Location:
Posted: 18th Aug 2005 08:33
hello, i heard that gosub is way better than goto but i don't know what it is can someone please explain what it does?

thanks in advanced

I don't fear pain.....I fear feeling it.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 18th Aug 2005 10:08
Gosub is like goto except it goes back to the line it was on (when there's a return).

Milky the purple crab
19
Years of Service
User Offline
Joined: 5th Aug 2005
Location:
Posted: 18th Aug 2005 10:41
i don't get it
i understand what it does with that code.

I don't fear pain.....I fear feeling it.
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 18th Aug 2005 11:55 Edited at: 18th Aug 2005 11:57
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
Milky the purple crab
19
Years of Service
User Offline
Joined: 5th Aug 2005
Location:
Posted: 18th Aug 2005 12:09
wow great thanks very detailed explaination

i understand now

I don't fear pain.....I fear feeling it.

Login to post a reply

Server time is: 2024-09-24 01:21:09
Your offset time is: 2024-09-24 01:21:09