-2

In what general occasions are constants used instead of variables. I need a few examples.

Thanks in advance.

3 Answers 3

4

A variable, as the name implies, varies over time. Variables mostly allocate memory. In your code, when you declare that a value will not change, the compiler can do a series of optimizations (no space is allocated for constants on stack) and this is the foremost advantage of Constants.

Update

You may ask why do we use Constants after all?

It's a good question, actually, we can use literal numbers instead of constants. it does not make any difference for the compiler since it sees both the same. However, in order to have a more readable code (--programming good practice), we'd better use constants.

Using constants, you can also save your time!. To be more specific, take below as an example:

Suppose a rate value for some products in a shopping system (rate value = 8.14). Your system has worked with this constant for several months. But then after some months, you may want to change the rate value, right?. What are you going to do? You have one awful option! Changing all the literals numbers which equal 8.14! But when you declare rate as a constant you just need to change the constant value once and then changes will propagate all over the code. So you see that by using constants you do not need to find 8.14's (literal numbers) and change them one by one.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you give me a few scenarios that constants are used instead of variables?
PI number is the most famous constant in programming languages. Since this number won't change and you can use this constant everywhere u want. PI = 3.1415. I'm trying to update the answer for some minutes :) @NishanFernando
Constants are also used when you need the value to remain unchanged. If you give a variable to a function, and it changes its value, then your entire program can fail. Constants are also useful when you use the same number over and over again. It's safer to define it in one place, than to enter it each time, and probably make a typo somewhere.
2

Constants are used when you want to assign a value that doesn't change. This is helpful because if you try to change this, you will receive an error.

It is also great for readability of the code. A person who reads your code will now know that this particular value will never change.

For example:

$name = 'Danny'; // this could change if I ever changed my name const SECONDS_IN_MINUTE = 60; // this will never change, so we assign it as a constant 

Comments

0

You use a constant, when the value of a variable never changes during the lifetime of your program. Once you defined a constant x, you can't change it's value anymore. Think of pi. Pi is a constant with value 3.1415. This will never change during your programs lifecyle.

const pi = 3.14159265359 

When you use a variable instead, you can change it's value as often as you want to.

int x = 1; x = 7; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.