08-02-2025, 04:57 PM
I don't think there is any practical difference between
CONST Rate = 7.5 and Dim Shared Rate : Rate = 7.5
is there?
CONST Rate = 7.5 and Dim Shared Rate : Rate = 7.5
is there?
|
Using CONST
|
|
08-02-2025, 04:57 PM
I don't think there is any practical difference between
CONST Rate = 7.5 and Dim Shared Rate : Rate = 7.5 is there?
Only minor differences.
One -- once that CONST is set, it can never be changed accidently on you. That's a big bonus. Two -- that CONST is a direct substitution of the value into your code before compiling. Think of it as this: CONST HW = "Hello World!" PRINT HW What the compiler get is *actually*: PRINT "Hello World!" The compiler doesn't get the line with the CONST, nor does it get the CONST. The value is substituted in place of the CONST before being sent off to be compiled. With the DIM SHARED, you're using a variable to store that value. Variables are mutable and can be changed on you. You have to be careful you don't accidently type Rate = X when you're tired and what you really meant to type was X = RATE. Second issue with variables is they all have some overhead associated with them. They take up a spot in memory. The value is stored in memory. The compiled program has to look up that value at runtime.... They tend to just generally use a bite more memory and run a nanoprocess slower. Is it enough to be concerned over?? In most cases probably not, unless you're just pushing for every speed optimization you can possibly shoot for, for an innermost loop that's going to be called a bazillion times a second. Then, even the smallest of improvements can add up and affect performance over time. But those are basically the two changes in a nutshell, along with some esoteric rules regarding namespaces and such things, which don't come up enough for most folks to ever really need to worry about. Think on this: Code: (Select All)
In Foo, we start with a global shared version of X. But then we create a static and local version of X. So which X are we using after that? The local version of X!! What you'll see printed from above will be: 123 <-- Shared X 321 <-- Local X 123 <-- Shared X OMG! BRAIN BLOWN!!!!!! CONST, on the other hand, don't do that. If set in the main module, they remain for every point after that in your code. You can't make a local const, or a local variable. You set that CONST and *BY GOD*, it's going to damn stay constant!! ![]() So it's those type of things which make up the differences between the two.
08-03-2025, 03:24 PM
Thanks Steve. You had to be a professor in another life. Thanks again.
08-03-2025, 11:14 PM
@Dimster
He already teaches in this life! He is a teacher for free money but he charges with attention and learning.
08-04-2025, 01:03 AM
More like in his next life. In this life, I see Steve as more of an amateur-fessor, you know, more like rolling in unpaid knowledge. Hey, maybe he should run a baking forum instead. That way, at least he'd be rolling in dough. I left out farming, because rolling in the hay is a topic more suited to the NSFW sub-forum.
Good topic Dim, on CONST, +1. Personally I'm in love with confusion so I never use it. It's also a lot more fun when Steve is helping me debug a routine, because it always gives him a headache. I'm heavily invested in BAYRY, the stock symbol for Bayer, makers of aspirin and thanks to Steve, I'm the one rolling in dough! Seriously though, Steve explained some really great points in favor of the use of CONST, and I'll add one more, variable identification. Since CONST is typically placed at the top of the program code, it also provides a first glance to important variables that will be be found throughout the code. This provides a greater ease of understanding to coders who are seeing the program for the first time as well as defining a variable as a constant. BASICally, two birds with one stone, which should really speak to Canadians who can't own fire arms. Pete
08-04-2025, 02:15 AM
08-04-2025, 03:33 AM
Quite Steve. If the government finds out, they'll cut off my grant money. Then I'll be a po-fessor!
Pete
Shoot first and shoot people who ask questions, later.
08-04-2025, 05:35 AM
Didn't you say you were raised in a poor meat market or something? Wouldn't that make you a REpo-fesser?
08-04-2025, 04:00 PM
Quote:@McNeill - CONST, on the other hand, don't do that. If set in the main module, they remain for every point after that in your code. You can't make a local const, or a local variable. You set that CONST and *BY GOD*, it's going to damn stay constant!!But you can still change the value in a function, for example. That way, you can manipulate the constant, right? Code: (Select All)
08-04-2025, 05:02 PM
Ah, the old passing by reference trick. Nice, but it still doesn't change the value of c. I other words just print c after the function call and you will still get 123.
Pete |
|
« Next Oldest | Next Newest »
|
| Possibly Related Threads… | |||||
| Thread | Author | Replies | Views | Last Post | |
| Using CONST & _RGB used together seem to error... | Dav | 12 | 666 |
12-12-2025, 12:29 AM Last Post: Dav |
|