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 / Program logic using Subs Vs. Functions

Author
Message
Oboro
14
Years of Service
User Offline
Joined: 27th Jul 2010
Location: Gilmer, TX
Posted: 2nd Sep 2010 20:55
Hey again all I started a project that got REALLY ambitious very fast for David's Dice Game Challenge Link:http://forum.thegamecreators.com/?m=forum_view&t=172821&b=1
Needless to say I didn't make the deadline but I've decided to finish this if it kills me but as per usual I'm having trouble with logic, since DBPro executes line by line in order unless told to reference somewhere else I'm REALLY trying my best not to make my code look like a choose your own adventure book lol.
For some reason my brain cannot wrap around why a function is better to sue than a GoSub, I've read up but I guess I can't understand where I would use a function in my code Vs. a GoSub. I guess I just like GoSubs... My question is really probably beaten to death around here but I haven't found an answer that truly resonated with me, Is it better to use functions in your code than it is to use GoSubs and how is this really possible since functions don't effect Global variables outside of them selves? Many thanks ahead of time if you post on this thread to give me some incite, I'm kind of a slow learner, thorough but slow. I just want to learn to write my code as neat and non-convoluted as possible.
~Cheers!

Space for rent.
Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 2nd Sep 2010 23:51 Edited at: 2nd Sep 2010 23:54
This topic has been endlessly debated, and it really just depends on what you are comfortable with.

I tend to use gosubs to keep tasks organized, and my code easy to maintain, like this...



I usually only use functions when I've got some values to pass to the function, and then get a value back in return.

like...



But I break my own rules all the time.

EDIT: and I think you've got it backwards, global variables are changeable in functions, that's why they are global. For instance if you have a local variable "x", then using "x" inside a function does not affect it. But if "x" is global, then using "x" inside a function will change it outside the function. Does that make sense?

Oboro
14
Years of Service
User Offline
Joined: 27th Jul 2010
Location: Gilmer, TX
Posted: 3rd Sep 2010 00:11
OH! Thank you so much for such a quick response, so functions can effect globals? I thought that a function could only effect one variable at a time, function name (variable to be returned). So you can change and effect all of your globals from within your functions? That's really handy if so...
Thanks~

Space for rent.
mikewhl
14
Years of Service
User Offline
Joined: 19th Jul 2010
Location: Columbus,Ohio
Posted: 3rd Sep 2010 00:15
Another thing only functions can be put into #include file.
In the tut I was reading he used an example of camera control in a function, in an #include file, so if you need this cam control in your project, just add it with the #include command and there you go added cam without typing the code for it again.

Please correct me if I am wrong, but that's what I thought he was saying.

Mike

put this in your code snippets, code lang = dbp
Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 3rd Sep 2010 01:20
Quote: " function name (variable to be returned) "


Actually it's

function_name(variable list passed by the calling statement)

and at the end of the function

endfunction variable_passed_back_to_the_calling_statement

Keeping track of which variables are global can be important.

@mikewhl - good point! I've never done that, it's so easy to cut and paste. But it's nice to know it's there if I need it! I should really start using includes

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 3rd Sep 2010 02:26
Functions are just as fast as subs if you don't have any parameters. They still have advantages over gosubs though because they allow you to make local variables, which allow your code to grow without naming collisions.

"everyone forgets a semi-colon sometimes." - Phaelax
Oboro
14
Years of Service
User Offline
Joined: 27th Jul 2010
Location: Gilmer, TX
Posted: 3rd Sep 2010 16:28 Edited at: 3rd Sep 2010 16:30
So with a function you can name local variables that only exist within the function yes?

So those variables I would apply a value through a function to a number of Globals?
Code untested but...
i.e.:

That is handy for running over and over again... hmm. I must experiment with this... Would this help the "random" seed generator be a little more random? I'm trying to make the comp roll 4 six sided dice and I tend to get a lot of the same numbers I guess it's to be expected with the limited amount of sides though...

Space for rent.
Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 3rd Sep 2010 20:05
Yes, but...

Since Zort is global, and Pinky() doesn't accept any parameters, and doesn't return any new value, you might as well use a subroutine to do the same thing.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 7th Sep 2010 18:01
Quote: "Would this help the "random" seed generator be a little more random?"


To make random number picking more random use RANDOMIZE TIMER() only once at the beginning of the program (never put it in a loop).

Here's your function returning a value:


slyvnr
14
Years of Service
User Offline
Joined: 2nd Nov 2009
Location: Texas
Posted: 10th Sep 2010 17:36 Edited at: 10th Sep 2010 17:39
Functions can actually have 255 variables passed to them.


Of course why you need 255 variables passed to the function is beyond me.

As far as what kind of variables a function can manipulate, functions can utilize: passed variables (non-global), global variables and local variables.


Functions can have parameters passed to them or not. Depends on your needs.

I usually write functions for things that I can see I will need/can use in more than one project and then #Include that as my FUNCTIONS_LIB. I can call any, all or none of my custom functions as needed and I dont have to worry about have I written that yet.

Normally I have two function libraries in each project. A Generic one that I add to over time as I use the functions in more and more projects and a project specific one that is used only in that project and I did not want to use GoSubs or needed parameters passed.

Hope this helps your understanding.

Slyvnr

Slyvnr

Login to post a reply

Server time is: 2024-09-28 22:38:53
Your offset time is: 2024-09-28 22:38:53