Why Const is Used at the End of Function Declaration in C++? Last Updated : 27 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the const keyword is used to define the constant values that cannot be changed during the execution of the program. Once a value or a function is declared as constant then its value becomes fixed and cannot be changed and if we try to change it, an error is thrown. In this article, we will learn about the const at the end of a function in C++. C++ Const Keyword at the End of Function DeclarationThe presence of const keyword at the end of a function declaration signifies that the function treats its member object as a constant and does not modify it. The use of the const keyword forces the compiler to ensure that object data is not changed or modified by the function. C++ Program to Demonstrate the Use of Const at the End of Function DeclarationThe below example demonstrates how we can use const at the end of a function in C++. C++ // C++ program to use const at the end of function #include <iostream> using namespace std; // Define a class named MyClass. class MyClass { public: // Constructor to initialize the data member. MyClass(int value) : data(value) { } // Const member function that does not modify the // object's data. void display() const { cout << "Data: " << data << endl; } // Non-const member function that modifies the object's // data. void increment() { ++data; } private: // Private data member. int data; }; int main() { // Const object of MyClass, initialized with the // value 10. const MyClass obj(10); // Calling the const member function to display the // data. obj.display(); // Uncommenting the below line would result in a // compilation error, as increment() is a non-const // member function and cannot be called on a const // object. obj.increment(); return 0; } OutputData: 10 Time Complexity: O(1)Auxilary Space: O(1) Comment More infoAdvertise with us Next Article Why Const is Used at the End of Function Declaration in C++? G gauravggeeksforgeeks Follow Improve Article Tags : C++ Programs C++ CPP-Functions CPP Examples Practice Tags : CPP Similar Reads When to Use Lambda Expressions Instead of Functions in C++? In C++, both lambda expressions and functions are used to define operations that can be invoked somewhere else in the code. However, there are some cases where using lambda expressions can be more beneficial than using functions. In this article, we will learn when to use lambda expressions instead 4 min read How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use 2 min read Different ways to use Const with Reference to a Pointer in C++ Before moving forward with using const with Reference to a Pointers, let us first see what they are one by one: Pointers are used to store the address of variables or a memory location. A variable can be declared as a pointer by putting â*â in the declaration. datatype *var_name; Example: CPP // C++ 5 min read How to use const with Pointers in C++? In C++, the const keyword is used as a type qualifier for defining read-only (immutable) objects that cannot be modified anywhere in their lifetime. It can be used in several ways with pointers, each serving a different purpose. In this article, we will learn how to use const qualifier with pointers 3 min read Function Pointer to Member Function in C++ In C++, function pointers enable users to treat functions as objects. They provide a way to pass functions as arguments to other functions. A function pointer to a member function is a pointer that points to a non-static member function of a class. In this article, we will learn how to use a functio 3 min read Like