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 / Coding Principles: Gosubs and Functions

Author
Message
Geo Kinkladze
18
Years of Service
User Offline
Joined: 8th Mar 2006
Location:
Posted: 18th Mar 2006 09:20 Edited at: 18th Mar 2006 14:21
I read somewhere in the tutorials that it is bad programming technique to call a subroutine from more than one location, yet this is not the case for functions. Why?

Why should the following not be done from more than one location:



but the following is positively encouraged:



Also I find I can't have "chooseaction" as a name for both a subroutine and a function in the same program, they have to be different names. Is this a feature, to stop you confusing yourself?

Skcollob
Me!
19
Years of Service
User Offline
Joined: 26th Jul 2005
Location:
Posted: 18th Mar 2006 10:01 Edited at: 18th Mar 2006 17:47
a gosub is a function with no parameters and a global scope (the variables are visible all over the program), there is no reason why you can`t call a gosub from more than one place , I don`t know who said that, but that was how they where originaly used, the whole point of a SUBROUTINE was to package commonly used bits of code and just reuse them with the one call from anywhere in the program, the downside is that you have to have unique variable names for your subroutine as they share the same space as the main program.

thats why FUNCTIONS where created, with a function, once you have made it you can use the same code in different programs just like a compiler/language function, with the advantage that you don`t have to worry about the variable names sharing values with the rest of your code, so whatever value X has in your function is different to whatever X is in the main code, so you can do this...

for X=1 to 380 step 10
print "main loop X is ";X
otherX()
next X
wait key

function otherX()
X=rnd(200000)
print "functions X is :";X
endfunction

as you can see if you run this, the function retains a different idea of what X is to what the main loop does, this allows you to make functions and never worry about accidentaly using the same variable in your main code, since one can`t corrupt the other, you can do exactly the same code with just GOSUB

for X=1 to 380 step 10
print "main loop X is ";X
gosub otherX
next X
wait key

otherX:
Y=rnd(200000)
print "functions X (actualy Y) is :";Y
return

but in this case you need to keep the names seperate or you will corrupt one or other of the variables in your code, this can cause hard to find errors, whoever told you can/should only call a subroutine from one location in a program is wrong/mistaken, until functions appeared inside BASIC (it didn`t always have them) the only way to have function like features was to use gosubs, thats normal and accepted practice, call em from wherever you like, basicaly, use functions wherever you can and gosubs where you want to do something like load variables or create and fill arrays, read data statements etc (since doing this in a function would make the variables created only available to that function), then try to organise your code so that all your main loop does is make decisions and call the right functions this makes your code easier to follow eg.

if man_in_lava() then player_on_fire(lava)
if player_bullet_hit_head(opponent) then announce_headshot(announcer)
etc

much easier to read, you can even write the whole main loop and then fill out the functions one by one until the whole code is working perfectly (my prefered routine...code some...test it...code some...test it...slow, but it makes for fairly sound code)



I don`t care what you say, theres no way the commander of a Kamakasi Squadron got promoted up through the ranks.
Pheonixx
21
Years of Service
User Offline
Joined: 6th Oct 2003
Location: Columbus, Ohio
Posted: 19th Mar 2006 04:06
When you are working with other programmers as well as in an OOP environment, it is very helpful for all programmers to conduct there code in self contained modules. You'll probably never have to worry about Class's and Member Functions in DBP, but Gosub's traditionally can be replaced with functions, and if you find yourself passing a variable to more then one function, perhaps it could just be a global variable, which are accessible by all functions.

dim blah(10) AS INTEGER

DO
DoItMan()
PRINT blah(0)
LOOP

FUNCTION DoItMan()
blah(0) = rnd(100)
ENDFUNCTION

DIM's are global, so you can manipulate it from any function... that's why it's global.

Now I don't know why DIM's are global in DBP, arrays in general with c/c++ are local to where they are declared. But anyhoo...

This all goes back to basics of flowcharts, when you flowchart out your program you can easily identify all the little peices that make up the proposed program, and code each logical block accordingly.

http://ausukusa.breakset.com

Login to post a reply

Server time is: 2024-11-27 23:32:54
Your offset time is: 2024-11-27 23:32:54