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 / #include, gosub and global...

Author
Message
mr Handy
11
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 5th Jan 2014 23:23
Hi. I am a little bit very confused with code struct even after a forum search. Let say I have a main dba file and a secondary dba file (using #include). In the secondary dba file I have a certain sub "test". Inside sub I declare global variable A. So I should have final code (compiled) structure like this:

1. ...start of main.dba code
2. #include secondary.dba
3. gosub "test"
4. ...end of main.dba code
5. ...start of secondary.dba code (attached to the end)
6. sub test: global A = 100
7. ...end of secondary.dba code

Is this global declaration correctly placed?
Can I use function to declare global there?

WickedX
10
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 6th Jan 2014 01:33 Edited at: 6th Jan 2014 01:38
Hi, mr Handy

Quote: "Is this global declaration correctly placed?"

In my opinion, No.

Quote: "Can I use function to declare global there?"

Yes.

Here is a general outline of the main source file, I use. Normally I don’t use the include directive but, instead include the files using the IDE.

Main.dba


This is the general outline I use for the for Initialization source.

Initialization.dba
mr Handy
11
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 6th Jan 2014 12:32 Edited at: 6th Jan 2014 12:35
Hi, WickedX!
Quote: "In my opinion, No."

umm but your example plan looks exactly as my plan Did I miss something?

And I heard attached dba's are always placed after the main source, so

would be after the main loop... and I heard that you should declare them at the very beginning of the code (main code?).

WickedX
10
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 6th Jan 2014 17:52
Quote: "6. sub test: global A = 100"


Notice, I define constants and udt and declare variables outside of a subroutine or function. Then I call a subroutine before the main loop to dimension arrays and initialize the variables.

Yes, included source file are added at the end of the main source. I have been using this outline for a while now and have not had any problems.
WickedX
10
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 6th Jan 2014 18:25
Attached is a simple project I am working on. When complete I'll upload the complete game and code anyway. Pay particular attention to the global g_GameState variable. Using my outline I change the variable in the MainMenu function. g_GameState is used in the main loop. If this variable is not global you would not be able to exit the program. So evidentially the variable is global.

Attachments

Login to view attachments
mr Handy
11
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 6th Jan 2014 19:26
Okay, I'll see your code! And I think I was confused by your declare global variables and initialize global variables. What's the difference?

WickedX
10
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 6th Jan 2014 20:02
Quote: "What's the difference?"


Declare global variable.


Initialize global variable.
mr Handy
11
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 6th Jan 2014 20:43
Now I am getting understand this!

Few questions left:
1. Why it is told to declare variables at top when you done this at the end and it is okay?
2. Why you used sub instead of a function? (and what sense to use sub at all?)

Thanks!!!

WickedX
10
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 6th Jan 2014 21:11 Edited at: 6th Jan 2014 21:20
Quote: "1. Why it is told to declare variables at top when you done this at the end and it is okay?"


A compiler will usually make more then one pass. So I think it really shouldn’t matter where you do things like define constants and user defined types and declare variables and global variables. I do believe this should be done outside of sub's or functions, as I can see that potentially causing problems.

Quote: "2. Why you used sub instead of a function? (and what sense to use sub at all?)"


Mostly this is my personal preference and if you define a variable in a function it will only be local to that function. Subroutines are said to be faster than functions.
mr Handy
11
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 6th Jan 2014 22:14
Thanks again!!!

And at the end of discussion, can you give me a short list of examples of Subroutines and Functions best usage? I mean when use what.

WickedX
10
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 6th Jan 2014 22:48 Edited at: 6th Jan 2014 23:51
If your don't need to pass any parameters and all variables used other than global have main scope, use subroutines. Otherwise use functions. This is not a law, you can decide how you want to use subroutine and/or function. But, be aware of variable scope.

You should take a look at the FPS Creator source code in the extras download for some ideas when to use sub's or functions.

Edit: Attached is the extras file, for your convenience.

Attachments

Login to view attachments
Kezzla
10
Years of Service
User Offline
Joined: 21st Aug 2008
Location: Where beer does flow and men chunder
Posted: 8th Jan 2014 04:23 Edited at: 8th Jan 2014 04:25
I personally use sub routines to break up tasks in my program flow.

I use functions for repeated calculations that are required for more than just one task.


sometimes you will have variables that permanently exist and change, subroutines will work well with these as changes in their data will be preserved.

Functions have self contained variables which can be nice if you to repeatedly use the same variables but do not wish to leave them with values in them. eg x y z co-ordinates of objects.

you could declare your variables as global. That way the values altered within the global variable will remain after it has completed rather than being deleted upon function completed.





eg. my screen setup sequence is a subroutine. It is only performed once at the start of the program.
Then I might load my images, this will be specific to this program and values will be required globally = subroutine.
Then i might set up my sky, which is performed once and required variables to exist during program execution - this will be a subroutine

Then I might set up my terrain. -this is a common setup function which i will use in a lot of programs. the textures and object number will be different each time. - this will be a function

mouselook using collision object - again common routine with variables-my object number may be different each program so it is a function


so my program flow would look like this.


you can see that my program flow is readable in plain english. This is important for productivity and debugging.
It wont always be possible for it to be so straight forward as your program becomes more complex, however it is good to try and keep it readable.

I find that using subroutines to break up chunks of code helps with readability.
I find that if I need a certain routine in more than one program and it requires variables then a function is called for.

This is just the way I use them.


so far as your program order is concerned, I think when the code is compiled the compiler tags your included files onto the end of the first pages code, so the include command may indeed work anywhere, however for readability sake I would always put includes at the top.
the same with gobal declarations. you don't want to go wading through pages of code to find where you left that global declaration.

in my case I would make a subroutine to house the global declarations - however I use indigo and it has a feature that allows to you select a routine or function from a dropdown menu and takes you right there, very nice feature says I.

That's how I go about things anyway.

Ok, Jokes over, No more eye burn.
mr Handy
11
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 8th Jan 2014 10:29
Thank you guys!

Login to post a reply

Server time is: 2019-04-26 11:57:17
Your offset time is: 2019-04-26 11:57:17