I have a piece of code like this:
#include <stdio.h>
int add(const int x, const int y);
int main()
{
printf("%d", add(9, 8));
return 0;
}
int add(int x, int y)
{
return x + y;
}
I forward declared the function "add" with const parameters after that i defined it without the const parameter, and when i compile it the compiler gives no complain.
The out put of the program is: 17. Why does this happen ?
Always use const on function parameters passed by reference or pointer when their contents (what they point to) are intended NOT to be changed. This way, it becomes obvious when a variable passed by reference or pointer IS expected to be changed, because it will lack const .
Parameter types In a function declaration, or prototype, the type of each parameter must be specified. In the function definition, the type of each parameter must also be specified. In the function definition, if the type of a parameter is not specified, it is assumed to be int .
A constant parameter, declared by the keyword const , is a read-only parameter. This means that we can not modify the value of the constant parameter in the function body. Using the const keyword tells the compiler that the value of that parameter will not be changed inside the function.
The function declarator includes the list of parameters that can be passed to the function when it is called by another function, or by itself. In C++, the parameter list of a function is referred to as its signature. The name and signature of a function uniquely identify it.
This problem can be avoided by declaring n as a const parameter as shown below (the function body is omitted to save space). Now the compiler reports an error whenever the value of n is modified in the function body. Note that we can pass either a const or a non-const variable as an argument where a const parameter is expected.
When you send it to consttest_func (consttest_var), the function expects const unsigned int as declared: void consttest_func (const unsigned int consttest_var1) the function itself is not allowed to change the argument, because it is const, but outside of the function's scope, the variable isn't const, hence, can be modified
Sometimes we may want that a function should not modify the value of a parameter passed to it, either directly within that function or indirectly in some other function called form it. This can be achieved using const parameters. Consider, for example, the function given below to calculate the sum of the first n integer numbers.
Note that we can pass either a const or a non-const variable as an argument where a const parameter is expected. However, the compiler gives a warning when we pass a const variable where a non-const parameter is expected.
Well, the function declaration and function definition have to be compatible, we all know that. So the same name and same return type and type of parameters. So we know from C11 6.7.6.3p15:
For two function types to be compatible, [...] corresponding parameters shall have compatible types. [...]
But, there is an explicit backdoor, later in that text:
(In the determination of type compatibility [...] each parameter declared with qualified type is taken as having the unqualified version of its declared type.)
The type-qualifier is for example const
. And it is ignored. You can put any type-qualifier and it is just ignored when checking if the function declarations are the same with each other.
int func(int n);
int func(volatile int n);
int func(const int n);
int func(const volatile int n);
int func(int n) {
return n;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With