constant argument to a function

Personal Contribution to open source C++.


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace 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 (const int 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;
}
Last edited on
if the user accidentally wants to change the original value of the variable.
void function (const int v = 0){
This arguments are passed by value, thus the original variable will not be changed.
> 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <type_traits>

void function ( int v = 0 ) ; // declaration (header)

// definition of void function ( int )
void function( const int 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( const int 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( const int ) ; // 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( const int ) >::value << '\n' ; // true
}

http://rextester.com/SJUFBL14720
http://rextester.com/FPHHN65548
@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>
using namespace std;

void function (const int v = 0){ 
    cout << v << endl;	
}

int main(){
	int x = 10;
	cout << x << endl;

	function();

	return 0;
}
closed account (48T7M4Gy)
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>
using namespace std;

void function (const int v = 999){ 
    cout << v << endl;	
}

int main(){
	int x = 10;
	cout << x << endl;

	function(555);
	function();

	return 0;
}
10
555
999
Last edited on
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.
Topic archived. No new replies allowed.