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 Returning a function pointer from a function in C/C++ In C/ C++, like normal data pointers(int *, char *, etc), there can be pointers to functions. Every function created in a program gets an address in memory since pointers can be used in C/C++, so a pointer to a function can also be created. Syntax: return type (*function_pointer_name) (argument_type 6 min read Calling a non-member function inside a class in C++ Member Function: It is a function that can be declared as members of a class. It is usually declared inside the class definition and works on data members of the same class. It can have access to private, public, and protected data members of the same class. This function is declared as shown below: 3 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 How to Use Default Arguments in Function Overloading in C++? In C++, we can provide the default values for the input arguments into the functions and it is also supported in function overloading. In this article, we will learn how to use default arguments in function overloading in C++. Default Arguments in Function Overloading in C++We can define the default 2 min read Why is Conversion From String Constant to 'char*' Valid in C but Invalid in C++? In both C and C++, strings are sequences of characters enclosed in double-quotes. In this article, we will learn why is the conversion from string constant to 'char*' valid in C but invalid in C++. Conversion From String Constant to char*In C, itâs permissible to assign a string constant to a âchar* 2 min read Function Overloading vs Function Templates in C++ In C++, both function overloading and function templates allow us to create functions that can operate on different types of data. While they might seem similar, they are used for different purposes. In this article, we will learn the differences between function overloading and function templates, 4 min read Like