What is the difference between constants and variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the difference between constants and variables

What is the difference between constants and variables

23rd Feb 2019, 8:26 PM
Pierce
2 Answers
+ 6
Constants are defined in a way that you get an error when you try to change them in your code. Like this, when the source gets longer and longer and it's hard to keep track of everything, you are able to protect the parts of your data that shouldn't ever change. Variables can be changed whenever you want.
23rd Feb 2019, 8:35 PM
HonFu
HonFu - avatar
+ 3
From a mathematical point of view: The difference between variables and constants is that variables can change their value at any time but constants can never change their value. (The constants value is locked for the duration of the program). Constants can be very useful, Pi for instance is a good example to declare as a constant.
23rd Feb 2019, 8:36 PM
Timothy Murphy
Timothy Murphy - avatar