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 / What is the difference between functions and gosub..whats better

Author
Message
OnQue Int
17
Years of Service
User Offline
Joined: 8th Jun 2007
Location:
Posted: 10th Dec 2008 19:33
i have 1500 lines of code and the only functions i have is for water and im amazed that i figured that out my shoot code is a gosub my controls for changing weapons is go sub and my collision is in the do loop... help me out here is any of this right.. i have a small lag problem and been trying to figure out what im doing wrong where should these blocks go or would they be alright where i have them i did another post asking about dims,globals contant,varibles to see if i can allocate memory or if imsetting up my varibles wrong. but nobody seems to want to answer or do not have the answer my varibles are regular like all my objects
example
ply=1
M_16=2
MP_5=3
tree1=4
tree1_2=5 ---clone from tree1
building=6
is there a different way to assighn here
trying to figure what is making me lose frame rate i have a frame rate of 11 and i have lowpolys would be the varibles or whether or not if im using gosubs instead of functions ploease help
Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 10th Dec 2008 21:33
There are reasons no one is answering your posts. I recommend cleaning up your grammar, punctuation, and spelling so people will have a much less difficult time working out what you're trying to say.

Another thing, when asking for help on your code, you actually need to post your code for people to look at. It's just about impossible for people to determine what's wrong with something if they can't even see it. Use the "code" button at the top of the Post Forum Message box to enclose your code in a little "Code Snippet" window.



I myself don't fully understand the problem you seem to be having, but based off the title of your post, I can try to explain some of the differences between writing functions and subroutines.

Here is an example I wrote up to explain each of their uses:



When you write a function, it's like writing a new command. You can pass the function parameters and it can give you a value, just like any built-in command, like rgb() for example. If you were so possessed, you could write a replacement for the rgb() command. Functions have their own variable scope too. This means that variables you define outside of your function will not be available inside it. Of course, you can get around this by defining a variable with a global statement.

Subroutines on the other hand, are useful for breaking down code into chunks. Different subroutines should have different purposes and the labels that define them should imply that purpose. Subroutines can also contain code that you need to reuse often. That way, whenever you need that code, you can just call the subroutine using gosub and avoid retyping everything. The thing to keep in mind about subroutines is that they are just essentially extensions of your main program. This means that they share the same scope of variables and everything else with the main program. Any subroutine will work in exactly the same way as if you had written the entire block of code in place of the gosub command.

I don't know if this is what you were looking for, but this is the difference between the two. I'm not sure how to relate this to whatever problem you have.
OnQue Int
17
Years of Service
User Offline
Joined: 8th Jun 2007
Location:
Posted: 10th Dec 2008 23:17
Well it answers a liitle why would i need a function if i could gosub and keep it with in the program i dont understand that. is it mandatory to have functions?
GIDustin
16
Years of Service
User Offline
Joined: 30th May 2008
Location:
Posted: 11th Dec 2008 00:38 Edited at: 11th Dec 2008 00:44
I find that I like functions a whole lot more than gosubs. It might just be my programming history or whatnot, but my current game (50% done) contains 0 gosubs, and 265 different functions. And trust me, each function has its use.
OnQue Int
17
Years of Service
User Offline
Joined: 8th Jun 2007
Location:
Posted: 11th Dec 2008 02:59
if you search for "Dim,varibles,lagging?" you will see where i posted my code please look at it and let me know what i could have done different and mabey you will see my problems. Alot is tidled because i am not using it at the moment but will be

please give me some examples in a first person shooter why i would use a function what are the benefits and how it would it look thank you
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 11th Dec 2008 03:10
Gosubs are for neatness, functions are for usefulness.

The main advantage to functions is their ability to be passed parameters and return values. For example if you wanted to write a function that would take two values and add them together, you'd do something like this:



Using that function you could add say, 5 and 7 together, and store the answer in a variable, like this:



Whereas with a subroutine, you couldn't pass it parameters, and you cant return values. To get around this you would have to store the 5 and 7 inside variables accessible to your entire program, as well as the sum#. Sometimes this is fine, but when you get into using the same variable name twice - which is very common - you need to use functions.

I dont know if you understand what variable scopes mean but basically, any variables declared in your main program are accessible to the main program. Any variables declared inside functions are ONLY accessible to those functions, allowing you to use the same variable names in the main program without fear of them overwriting each other.

For example, say you wanted to store the object positions of object number 1 inside your main program code, and in a function you wanted to display a circle at the mouse's x and y position. To do this you might use the variables x#, y#, and z# in your main program for the object's position, however you'd probably also want to use variables called x# and y# for your mouse coordinates as well. If you did this inside a gosub, the two x# and y# variables would overwrite each other, however inside a function they dont affect the main program's variables.

Example:



The above example uses a function to do what I explained. Now here is an example using a subroutine, notice how the x and y position aren't related to the object's position anymore, but instead the mouse's? This is because the variables aren't local to the subroutine, they're local to the entire program (except for functions), and since subroutines are essentially branches off of your main program, they're altering the x and y variables we're using for object positions.



So, thats the main benefit of functions.

OnQue Int
17
Years of Service
User Offline
Joined: 8th Jun 2007
Location:
Posted: 11th Dec 2008 16:55
I think i understand it alittle but need to understand why i would use it

lets say in my AI code i have many enemy types 5 that are ene_sniper 5 that are ene_closecombat 5 ene_guards

And if 5 ene_guards are runing around the level i would have to creat a function that would aloow these bots to move with out effecting each others X#,Y#,Z#.. or effect there shooting, and what ever else there doing since there using the same varibles for movement abd shooting ect...
right?
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 11th Dec 2008 20:17
You dont need to... its just an option. Functions just make things easier on you. For example using your AI question. Say you had 10 objects that represented 10 AI enemies, and you wanted to make them shoot at the user's object when they saw the user. You could write a function that would take the AI object as a parameter, and then inside the function the code would make the specified AI object shoot the player. Then you could just call the function instead of writing out the code for an AI enemy to shoot at the player multiple times for each AI entity. You couldn't really do this with a gosub because you'd need a way to specify which AI object you want to do the shooting, unless you made a new variable and set this variable to the AI object before calling the GOSUB, which causes you to write more code, and use another variable name in your main program that you dont need.

Its like saying "whats the difference between using the built-in command POSITION OBJECT, or writing your own subroutine that would take in the object number and xyz location you want to position the object at, then calculate the new position and move the object there." You'd obviously use the built in command, not because its already written for you, but because it does all of the subroutine stuff in 1 neat line of code, and allows you to easily change the parameters passed.

zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 11th Dec 2008 21:02
code reusablity. (If you code them properly, ie. (don't rely on globals in them))

If you code functions, you can pass them variables. Internally the function will use a it's own private copy of a variable.

In this way, you could easily move those functions to another program, and they will work, as long as the new program passes the correct parameters.

If you did this with gosubs, you would have to manually make sure that any variable names used in the routines aren't the same name as other variables throughout the program.


So use functions when you can. Try to allow them to use their own private variables internally. If you do this, you can later use any of the functions you coded in your next project.

~Zenassem
OnQue Int
17
Years of Service
User Offline
Joined: 8th Jun 2007
Location:
Posted: 11th Dec 2008 23:37
THANKS for the help. It is clear now. Is there a good physics program that darkbasic is compatible with that i can download or buy? Ive heard of darkphysics but thats all.


thanks again RUCCUS and ZENASSEM
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 12th Dec 2008 06:52
Quote: "
Gosubs are for neatness, functions are for usefulness.
"


Absurd.

RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 12th Dec 2008 17:08 Edited at: 12th Dec 2008 17:10
Whats so absurd about that? Gosubs are merely in place to prevent you from rewriting code over and over. They label sections of code so you can easily find and call that section, and that's it. If I want to declare all of my variables at startup, Ill do it in a subroutine so it's not bunched up at the top of my program. If I want to call a series of initalization functions, Ill put the calls inside a subroutine and call that instead. The only purpose they're serving is neatness. If however, I want to figure out the index number of an object Im clicking on stored in an array of all of my objects, Ill create a function to take the object clicked on and return the index. The function serves to make my code more efficient, the subroutine makes it neater.

About the physics question, search for Newton on the forums, its a wrapper written by Walaber for physics in DBP. Its fairly hard to get to grips with but once you do it works pretty good.

Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 12th Dec 2008 18:05
Amen RUCCUS...
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 12th Dec 2008 20:23
OnQue Int
17
Years of Service
User Offline
Joined: 8th Jun 2007
Location:
Posted: 12th Dec 2008 20:56
Could a mod please remove the jasons comments.. they are not needed in this thread... Thank you
OnQue Int
17
Years of Service
User Offline
Joined: 8th Jun 2007
Location:
Posted: 12th Dec 2008 21:12
Ruccus, I am having some sticking problems with my sparkys collision if you would check this thread
http://forum.thegamecreators.com/?m=forum_view&t=141346&b=7
I have come far on this engine but still have a few problems and questions and I'v always known you are the one to ask when it comes to FPS and by the way do you have any tutorials on how i could make a level editor for my engine and does it have to be inner twinded with the engine. i want a kinda point click and drag on to the terrain
thank you
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 12th Dec 2008 21:42
Well.. my comments might be unnecessary.. but generally they are responses to things I read in this thread... and its ok to disagree and discuss things in the forum... for example ... you need a level editor and I think you might want to scope out VanB's terrain editor... source included... might be a good starting point for you... http://forum.thegamecreators.com/?m=forum_view&t=122906&b=5

Also... try to be a little more tolerant.. you might find some of the people reading your thread, who can help you and have alot to offer you in know how, get turned off by unneccessary censorship pleas...

I've been using TGC products for five years... and I contribute. I apologize if I offended you in anyway or made you feel like I was worthlessly hijacking your thread.

Good Luck.

--Jason

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th Dec 2008 23:03
@OnQue Int,
No, I won't remove anything from this thread unless it breaks the AUP or is blatantly off-topic, none of which applies at this point. It's Ok to disagree, as long as it's kept polite.

Anyhow, here's my view on the question:
From a purely technical perspective, a subroutine is the same as a function, except it doesn't have parameters or local variable, and can't return a value. That's all. Nothing more, nothing less.

Their reason for being was to allow code reuse within the same application. Before they were invented (and we're talking a long time ago here), the code was simply duplicated. Subroutines allowed program memory to be used more efficiently. Subroutines gradually grew into procedures and functions over time as people hit restrictions with them.

Of course, people don't just leave it at that - they find other uses for subroutines/procedures/functions too. Such as simplification. Moving a complex piece of code into a function, even if you aren't going it to reuse it, usually makes the code you removed it from simpler to read, especially when the function is named appropriately.

Then there's the neatness. I have a dislike for too many lines of code bunched together at one point - if it's more than 2 screenfulls, I break it up into functions, making sure to keep those functions doing a logical unit of work.

Ruccus's usage of them is something he's worked out for himself over time. He has a use for them for specific things, and that's Ok for him. I don't limit myself in their use in that way though, and you don't need to either, although you can if you want.

I take it from Jason's replies that he agrees more with my view of this than he does with Ruccus

RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 13th Dec 2008 15:33
but our views are the same, you essentially just said you use subroutines to clean up code, break them up into neater, easy-to-read sections, and organize the program better. Thats what I was trying to say the entire time.

Anyways to each their own, as long as you develop a good, efficient style that you're ok with you'll be fine.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 13th Dec 2008 16:36
Maybe, but it seems to me as if you went a little further. Of course, it's just as possible that I've read too much into what you wrote too

jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 13th Dec 2008 20:21
Same here - and because I know Ruccus can code - I didn't feel like I was being to much a pain by dropping "Absurd" without explaination to provoke discussion

I remember when I was 10 (37 now) and I was coding with all Print, Input, If-then, goto.. and then someone showed me gosub... it was ground breaking! LOL

--Jason

Pillarofire
20
Years of Service
User Offline
Joined: 31st Dec 2003
Location: Good Question, <looks around.>
Posted: 16th Dec 2008 11:20
This argument is eons old.

But I use function mostly because I like the modularity and ease of code reuse.

gosubs just make me cringe because I have to re-read the entire gosub to make it portable. Were as when I write a function, I try to make it perform a specific task, so when I port it to another program the result is always predictable, and I don't get colliding variable names; cause I'm lazy and use variable names like cpx# for camera position x. etc.

RUCCUS-
Quote: "Anyways to each their own, as long as you develop a good, efficient style that you're ok with you'll be fine."


Exactly. And be consistent.
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 19th Mar 2009 17:50
Sorry for the bump, just spotted this thread while searching for something.

I remember the days of coding before copy and paste. I remember coding entire programs that had nothing but GOTOs. Like Jason said, the discovery of GOSUB was a phenominal moment.

I'm just very thankful that I don't have to write any more COBOL programs..

jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 19th Mar 2009 18:12
COBOL? LOL I Wrote an entire Warehouse Management System in DiBoL - Digital's cheap answer to the Pricey COBOL of the day. I will say it ran fast as heck though!

Quote: "gosubs just make me cringe because I have to re-read the entire gosub to make it portable"
True

Quote: "Were as when I write a function, I try to make it perform a specific task, so when I port it to another program the result is always predictable"
- Good Show!

KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 19th Mar 2009 18:38
It was fast, that's for sure. The place where I worked had centers all over the country connected to a central mainframe over frame relay lines. Their requirement was that once the request was submitted on the dumb terminal, the response had to be back in less than a second.

This was in the early to mid 90s, so the technology at the time wasn't to bad, even though it was a government operation with some 20+ year old equipment.

Heck, it's been long enough I don't even remember if COBOL has GOSUBs. I don't think it did, and they frowned heavily on GOTOs.

Login to post a reply

Server time is: 2024-09-28 04:25:13
Your offset time is: 2024-09-28 04:25:13