Parameter Passing in C++ Functions

Language:中文/EN

Parameter Passing in C++ Functions

In a C++ program, when calling a function, you need to pass an argument to it. Except for void parameters, argument passing is divided into pass by reference and pass by value.

Pass by Reference and Pass by Value

When a parameter is of reference type, the corresponding argument is said to be passed by reference, or the function is called by reference passing. Like other references, a reference parameter is an alias for the object it is bound to; that is, the reference parameter is an alias for its corresponding argument.

void reset(int &i){
    i = 0;   // The value of i will change
}

When initializing a non-reference type parameter, the initial value is copied to the variable, and the argument’s value is copied to the parameter. The parameter and the argument are two independent objects. This is called pass by value, or the function is called by value passing.

void reset(int i){
    i = 0;   // The value of the argument i will not change
}

It is important to note that if a pointer object is passed, you can change the value of the object it points to through the pointer. This is common in C, but in C++, if such a need arises, reference passing should be used.

void reset(int *i){
    *i = 0;   // The value of the object pointed to by pointer i will also change
}

Additionally, when passing a literal constant, if the literal is large, it is best to avoid the copy operation of pass by value and use pass by reference. It is important to note that for reference passing, the parameter must be defined as a const reference because a literal cannot be passed to a non-const reference parameter.

string::size_type find_char(string &s, char c);
// The first parameter of this function should use const string &s, otherwise, an error will occur during compilation

Functions with Variable Parameters

To write functions that can handle different numbers of arguments, the C++11 standard provides two main methods: if all argument types are the same, you can pass a standard library type called initializer_list; if the argument types differ, you can write a special function known as a variadic template, which is not detailed here.

Use the following form to write a function for outputting error messages, allowing it to work with a variable number of arguments:

void error_msg(initializer_list<string> i1){
    for(auto beg = i1.begin(); beg != i1.end(); beg++){
        cout << *beg << " ";
    }
    cout << endl;
}

Default Initialization of Parameters

Some functions have parameters that are often given the same value in many calls. In this case, we call this repeatedly occurring value the default argument of the function. It is important to note that once a parameter is given a default value, all subsequent parameters must also have default values.

typedef string::size_type sz;
// Define default values for function parameters in the declaration
string screen(sz ht = 24, sz wid = 80, char backgrnd = ' ');
string window;
window = screen();  // Equivalent to screen(24, 80, ' ')

For function declarations, the usual practice is to place them in header files, and each function is declared only once, although multiple declarations are allowed. However, it is important to note that within a given scope, a parameter can only be assigned a default argument once.

string screen(sz, sz, char = '*');  // Error, duplicate declaration