Constants
In the good old days, programmers would use preprocessor directive to define constants.
e.g.
#define s 10
But we now know that, these are processed and translated by pre-processors and compiler never gets to see them. Hence they are error prone.
So the good practice is to use constants instead. In C++ constants are defined with keyword const.
A constant promises that this entity is never going to change. And if we accidentally try to modify a const, compiler gives a syntax error.
A constant is defined similar to a variable - data type followed by name of the constant. But we add the prefix const to this definition.
And a constant must always be initialized.
e.g.
const int a = 10;
const char ch='+';
Here in this example, we have defined a as integer constant with value 10. And ch as character constant with value '+'.
Let us look at an example to find out what happens if we try to modify a const.
We have defined a constant b and initialized this with 10.
But in the third line, we try to increment a - which is not allowed.
What happens when we compile the program above? We get a syntax error -
default.cpp: In function ‘int main()’:
default.cpp:8:7: error: assignment of read-only variable ‘b’
b = 18;
So compiler is telling us that we are trying to modify a constant.
This shows that, by using const s, we can avoid accidental modifications of constant values.
Whenever a local variable or parameter should not be modified, we have to define it as a constant.
We can define parameters also as constants. If you think that does not make sense - parameters are call by value, you are forgetting about reference parameters.
Very often reference parameters are made constant references.
There are other uses of const keyword in C++. We can create constant objects and we can even have member functions and data members of a class as constant.
Constant parameters to functions
A function need not have constant parameters. Because, any way the parameters are not modified in the caller. Call by value, remember?
This is true for ordinary parameters. A reference parameter can be modified by a function. Isn't it?
If a function has a constant reference parameter, then this parameter can not be modified by the function.
But if the function should not modify these values, why would anyone use reference parameters instead of value parameters?
The answer is - constant reference parameters are useful when sending large objects to functions without the need of copying them. (Remember that ordinary parameters are always a copy of original arguments. ) This saves time and makes code more efficient.
Let us look at an example program which has a function with constant reference parameter.
Here cube function takes a - as a constant reference parameter. - a is a reference to argument - but can not be modified by cube function.
What happens if we uncomment the line a++? We get a syntax error. Attempt to modify a read only variable.
We will discuss about constant objects, constant data members of a class or even constant methods of a class and mutables later.
Exercises :
1) Write a program with a constant PI and use this to find area of a circle, after reading radius of circle.
2) Now modify the value of PI to 3.1. What do you see?
3) Write a function to find sum of elements of an array. Make array as a constant reference parameter.