The data types that are predefined and are supported by programming language are known as primitive data types. For example, the primitive data types for the C++ language are as follows:
bool
short int
int
long
float
double
char
const
primitive data type parametersPrimitive data type parameters can be declared constant.
int sum(const int a, const int b)
The following sample code shows the const
datatype parameters:
#include <iostream>using namespace std;int sum(const int a, const int b){return a+b;}int increment(int a){return ++a;}//int incrementconst(const int a){// return ++a;//}int main() {int a =10;int b=20;cout << "Sum is "<<sum(a,b)<<'\n';cout<< "Increment is "<<increment(a);//cout<< "Constant patrameters increment is "<<incrementconst(a);return 0;}
const
data type use caseIf we want to modify the parameter’s value, then it should not be constant.
This is because when we write a parameter with the const
keyword, there will be an error while modifying the parameter.
In the above code, we use a non-const
parameter that can be modified easily. However, if we use const
parameters, the compiler will give an error.
Note: We can uncomment lines 10, 11, 12, and 18 to check the type of error we get when modifying a constant variable.
Free Resources