Passing Parameters Using const

Using const with call-by-value parameters

Recall that variables which are designated as "const" cannot be changed. Any attempt to do so results in a compiler errors. Using "const" with parameters is a powerful tool for writing robust code.
  1. How do I do it?

    With a built-in type...

    Call-by-Value Call-by-Value with const
    int AddOne( int n ); int main ( ) { int x = 42; int y = AddOne (x); } int AddOne (int n) { return n + 1; } int AddOne( const int n ); int main ( ) { int x = 42; int y = AddOne (x); } int AddOne (const int n) { return n + 1; }

  2. Is it there a significant difference?

  3. Should I do it? While not "wrong", the conventional method is not to use "const" when passing built-in types by value. As the summary at the bottom of the page shows, strings, vectors , and other user-defined types are typically not passed by value.

Using const with call-by-reference parameters

  1. How do I do it?

    With a built-in type...

    Call-by-Reference Call-by-Reference with const
    void AddOne( int& n ); int main ( ) { int x = 42; AddOne( x ); } void AddOne (int& n) { n++; } void AddOne( const int& n ); int main ( ) { int x = 42; AddOne ( x ) } void AddOne (const int& n) { n++; // compiler error }

    With user-defined types....

    Call-by-Reference Call-by-Reference with const
    void Print( string& s ); int main ( ) { string Hello = "Hello, Bob\n"; Print( Hello ); } void Print (string& sentence) { cout << sentence << endl; } void Print( const string& s ); int main ( ) { string Hello = "Hello, Bob\n"; Print( Hello ); } void Print (const string& sentence) { cout << sentence << endl; }

  2. Is it there a significant difference?

    YES !

  3. Should I do it? Not for built-in types....use pass-by-value instead.
    Yes for user-defined types....it's preferred over pass-by-value because it save potentially inefficient copying of data.

A summary of const vs. non-const
parameters and arguments passed by reference


non-const argument const argument
reference parameter
OK
Not Allowed
reference to const parameter
OK
OK

A summary of how to pass parameters

Call-by... Built-in types User-defined types
Value Use call-by-value for built-in types when you want to pass a copy of the argument Don't pass user-defined types this way. Making a copy may be inefficient.
Const Value Generally, don't pass built-in types this way Don't pass user-defined types this way. Making a copy may be inefficient.
Reference Use call-by-reference for built-in types when your intention is that the function will change the argument being passed. Use call-by-reference for user-defined types when your intention is that the function will change the argument being passed.
Reference to const Don't pass built-in types this way. Although not "wrong", there's no savings and it's not the conventional method. Use pass-by-value instead. Use call-by-reference-to-const user-defined types when you don't want the function to change the argument. This method is preferred over pass-by-value because it accomplishes the same thing without copying.

The Bottom Line

The bottom line is this....with few exceptions
  1. Built-in types (int, char, float, etc) should be passed by value if the function is not changing the argument and should be passed by reference if the function is changing the argument. Don't use const with built-in paramaters.
  2. Always pass user-defined types (string, ostream, vectors, and types that you create) by reference, never by value. If the function is not changing the argument, use a const reference.


Last Modified: Monday, 28-Aug-2006 10:16:02 EDT