Note: The purpose of adding const to your type and variable inside your argument is to catch errors if the user accidentally wants to change the original value of the variable.
#include <iostream>
usingnamespace std;
// Program to explain the constant argument to a function.
/* Note: Having const in the parameter will prevent
any change to variablve v inside the function
*/
void function (constint v = 0){
v = v*2; // Error, you cannot change the value of v.
cout << v << endl;
}
//// However this is possible:
// void function (const int v = 0){
// cout << v * 2 << endl; // You are changing output, not the value of v.
// }
int main(){
int x = 10;
cout << x << endl;
function(x);
return 0;
}
> Having const in the parameter will prevent any change to variablve v inside the function
Yes. Ideally, limit the const qualifier to the implementation; at best it is an implementation detail
To the caller, semantically it makes no difference whether the type of an argument passed by value is const-qualified or not. The const qualification is ignored for determining the type of the function.
#include <iostream>
#include <string>
#include <type_traits>
void function ( int v = 0 ) ; // declaration (header)
// definition of void function ( int )
void function( constint v ) { std::cout << v << '\n' ; /* definition */ }
// *** error: redefinition of void function ( int )
// void function( int v ) { std::cout << v << '\n' ; /* definition */ }
// declaration and definition
void function_two( constint v ) { std::cout << v << '\n' ; /* definition */ }
int main()
{
function(7) ;
using function_type = void( int ) ;
function_type* fn = function ; // fine
fn = function_two ; // also fine note: const is ignored
using function_type_const = void( constint ) ; // note: const is ignored
function_type_const* fnconst = function ; // fine
fnconst = function_two ; // also fine
std::cout << std::boolalpha << "void(int) and void( const int ) are the same type: "
<< std::is_same< void(int), void( constint ) >::value << '\n' ; // true
}
@integralfx, my professor taught me that if nothing is passed to the function from main then the value of the variable in the argument of the function is automatically considered 0, and thus it doesn't let compile error happen due to not having anything sent to it.
I mean this situation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
void function (constint v = 0){
cout << v << endl;
}
int main(){
int x = 10;
cout << x << endl;
function();
return 0;
}
It might be clearer with a non-zero default value. And without specifying a default value of any sort, an empty argument list is an error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
void function (constint v = 999){
cout << v << endl;
}
int main(){
int x = 10;
cout << x << endl;
function(555);
function();
return 0;
}
Yes. Ideally, limit the const qualifier to the implementation; at best it is an implementation detail
This is a good point. :)
I mark most parameters const, but I never considered the parameter's const qualifier as noise to the caller. I suppose the same advice would apply for volatile, too.