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 / Gosub or Functions. Difference??

Author
Message
Eckker
21
Years of Service
User Offline
Joined: 27th Jun 2003
Location:
Posted: 5th Nov 2003 22:12
Hi,

I have found an instance where a gosub and function perform the same result for me. Is one better to use than the other, ie faster or cleaner.

Also what exactly are the differences between the two. I know you can pass a variable to a function and get one back but does a gosub not do the same?

Appreciate your help. Let me know if this is gibberish and I'll elaborate.
Thanks
Eckker.
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 5th Nov 2003 22:19
in dbp gosubs and functions are NOT the same, although very similar. In dbp subs can't accept arguments like functions can. Also subs can use declared or global declared variables, but functions use locals and Global Declared only. Also functions are meant to return a value to the calling code like:

intMyInteger=MyIntegerFunction(arg1, arg2, argn)


where subs are more like a command:

gosub _DoMath

-RUST-
Guyon
21
Years of Service
User Offline
Joined: 6th Jun 2003
Location: United States
Posted: 6th Nov 2003 03:40
I like to think of Functions and having a number returned from a complex equation or small program.

function:
math stuff
retuen value
end function

Main program
x = function equation
End Program


Where as I think of a subroutine as a small program that keeps my code neater by putting it in a place out side my loop. It just keeps things looking nice.

Main Program
gosub load_game
gosub move_player
gosub shoot
gosub score
gosub test_end
End Program
Algae Man
22
Years of Service
User Offline
Joined: 11th Nov 2002
Location: Canada
Posted: 6th Nov 2003 17:26
Yes, but not just keeping things nice, but also if you want to execute the subroutine multiple times, it's easier than writing out the code multiple times.
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 6th Nov 2003 18:32
reusability in your code is something that every programmer should strive for, IMHO

-RUST-
Black Hydra
21
Years of Service
User Offline
Joined: 2nd Oct 2003
Location:
Posted: 6th Nov 2003 22:06
A function is for a simple, well defined task.

A subroutine is used like a bookmark in the code for time saving and organizational skills.

They are mostly the same except functions have more rigid guidelines.

If it is a simple task that doesn't need input from the game exceeding its parameters and doesn't output things exceeding its return then use a function. Otherwise subroutines would probably be easier.

Login to post a reply

Server time is: 2024-11-25 16:39:46
Your offset time is: 2024-11-25 16:39:46