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 / Is there any advantage of declaring a variable as CONSTANT?

Author
Message
Quel
15
Years of Service
User Offline
Joined: 13th Mar 2009
Location:
Posted: 25th May 2011 18:51
I didn't find anything about this, is it faster to handle for the computer if it knows it's just rigid value, or what is it for?

-In.Dev.X: A unique heavy story based shoot'em ~25%
-CoreFleet: An underground commander unit based RTS ~15%
-TailsVSEggman: An Sonic themed RTS under development for idea presentation to Sega ~15%
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 25th May 2011 18:58 Edited at: 25th May 2011 18:59
If you set a variable that isn't global, then it's considered local.

Local variables are not accessible by functions. So if you try and access it inside a function, it creates a new local variable that is only available to that function. Once you exit the function that variable no longer exists. The original one you created before the function still does though, and outside the function it is available.

If you set the variable as a global, then it can be read and modified anywhere in the program.

Quel
15
Years of Service
User Offline
Joined: 13th Mar 2009
Location:
Posted: 25th May 2011 19:07
I'm aware of those, i was thinking about this: "#constant VARIABLE, VALUE"

-In.Dev.X: A unique heavy story based shoot'em ~25%
-CoreFleet: An underground commander unit based RTS ~15%
-TailsVSEggman: An Sonic themed RTS under development for idea presentation to Sega ~15%
Qqite
14
Years of Service
User Offline
Joined: 28th Feb 2010
Location: Florida, U.S.
Posted: 25th May 2011 19:20
@KISTech
Isn't it when you declare something as LOCAL then it holds its memory even if you leave the function? Whereas if you don't declare it as local then the language automatically destroys the variable.

I could be wrong but I think what is going on is that if you don't declare a variable as local then the interpretter puts in a destructor for the variable on exit.

Ventures of the worlds around us are limited only by our imagination.
tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 25th May 2011 19:27
Quote: "Isn't it when you declare something as LOCAL then it holds its memory even if you leave the function?"

I would be really surprised by this as that would defeat the purpose of calling it Local.

Quote: "I'm aware of those, i was thinking about this: "#constant VARIABLE, VALUE""

That is a replace like C's #define so if you are replacing it with a static value like #constant MYSTRING "Hello" then you're asking how the Compiler handles a raw string in a program line and if it's faster than reading from a Global defined variable. I would assume it is faster but I don't know such things. Diggsey and IanM who like looking at EBM files can probably tell you.

To answer your second question, #constant is useful for those magic numbers that you may have scattered around in your code, like MAX_ENEMIES. So in all your loops "for e = 1 to 100" you replace it with "for e = 1 to MAX_ENEMIES" and then whenever you decide to add more enemies you can just bump up the number in the #constant line. I use it as a typical enumerator as well, so NORTH/SOUTH/EAST/WEST all have number constants so the code is more readable.


A 3D marble platformer using Newton physics.
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 25th May 2011 19:29
Quote: "I could be wrong but I think what is going on is that if you don't declare a variable as local then the interpretter puts in a destructor for the variable on exit.
"

DBPro's syntax doesn't make it clear whether or not a variable is local or not. I think that saying:


is the same as


the latter is just easier to read. (well... clearer at least)

I guess it could be like a static variable? hopefully not because that would confuse me a lot.

Constant variables can't be changed. Uhh, the # before a command usually means that's a preprocessor command, so perhaps the variable is never allocated at all.

The only thing I can say with certainty is that in other languages, declaring something "constant" is mostly a pinky swear between functions, so you don't accidentally modify the value of something supposed to be constant.


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 25th May 2011 19:29
@Quel,
Sorry. I haven't had enough coffee yet.

There may be some special uses for constants, but for the most part they are just globals that don't (and can't) change.

I ran into an error with a select case statement once where I had changed the variables I was using to integers. The CASE operation didn't like that so I had to change the variable back to a constant. That's the only strange exception I've seen where you actually had to use a constant.

@Qqite,
A local in a function gets destroyed when you leave the function. Only a global remains until you exit the program. Constants and DIM'd arrays are automatically global.

Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 25th May 2011 20:25
I agree with Neuro Fuzzy. Constants are only a guarantee that we don't accidentally change the value and screw up our program despite our best efforts to create bugs.

enderleit
16
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 26th May 2011 06:28 Edited at: 26th May 2011 06:37
#constant MYVALUE 5

Anytime you put MYVALUE into your code it is replaced with whatever you have written after MYVALUE, in this case 5, by the pre-processor. This can be used to replace values that don't change during runtime, but that you might want to change later without having to change the value every place you have it in the code.

Basically it's not set up as a variable in the final program it's just a label for "5". (not including the "s)

You can also make macro's like this:



This would print 3 lines like this: HELLO 5 GOODBYE

In other languages like C++ you can string multiple commands together in one macro, but DBP seems to limit this to only 1 command.

Here's a more interesting example:

This will print the value of X every loop, and you can dec/inc it with the left/right arrowkeys.

Notice that the variable X doesn't need to be setup before defining the macros, as they are just a string that will replace the macro name when the pre-processor sees it. X could be replaced by Player.X and it would still be fine, as long as Player.x is defined before the MACRO is used.

enderleit
16
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 26th May 2011 06:44
Quote: "I ran into an error with a select case statement once where I had changed the variables I was using to integers. The CASE operation didn't like that so I had to change the variable back to a constant. That's the only strange exception I've seen where you actually had to use a constant."


This is because in a CASE statement you want the program to do something when it encounters a specific value. If you supply an integer this isn't a specific value as it can change.

So for example

select X
case Y
... do something...
endcase
endselect

As the compiler doesn't know what value Y will be when the program is running(as it can change dynamically) it is not a valid case. This is why you have to use a constant. Either defined with a #constant, or the actual value, like 5.

miso
13
Years of Service
User Offline
Joined: 16th Jun 2010
Location: Budapest, Hungary, 127.0.0.1
Posted: 26th May 2011 08:39
@QUEL -

Quote: "Is there any advantage of declaring a variable as CONSTANT?"


I belive, that consants are just replaced with values in the text source in a precompile phase, before starting the machine code compile part.


I can run the following source (dbpro 7.5), to get the camera position x:



No place like 127.0.0.1 .
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 26th May 2011 09:09
Constants are faster but inflexible...

x = x + MyVal

where MyVal = 1
as a constant: x = x + 1
as a variable: x = x + the value you'll find in the specified location

It's an extra lookup for the program to carry out as a variable.
However they can be extremely useful at design time as described above, to make your code easier to read and easier to edit.

tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 26th May 2011 22:15
I love using #constant since we don't have enum type stuff in DBPro. And I love that Indigo gives it its own color, I don't know if other IDEs do that, but it is very useful.


A 3D marble platformer using Newton physics.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th May 2011 23:33
I'm not sure everyone on this thread realises that DBPro #constants are not constants at all in the usual sense - if they are defined as variables they can change just like any other variable. The crucial thing to remember is that they are simply a message to the editor/compiler to replace every occurrence of the "constant" by whatever was in the definition - before doing the main compile.

Example:

tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 27th May 2011 23:36
gg-

I've never used constants like that, don't like it. I want to see what's under the hood as much as possible. :/ I only use it for magic numbers and static strings.

But I have heard of people using it to shorten commands and replace often used calculations.


A 3D marble platformer using Newton physics.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th May 2011 23:56
Yep.

But it is important to realise that #constant is not an alternative to local or global - it is a different beast entirely.
enderleit
16
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 28th May 2011 02:17
Why the heck are they called constants if they're not constant?

Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 28th May 2011 03:07 Edited at: 28th May 2011 03:08
Quote: "Why the heck are they called constants if they're not constant? "

Well, they are constant as in they cannot be changed during runtime (pretty obviously as each defined constant name is replaced by whatever follows it on the declaration line).
What's with that demonstration macro by the way? Who would ever use that? Now THIS is a real useful macro:


(Joking, obviously)


"Why do programmers get Halloween and Christmas mixed up?"

Login to post a reply

Server time is: 2024-05-19 08:43:34
Your offset time is: 2024-05-19 08:43:34