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 / Function or Go Sub?

Author
Message
Stedders
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 14th Oct 2003 01:45
Hey peeps,

I'm just going through my program and it's a bit messy, I'd like to create blocks of code to call within my main loop.

My question is which is the best way to do it, should I use the Go Sub command or create Functions?

What are the advantages and restrictions of both methods?

I think I could create the program with either, but I would like to start to getting into good programing habits from the start.

To begin with I'm making a simple bouncy ball game, either a pong or breakout clone.

Cheers
Stedders

Are Games getting worse or our standards getting higher?
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 14th Oct 2003 02:52 Edited at: 14th Oct 2003 02:58
you can use either but functions are intended to Return some value to the calling code, where a sub doesn't return anything. Also subs in DBP don't accept arguments, where functions do...

A sub is like:

MySubRoutine:
...code...
...code...usually acting on some global vars...
...code...
Return

(return meaning return execution to line after call)
(you call the sub simply by GOSUB MySubRoutine)

A funtion that IanM wrote to fix wrapvalue function in dbp...
Function NewWrapValue(Source as float)
local Result as float
local Limiter as float
Limiter = int( Source / 360.0 )
Result = Source - (Limiter * 360.0)
if Result < 0.0 then inc Result, 360.0
Endfunction Result


and the call to the function would be...
fltMyVariable=NewWrapValue(fltSomeVar)
(the function is returning the value held in the local var Result)

It really depends on what you're doing. As you experiment with coding you'll start to see where each one is appropriate.
Hope this helps

-RUST-
skovron
21
Years of Service
User Offline
Joined: 14th Sep 2003
Location:
Posted: 14th Oct 2003 03:24
I prefer using functions but its my habbit from other programming languages.
gosubs and gotos remaind me the years of coding basic on 8-bit computers such as commodore c64 where using goto was almost prohibited by programming teachers and mentors

Anyway now using gosubs helps make your program well structurized.

My advice is to mix them. I mean use gosubs to call some big modules of your program (from main loop for ex.) and use funcs for stuff such as evaluating, some calculations. (but belive me I dont stick to this)

I agree with CattleRustler that you need to experiment with coding and you'll find out your own solution
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 14th Oct 2003 05:26
functions all the way, forgoe gotos and gosubs for a smarter method of modulised functions. drop in functions when u need them for new games and applications. extend the commands with your own functions.


good song lyric also "this function is a function"

the_winch
21
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 14th Oct 2003 05:36
If you have a lot of code and want to clean it up after you have written it then gosubs are attractive, mostly because you don't need to declare all the variables needed in the gosub as global like you would if you where using functions.

If you haven't written the code then functions have a few advantages, global/local variables and being able to accept variables and return a variable.
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 14th Oct 2003 07:12
and just to add although I endorse using Functions and Subroutines how you see fit, I am heavily against ever using goto statements (Like Skovron mentions above) They are open-ended dangerous things to play with, which may lead to the dreaded "Spaghetti-Code" from the days of old.

goto=bad IMHO


-RUST-
Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 14th Oct 2003 08:22
Always use functions. Never use gosubs.

Philip

What do you mean, bears aren't supposed to wear hats and a tie?
skovron
21
Years of Service
User Offline
Joined: 14th Sep 2003
Location:
Posted: 14th Oct 2003 13:51 Edited at: 14th Oct 2003 13:54
Well I see there are many pro-functions people. I'm glad
You see good programing habbits its not only using gosubs or funcs. Its also naming convention, well program comment (not for others but for yourself), modularity etc...

I think that you really need to experiment with it and find out your own 'style'. One example: there was a time when I was using 'hungarian notation' (not sure in english). I was following each variable name with lowrcase letter indicating its type. It was working for me until I've been faced up with one problem. Every time when I wanted to change variable type I had to change its name...every place I've used it!!!
So for me it doesnt work anymore though many coders use it and I dont think its a bad idea.

So you see in this case I cant tell you to use hungarian notation or not and is it good habbit or not. its up to you

However there are some rules:
goto = bad
naming like aabbcc, dfghjk, a1s2d3f4, hahahihi = bad
if..then hundred times when you can use for next = bad
not using comments when its large project = bad too
...
Stedders
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 14th Oct 2003 14:07
Thanks for all your tips guys,

I'm going to use functions for my projects, skovron thanx for the tips much appreciated, for my style I'm just going to have to play around and work out what's good for me.

Does anyone have know any threads/tutorials that explains functions well I think I understand them but would like to go over them just in case.

Are Games getting worse or our standards getting higher?
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 14th Oct 2003 18:30
actually when i was doing classic I wrote this sorry its a bit messy but it will help if you digest it slowly

in each block of code in each paragraph the rem lines is what u unremark when u want to test that function.

avoid unremarking the ` lines so its obvious what to unremark with both types of remark statements




CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 14th Oct 2003 23:08
dbp functions are not similar to C++/VB functions as the return can be anything where in VB/C++ the return is the function name, so I suggest looking for tutorials on dbp functions as opposed to general programming - aside from those differences they are pretty much the same.

GL

-RUST-
Surreal Studio IanG
20
Years of Service
User Offline
Joined: 21st Sep 2003
Location: Cyberspace...I think
Posted: 17th Oct 2003 23:26
FUNCTIONS ALL THE WAY

Login to post a reply

Server time is: 2024-09-21 06:47:38
Your offset time is: 2024-09-21 06:47:38