< Previous | Contents | Next >
Deciding How to Pass Arguments
At this point you’ve seen three different ways to pass arguments—by value, as a reference, and as a constant reference. So how do you decide which method to use? Here are some guidelines:
n By value. Pass by value when an argument variable is one of the funda- mental built-in types, such as bool, int, or float. Objects of these types are so small that passing by reference doesn’t result in any gain in effi- ciency. You should also pass by value when you want the computer to make a copy of a variable. You might want to use a copy if you plan to alter a parameter in a function, but you don’t want the actual argument variable to be affected.
n As a constant reference. Pass a constant reference when you want to efficiently pass a value that you don’t need to change.
n As a reference. Pass a reference only when you want to alter the value of the argument variable. However, you should try to avoid changing argument variables whenever possible.