TITLE: Pass by value versus pass via const reference PROBLEM: dkarr@sierra.com (David Karr), 13 Apr 93 In my opinion, if you have a function that takes a single scalar value (single is somewhat irrelevant), then the formal parameter should be declared by-value. For old Fortran programmers, they may tend to declare the formal parameter as a "const reference". Either void func(int newValue); or void func(const int &newValue); Can someone provide crystal clear reasons why the first option (by-value) is better than the second (by-reference)? RESPONSE: barmar@think.com (Barry Margolin), 14 Apr 1993 Thinking Machines Corporation, Cambridge MA, USA Passing by value allows the compiler to optimize better. This is because the caller might pass a reference to a global variable, which might be modified directly or through other references. Consider: int global_int[10]; void func(const int &new_value) { for (int i = 0; i < 10; i++) global_int[i] = new_value + 1; return; } main () { func(global_int[1]); return; } The compiler can't keep new_value in a register, or move the calculation of new_value+1 out of the loop, because the assignment to global_int[1] will change the value of new_value. Remember, const really means "read-only"; it doesn't mean that the value can't change. [ This is true for built-in types like int. But you WANT to pass by const reference for larger objects. -adc ]