Const Functions in C++ are very important in programming and we can use such functions in different ways. In C++, there are many way to use functions and to perform specific functionality from it. We can have functions of different access modifiers, having different parameters etc. but we have a keyword in C++ that is known as “const”. Const is not only used with function but can be used with primitive data types, structure objects and class objects as well. We can also have const pointers while making different objects of it. I have explained the const pointers in detail in my article Const Pointer vs Pointer Const in C++. In this specific article, I will be explaining different criteria where you can use const with functions. Before starting it, I would like to simply introduce you about const keyword first.
Const: It is a predefined keyword written before the data type and it verifies that the value of variable should not be changed once it is initialized e.g. const int a=5;. Now if we will try to change value of a, compiler will not allow us to do so.
Const Functions in C++
Here I will show you different variations of functions using const keyword with it. I will assume that all of you know C++ syntax and have some programming experience in it. So, here I have created a simple console application and made a new cpp file called Test.cpp. Inside this file I have a class called Me and a main function outside that class as shown below.
class Me
{
int id;
// constructor
public:
Me()
{
id = 3;
}
void setId(int& newid)
{
id=newid;
}
};
So, in the above code I have a class and inside this class I have a constructor and a function that is used to update id by a reference variable newid as formal parameter. Now below this class, I have a main function as shown below.
int main()
{
int a = 2;
Me me;
me.setId(a);
cout << a << endl;
return 0;
}
If you will run this program now, you will see that it will print 2 as value of a. But If we carefully see the formal parameter of setId function that it is taking parameter as reference. Its mean that if we change the value of newid inside that function, it will also effect the value of a in main function. Try to write newid++; below id=newid; inside setId function and test the result yourself. If you will run after making this change, you will see that the value of a will be 3 inside main function.
What if we don’t want our function setId to not to make any change to the values of formal parameters? To achieve this we use const key word before the data types of formal parameters as shown below.
void setId(const int& newid)
Now if we will try to change the value of newid, compiler will not allow us to do that. Note that the newid must be passed by reference, not by value in order to achieve this functionality of const.
Tip 1: We can have two functions with same name but one function should a const formal reference parameter and other should be non-const reference parameter. Here if we send a const variable to this function, it will automatically call const formal reference parameter function and if we send a non-const variable to this function then it will automatically call non-const formal reference parameter function.
Now lets see one more case of const in functions, that is const functions. So, to see an example of it, lets add a new function inside our class Me. You need to add a function called changeId as shown below.
// const function can't modify any member variable of this class
void changeId() const
{
//id=id+5; // this is not allowed here
}
Now if we call this function changeId from our main function, then compiler will throw an error that variable id can not be change by a const function. To see this, we can call this function from our main function as shown below.
int main()
{
Me me;
me.changeId();
return 0;
}
Note: If we try to call another function from within the body of changeId const function, then that called function should also be const.
Tip 2: We can have two overloaded functions as well with same name and same parameters. But in this case one function has to be const and other shouldn’t. Here if a calling variable (me) is const, it will automatically call const overloaded function and if calling variable (me) is not const then it will automatically call non-const overloaded function.
Now one last thing is left, that is const return value by a function. To explain this I will add another function inside my class Me as shown below.
const int& getId()
{
return id;
}
Here a variable that will receive the value of id, returned by this function getId, then that variable will not be able to change the value. “But one thing has to be keep in mind that a variable that is receiving the returned value should also be const in order to achieve this purpose as shown below.
Me me; const int b=me.getId(); b++; // compiler will not allow this
So, this was all about this topic. I hope that I have covered all the main points of Const functions in C++. If any body have any problem or want to share something more interesting, he/she can do this by commenting below this post. Thank you 🙂

