(SNIP!)
(SNIP!)
const is completely redundant to ordinary variables, and its implemented more stupidly. so don't use it.
no, i mean it: const is nothing but a brain diaper.
(SNIP!)
I use constants they are very handy, change one value in code and throughout your program the values are updated.
Only a fool would argue against such a convenience.
"define me and I will protect thee from thyself and any muck-ery which might be afoot down the road"
@bplus That's the most common use for "constants" in a program, but you don't actually need the CONST to do it...
pi = 3.14159
' blah, blah, blah
PRINT 27 * pi
' blah, blah, blah
PRINT pi / 2
' etc, etc., etc.
So just change the variable pi at the top, to any number of acceptable digits, and it works just the same as CONST, except it won't throw a compiler error if you mistakenly assign pi to a different number someplace else in the program.
Pete
In a huge program with a lot of stuff going on and risk of typos, I could imagine defining something as a constant could help to easily catch a mistake when at some point assigning a new value to something that was meant to stay a constant.
Danged if I can think of a good example off the top of my head...
Which is easier to type:
Const pi = 3.1415
or
Dim Shared pi as some type
pi = 3.1415
Just wanted to say thanks for all comments. I've decided to introduce a new statement called "EQN" (for equation). Although EQN and CONST use the same background stuff to implement them, I like the semantic differentiation. I'm not liking a CONST statement allowing appearance of not looking like a constant.
If anybody knows of any BASIC that already has an EQN statement or function, please let me know.
Imagine an income tax program or something like that, here's how EQN works:Code: QB64: [Select]
Ah...
Pi = 3.1419
Nice thing about BASIC. It automatically uses a default SINGLE designation for Pi, in this case. No need to DIM it.
Pete
I take a dim view of Dim.
const is better for constants, you have be careful with #define/macros
suppose that your define was #DEFINE p2 Pi + Pi then x=p2*4 would become Pi+Pi*4 unless you enclose it in parentheses
Just wanted to say thanks for all comments. I've decided to introduce a new statement called "EQN" (for equation). Although EQN and CONST use the same background stuff to implement them, I like the semantic differentiation. I'm not liking a CONST statement allowing appearance of not looking like a constant.
If anybody knows of any BASIC that already has an EQN statement or function, please let me know.
Imagine an income tax program or something like that, here's how EQN works:Code: QB64: [Select]
My philosophy on constants: it's something between a micro-optimization and a sanity check for a symbolic representation of a literal (not live-computed) value. I.e.: it's ugly to type 3.141593 every time you need it so CONST pi=4*atn(1) lets the compiler calculate it for you at compile time then type it in for you. The physical result is that it's global, just like a literal "3.1415.." is, and should compile with the same performance as a literal value. What Steve and the gang actually do with it is none of my business, as long as my code looks pretty.
That one works, but I wonder if relying on the compiler to produce the value of some other calculated number to use as a constant could cause an inconsistency if one program was compiled on a 32-bit and another on a 64-bit system?
Pete
(SNIP!)
Somewhere in your code you have a line like: IF program_calculation = 0.11111111111111117896 * 2 THEN...
Now, on the 32-bit OS, x and 0.11111111111111117896 are going to be equal. On a 64-bit OS, those values might be minutely different. Your 32-bit compiled EXE may not run exactly the same on the 64-bit EXE, as the values won't match -- which makes it advisable to use CONST over literals in such cases, or to at least not mix-and-match them any more than possible.
(SNIP!)
Which is easier to type:
Const pi = 3.1415
or
Dim Shared pi as some type
pi = 3.1415
IF BALLPARK(program_calculation, 0.11111111111111117896 * 2, +-0.11111111 ) ???
Just to say that has me thinking about a programming language full of funny expressions and colloquialisms .
So many interests, so little time ...
Makes me wonder how the human species managed to land anything on Mars ...
(SNIP!) why do people think if Martians existed, they would be smarter than humans? (SNIP!)
Pete
@johnno56
One may argue that they are man made constructs invented to prolong the illusion of man's existence.
That one works, but I wonder if relying on the compiler to produce the value of some other calculated number to use as a constant could cause an inconsistency if one program was compiled on a 32-bit and another on a 64-bit system?
Pete