Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Why constants and not variables?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cezna




PostPosted: Sun Jun 06, 2010 2:50 pm   Post subject: Why constants and not variables?

What exactly is the purpose of using constants, as opposed to variables?
I was thinking that constants probably speed up the program because Turing dosn't need to check if the value has changed all the time.

Also, if anyone knows other ways to speed up my programs, or some places where I can find other ways, please post your tips or links.
Sponsor
Sponsor
Sponsor
sponsor
Monduman11




PostPosted: Sun Jun 06, 2010 3:21 pm   Post subject: Re: Why constants and not variables?

constants are better if your working with numbers that are going to stay the same.. for example the value of pi, or the value of your jumpspeed. things that will not change throughout the program. use variables for things that do change. the position of a character, ball of anything that will be changed. hope this helps
DtY




PostPosted: Sun Jun 06, 2010 3:24 pm   Post subject: RE:Why constants and not variables?

You should use constants when you don't want the value to change. For example, the value of π will not change while the program is running, so if you do change it, it will stop the program and give you an error, this makes debugging easier, since you don't need to worry about your area of a circle function returning the wrong value, because π suddenly changed.

I'm not sure if Turing will, but some languages will also optimize around constants, for example
Turing:
const num := 5;
put num*3;
%Will become
put 15;

Since num will always be five, it can safely say that num*3 will always be 15. Again, I'm not sure if Turing will even optimize that, but it's good to do for style.
A.J




PostPosted: Sun Jun 06, 2010 7:07 pm   Post subject: RE:Why constants and not variables?

Well, using constants as opposed to actually using a value multiple times in a program makes your code easier to read. Also, initializing all your constants at a specific location in your program can save you a lot of time when updating a certain few of them (like x-velocity when creating a particle engine, etc...).
InfectedWar




PostPosted: Sun Jun 06, 2010 7:12 pm   Post subject: RE:Why constants and not variables?

Constants are not variables, they use absolutely use no memory when the program is running unlike variables.

The use of an constant is for repetitive values used over and over again in an program, if the value needs to be modified changing the value of the constant will change all these values.

All a constant does is when you run the program it replaces every part where the constant is called with the pre-defined value you choose. These values cannot be modified during run-time of the program because the constant is not a variable. During run-time of the program the the spots in your program instantly get switched with the value of your constant (Thus these values are hard coded in the program and cannot be changed during run-time)
InfectedWar




PostPosted: Sun Jun 06, 2010 8:11 pm   Post subject: RE:Why constants and not variables?

The speed of the program is determined by your computer and how efficient your variables types are (nat vs real), also how efficient is your program ex. you can get the same results with a loop running 5 times and 50 times. You would want to use the method that runs quicker (So the loop that runs 5 times)
Prabhakar Ragde




PostPosted: Sun Jun 06, 2010 8:28 pm   Post subject: Re: Why constants and not variables?

Cezna @ Sun Jun 06, 2010 2:50 pm wrote:
What exactly is the purpose of using constants, as opposed to variables?


The entire first-term CS course at UW uses only constants and never variables.
DemonWasp




PostPosted: Mon Jun 07, 2010 12:37 am   Post subject: RE:Why constants and not variables?

InfectedWar, both of your comments are full of falsehoods, mostly about performance.

Constants do use memory when a program is running - this memory is part of the data block that is loaded at runtime. While they don't need to be allocated at runtime, they are still in memory and therefore consume space.

Constants are generally not replaced at runtime. They are generally treated as "any other variable" at runtime. The difference is in the compilation step, where static checking is used to ensure that const variables are never changed. You might be thinking of C/C++'s #declare directive, but that replacement is also done at compile-time, not run-time.

A loop that runs five times is not necessarily faster or slower than a loop running 50 times. This depends entirely on the computation done by the inside of the loop.

The differences between data types like nat and real are trivial next to the slowdown imposed by Turing itself.


To answer the OP:
  • Constants can be used to replace "magic numbers" like gravity, pi and so forth, so that the use and function of values is made obvious.
  • Using constants can improve program performance, depending on the language, compiler / interpreter and so forth. This is not true of Turing. No environment I have ever heard of "keeps checking whether the value has changed".
  • General solutions to improving run speed include doing less work, switching to a language with some performance (any language other than Turing), and fooling the user into thinking that the program is going faster than it really is by trickery (this is probably the most difficult to use in general).
Sponsor
Sponsor
Sponsor
sponsor
Cezna




PostPosted: Mon Jun 07, 2010 5:39 am   Post subject: RE:Why constants and not variables?

Thank you to everybody!

I definitely understand now.
I knew what constants were, just not why I should use them as opposed to variables.
The debugging aspect clears it up a lot, and also the part about making code easier to understand.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: