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.

DarkBASIC Professional Discussion / Gosubs or functions

Author
Message
Mnemonix
22
Years of Service
User Offline
Joined: 2nd Dec 2002
Location: Skaro
Posted: 6th Jan 2003 19:40
I am about to begin a new project and i have been experimenting with the efficiency of gosubs and functions. There are no particular advantages of functions i can see over gosubs. Other than being able to repeat variables in a function i would prefer to be able to return more than 1 function without having to use globals. Any opinions?
If you forget one little thing i shall have you shaved sterilised and destroyed: Jeffrey Goines
Kangaroo2
22
Years of Service
User Offline
Joined: 26th Sep 2002
Location: United Kingdom
Posted: 6th Jan 2003 23:42
I occassionally find a use for functions, but mostly use gosubs because 1:I think they look tidier and 2:Apparantly they are slightly quicker

* If the apocalypse comes, email me *
Mnemonix
22
Years of Service
User Offline
Joined: 2nd Dec 2002
Location: Skaro
Posted: 7th Jan 2003 00:13
I can see gosubs being quicker than functions. I am going to use gosubs from now on. They are much better equipped than functions. The downside is that i can not store gosubs in a library while i can do this with functions

If you forget one little thing i shall have you shaved sterilised and destroyed: Jeffrey Goines
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 7th Jan 2003 00:50
Well, if you want to re-use routines or algorithms in later projects, for example, functions are better. They would even be more useful if you could pass and return UDTs and arrays to / from functions.

ATM functions and subroutines are nearly the same speed-wise. But that can change...

Ogres have layers.
xtom
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Ireland
Posted: 7th Jan 2003 01:23
I like to use functions for small tasks like returning a distance value for example. And use gosubs for larger sections of code like menus etc.
Dr OcCuLt
22
Years of Service
User Offline
Joined: 27th Nov 2002
Location: a Dark Deep Dark pit, it dark in here
Posted: 7th Jan 2003 04:54
i ues functions all the time because

1) thay are tidier
2) you can have private variadles
3) you pass variadles to functions
4) you can pass variadles back from the functions
5) there easyer to debug

all these thing make for good and easy codeing

--Dr 0--
Aith
22
Years of Service
User Offline
Joined: 21st Oct 2002
Location:
Posted: 7th Jan 2003 17:55
I've seen these discussions so many times. About whether subroutines or functions are better. In truth, they both have their place in a program. One is not necessarily "better" than the other. They both have their respective strengths and weaknesses.
So opting never to use one or the other again is clearly not a good idea, if you think about it. Rather, it would be wise to strike a balance between the two, utilizing one where it would be more useful than the other, and vice versa.
If I'm not mistaken, C++ doesn't even have subroutines. It relies entirely on functions, and yet it works just fine. But that doesn't mean that subs have no use, either.

Bottom line - Don't resort to using only functions or subs. Make use of them both; that's why both of them are there.

wargamer
22
Years of Service
User Offline
Joined: 7th Jan 2003
Location: United Kingdom
Posted: 9th Jan 2003 16:13
..and don't forget that you can have global data in functions - Any array declared in the main program is automatically global, so you can effectively pass out multiple values from a function.

If you want global scalar variable, just dimension an array sized 0 in the main prog.
Dr OcCuLt
22
Years of Service
User Offline
Joined: 27th Nov 2002
Location: a Dark Deep Dark pit, it dark in here
Posted: 10th Jan 2003 01:15
this wood look a bit untidier but it the best way ot do it in DB

what DB needs is variable pointers so that functions can pass back real numbers, variable can be pass by ref and u can neven pass a Datas type back

--Dr 0--
jasuk70
22
Years of Service
User Offline
Joined: 3rd Dec 2002
Location: Hemel Hempstead
Posted: 10th Jan 2003 12:54
With what I'm currently working on having things in functions and being able to fold them and the editor still work properly with be great in making my code easier to work with. I'm currently not doing anything that needs a great deal of speed so that I would need to use gosubs.

Jas
Shadow Robert
22
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 10th Jan 2003 19:03
This is an interesting topic ... because most of you have been using DarkBasic long enough to understand the differences and uses for GoSubs and Functions.

There is a speed difference in the calls, and there is a variable difference in the calling, and lastly there is a Private and Public setup between the two.
GoSub are more for Static Variable Code ... suchas Controls or Updating Particles, etc
Whereas the Functions should be used for Creation and things that require variable inputs.

I'm sure someone can explain them in better depth, but if you only see them as you use one over the other - then i don't exactly see what you have come to learn about programming since using DarkBasic.
no offense of course

Anata aru kowagaru no watashi!
Kale
22
Years of Service
User Offline
Joined: 8th Sep 2002
Location: United Kingdom
Posted: 10th Jan 2003 19:32
I agree Raven, you should not just use one or the other, you should learn where to use both correctly.

What the flame does not consume, consumes the flame.
------------------------------------------------------------------------
AMD XP2100+, Geforce4Ti 4400, 512Mb DDR, Abit KX7, WinXP Home
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 10th Jan 2003 19:35
heres a trippy thought

a gosub is like a U
A function is like a X and or a Y

Dr OcCuLt
22
Years of Service
User Offline
Joined: 27th Nov 2002
Location: a Dark Deep Dark pit, it dark in here
Posted: 11th Jan 2003 01:28
i do`t thick that there is a true subroutine DBpro i thick
all GOSUB do`s is call a void function() white no pass of variable

you my thick i`n mad but look at this code it compile and runs on my pc



all my years of programming says this do`t work but it do`s

can you all not have a go at me if you have`t try the code

--Dr 0--
MrTAToad
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 11th Jan 2003 01:42
Thats right up there with the 'allowing reserved words to be used as variables'...

They really need to improve the syntax checker (along with other bits)...

Good news everyone! I really am THAT good...
http://www.nickk.nildram.co.uk/ for great plug-ins - oh my, yes!
MrTAToad
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 11th Jan 2003 01:45
I forgot to mention the reason that its naughty because without passing and returning the correct number of parameters, it could mess up the stack and crash either the computer and/or program...

Good news everyone! I really am THAT good...
http://www.nickk.nildram.co.uk/ for great plug-ins - oh my, yes!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Jan 2003 01:57
Not good, but shouldn't cause any problems.

DBPro uses the 'C' calling system so the caller is responsible for setting up and clearing of parameters on the stack.
MrTAToad
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 11th Jan 2003 02:14
Yes, no doubt there is protection during the compilation process (otherwise the example would have crashed)...

Good news everyone! I really am THAT good...
http://www.nickk.nildram.co.uk/ for great plug-ins - oh my, yes!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Jan 2003 02:19
It does usually ...
MrTAToad
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 11th Jan 2003 02:25
Have you listed how to call and use the DBPro internal structures yet, by the way ?

Good news everyone! I really am THAT good...
http://www.nickk.nildram.co.uk/ for great plug-ins - oh my, yes!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Jan 2003 02:54
Here's what I've got so far:

Automatic generation of headers and libs for the DBPro DLLs - This works OK until the DLL names change, and they will.

Partially completed program to generate interface code to DBPro DLLs - does not rely on the DLL name, but does have other issues.

Direct access to arrays and their contents - yes, you heard right. The idea came during an email conversation with Guy Savoie about his ziptools DLL. I'm trying to think of a way to use this for a 'pass by reference' type ... not sure it will come to anything though.

Thats all I'm willing to talk about just yet, but there will be more. I expect great things of patch 4 especially.

I love digging into all this kind of stuff
Dr OcCuLt
22
Years of Service
User Offline
Joined: 27th Nov 2002
Location: a Dark Deep Dark pit, it dark in here
Posted: 11th Jan 2003 03:28
function and subroutine must be call in the same way to strat white are it wood not

function and subroutine do the same thing thay keep code in packet but ownle function pass variable backward and forward so if a subroutine calls a function all the pass variable are not filled there for thay get fill white junk from memory

by the way function can`t call subroutine
JamDotNET
22
Years of Service
User Offline
Joined: 15th Jan 2003
Location: United States
Posted: 15th Jan 2003 15:35
All I can say on the subject of Functions vs. Subs...

Read Aith's comments above

Right on Aith!
Jam.net

indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 16th Jan 2003 02:48
also if your not sure about this matter Ive made an extensive readme for DB1 functions at RGT.

TheCyborg
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Denmark
Posted: 16th Jan 2003 12:50
I prefer Functions because I found a way to make it return multiple variables (without using globals).
ex:
(I have posted this somewhere else in these forums, go look for it)

First you declare your function:
Fuction MyExampleFunction(a,b,c,d)


In the last line before endfunction you collect all the variables that should be exported into one and seperate them with different chars (ex. ;,:,/,-,€):

OutPut$=Str$(MyInteger)+";"+MyString$+"€"+Str$(MyInteger2)
EndFunction OutPut$


Now declare 2 new functions, one called GetBefore$(Search$,String$) and the other GetAfter$(Search$,String$).
I don't remember these so I cannot post them here right now (while in school).
But basically the first function (GetBefore), will return the contents of a string before "Search$" in "String$". And the other will return the letters after "Search$". (Hope you understood this) Ex:

Return$=MyExampleFunction(1,2,3,4)
PlayerNumber=Val(GetBefore(";",Return$))
PlayerName$=GetBefore("€",GetAfter(";",Return$))
PlayerScore=Val(GetAfter("€",Return$))

At this moment the variables would be.
Return$="42;TheCyborg€8529"
PlayerNumber=42
PlayerName$="TheCyborg"
PlayerScore=8529

Good luck
--> TheCyborg

TheCyborg Development.
http://TheCyborg.Amok.dk
The Ultimate Source To DarkBASIC Programming.
Dr OcCuLt
22
Years of Service
User Offline
Joined: 27th Nov 2002
Location: a Dark Deep Dark pit, it dark in here
Posted: 16th Jan 2003 14:57
this is a good ider but a bit slow all the passing of string eats up time it will go a bit faster if u ues GetBefore$(Search as int ,String$)

--Dr 0--
Dr OcCuLt
22
Years of Service
User Offline
Joined: 27th Nov 2002
Location: a Dark Deep Dark pit, it dark in here
Posted: 17th Jan 2003 03:10
i just posted a good ues of Functions in the bullet data in arrays topic so if think Functions are uesles look at that

--Dr 0--
Aith
22
Years of Service
User Offline
Joined: 21st Oct 2002
Location:
Posted: 18th Jan 2003 03:07
Wow. I didn't expect this thread to grow so much (even though I should've).

Cyborg - That's a good idea you had, though the process is a little bit convoluted. But I really see no need to rule out subroutines. If you need to return multiple values, why not use a gosub instead? Sure, functions may look tider (especially with function folding), but if you need multiple variables returned, should you even be using a function to begin with?

Don't get me wrong; if any of you want to code a certain way, be my guest. But it is important to learn how to write efficient code.

JamDotNET - Thanks for the acknowledgement. I'm glad to see I'm not alone in my beliefs.

Matto
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location:
Posted: 18th Jan 2003 04:49
Yes thats an interesting design to get a function to return more than 1 variable.. nice very nice, I would like to see the rest of it but I am too lazy to go and search for it.

As far as the gosub vs functions goes, I had this same question and posted it not long(couple of weeks) before seeing this post, since then I have started work on rather intricate project, and I really wanted to know the better use of the 2, I had the initial information set to use an array/s and then came across the user created Types which I thought I could use instead.. what I have found by dealing with this complicated project is that I am actually using Gosubs, Functions, Types and Arrays all in harmony and they are workinga treat so far(early stages).

My point here is that once you start defining your project or actually programming it you will gradually see what is best used where and you should strike the right balance for your program using each in it's best place, it is nice to see others oppinions on this though, mine is that it depends on what your Programming, are confortable with using or are prepared to learn about and what you find works best in different places(test the variations..) within your program.

1ghz Cel,512 sdram 133,Gf2 MX 400 64,SBL 5.1,Win98
Best Upcoming MMOG - http://WWW.Atriarch.com
Project: Card Game with No Name
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 18th Jan 2003 07:10
global arrays return results from functions without using the defualt return parameter.

so does typed arrays in the same manner.

CloseToPerfect
22
Years of Service
User Offline
Joined: 20th Dec 2002
Location: United States
Posted: 18th Jan 2003 07:43
you can use a dim and return all the values you want

dim returnval(10)

do
thisway()
if returnval(1) = 1 then this
if returnval(2) = 1 then this
ect...
loop

function this_way()
if this returnval(1) = 1 else returnval(1) = 0
if this returnval(2) = 1 else returnval(2) = 1
ect...
endfunction

I like return trues or false from function but you could return anything this way.

Shadow Robert
22
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 18th Jan 2003 22:08
i think what you're all forgetting here is the basic elements

a SubRoutine is a Public repeatition of code
a Function is a Private repeatition of code with possible IO Conditions

Now last time i checked C++ Classes and Functions both are capable of being Private or Public ... so really they do have SubRountines they're just not called the same thing.

thats all i'm gonna add here

Anata aru kowagaru no watashi!
Mnemonix
22
Years of Service
User Offline
Joined: 2nd Dec 2002
Location: Skaro
Posted: 19th Jan 2003 01:46
The important thing to realise is that without using globls functions have a smaller IO functionality than gosubs. Having said that functions can be built into a library and used many times by different programs. If you were using a function to setup a default camera perspective for example then you would not actually need the output value(except in rare cases) and that would be the most efficient way.

If you forget one little thing i shall have you shaved sterilised and destroyed: Jeffrey Goines
Shadow Robert
22
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 19th Jan 2003 04:09
yeah it is... well atleast until we can convince Lee that we'd all benifit from class functions
kinda amusing actually because i've recently been thinking of way to make a fake class function within Ciyanna - kinda like how DarkEdit had fake types.
I think i should probably release this program soon, cause else i'll forget that its in a working state again - tweak and need to bug hunt for day hehee

Anata aru kowagaru no watashi!
howie
22
Years of Service
User Offline
Joined: 17th Jan 2003
Location: United States
Posted: 19th Jan 2003 06:21
As a programmer, your biggest strength is to write reusable code. The "GoSub" as well as the "GoTo" lends itself to hard to maintain and hard to read code. This is more apparent on larger projects. It's not to say you shouldn't use them.

As far as which one is faster, on this level, it makes no difference because your game is using up all the cpu cycles rendering the scene.

Each function you can reuse in the next games is very valuable and to do that, you need to break out everything nicely into functions.
Dr OcCuLt
22
Years of Service
User Offline
Joined: 27th Nov 2002
Location: a Dark Deep Dark pit, it dark in here
Posted: 19th Jan 2003 17:56
sum one how now what his takeing about at last

the thing is that it all about style if yoer happy uesing GoSub then keep uesing gosub if u like uesing function then ues functions do it matter

--Dr 0--
Shadow Robert
22
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 20th Jan 2003 04:19
at any level the difference always is worth knowing and using what is better for the available task ... because whether the difference is ns or ms in the end they all add up and mean that when you do heavily need the processor for something else that power just isn't there.

it is more noticeable within DarkBasic Standard and Enhanced the speed - and probably learnt more from those, but generally is a better idea to do certain things as go subs just to make sure sometimes you main loop doesn't get cluttered and that the returns are set.

I mean if you have a function for doing the something like the controls, what happens within that function is stuck there ... so the loop ignore the controls.
But if you do it in a Subroutine then it is completely open, sometime this is exactly what the code needs

and the Goto's are oftenly to hack your way around something, exactly what you'll just learn as it appears

Anata aru kowagaru no watashi!
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 20th Jan 2003 10:30
"a Function is a Private repeatition of code with possible IO Conditions"


my arrays that work globally across functions and main defeat this notion.

Shadow Robert
22
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 20th Jan 2003 12:12
yeah but arrays arn't the answer, especially as they take up valuable processing power - hense my previous comments standing.

alot of people have always remarked on how i possibly get my code working faster than thiers ... knowing things like this is partly the reason why.
Also writing my own versions of builtin functions like the pointer checkers for knowing what objects are available also tend to help.

Speed might not seem like much, but as soon as you start adding slightly complex things which are not graphics related DarkBasic Standard and Professional both start to show thier major weakness's ... knowing howto speed up the program in the unobvious essential areas allows you to think less about the other areas.
Specifically Networking and Artificial Intelligence, something alot of people don't even attempt alot of the time - i mean if you notice the games which are finally released the Ai within them isn't anything you want to write home about, or is upto a level which offers a great challenge.

The DarkBasic team havn't added certain features just to look pretty, there is a reason behind alot of such features and useage

Anata aru kowagaru no watashi!
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 20th Jan 2003 12:18
Typd arrays do the same thing, I dont have a slowdown issue with DT yet and this is the method im using.

there are two issues now u missed without realising theres always an exception to the rule.

Shadow Robert
22
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 20th Jan 2003 19:45
yeah there are oftenly exceptions... but from what i've heard Typed Data is actually slower than standardly made data.

I mean you can make the a Type Array, and globally from within the function itself - but why goto the trouble of setting up a tonne of data types and arraying it (which has to be released later) and making a function for something that is simple code repition rather than something that actually has a variable set?

I mean if i make a Particle set, i'll make a Type for the Particles, i'll make a function for thier creation, but i'll also make a gosub for thier lifespan because (might just be me) i tend to usually code with generics in mind.

This usually means i use several functions and gosubs for different aspects of the program itself, the values that go into the gosub are different because you set them different in the variable array - because you have the particle array with the actual particle data and the variable array which is more like a buffer holding generic data from all the particles active at that second, just copy across the pointer data to let the gosub know its reading a different location and there is no need for the program to use a function for the code

i guess it is more upto the programming styles, but gosubs are a little more invaluable than most realise ... just as Case Selects are as well.

Anata aru kowagaru no watashi!
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 21st Jan 2003 12:12
Shadow Robert
22
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 21st Jan 2003 18:45
lol ... well it upto you if you use them or not
no need to be rude about it

Anata aru kowagaru no watashi!
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 21st Jan 2003 18:47
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 23rd Jan 2003 03:57
Lol

xxxpetratxxx
B. R. W

Login to post a reply

Server time is: 2025-05-19 13:04:45
Your offset time is: 2025-05-19 13:04:45