In C++, function parameters can be passed by value or by reference, and can have default values. You can also define multiple functions with the same name (overloading) but different parameter lists. Here are some details about each of these features:
1. Pass-by-value: When a function is called with a value as a parameter, a copy of the value is created and passed to the function. Any changes made to the parameter inside the function do not affect the original value. For example:
void increment(int x) { x++; } int main() { int a = 3; increment(a); cout << a << endl; // output: 3 return 0; }
In this example, the `increment` function takes an integer parameter `x` by value. When the `increment` function is called with `a` as the argument, a copy of the value of `a` is passed to the function. The function increments the value of the copy, but the original value of `a` is not changed.
2. Pass-by-reference: When a function is called with a reference to a value as a parameter, the function operates directly on the original value. Changes made to the parameter inside the function affect the original value. To pass a parameter by reference, you use the `&` operator. For example:
void increment(int &x) { x++; } int main() { int a =3; increment(a); cout << a << endl; // output: 4 return 0; }
In this example, the `increment` function takes an integer reference parameter `x`. When the `increment` function is called with `a` as the argument, a reference to the value of `a` is passed to the function. The function increments the value of the original variable, and the value of `a` is changed.
3. Default values: You can assign default values to function parameters, which means that if those parameters are not provided when the function is called, the default value will be used. Default values are defined in the function declaration. For example:
void greet(string name = "World") { cout << "Hello, " << name << "!" << endl; } int main() { greet(); // output: Hello, World! greet("Alice"); // output: Hello, Alice! return 0; }
In this example, the `greet` function takes a string parameter `name` with a default value of "World". When the function is called without any arguments, the default value "World" is used. When the function is called with the argument "Alice", the provided value is used instead of the default value.
4. Overloading: You can define multiple functions with the same name but different parameter lists, which is called function overloading. The compiler will choose theappropriate function to call based on the number and types of arguments provided. For example:
void print(int x) { cout << "The integer value is: " << x << endl; } void print(double x) { cout << "The double value is: " << x << endl; } int main() { print(3); // output: The integer value is: 3 print(3.14); // output: The double value is: 3.14 return 0; }
In this example, the `print` function is defined twice, once for an integer parameter and once for a double parameter. When the `print` function is called with an integer argument, the first function is called, and when it is called with a double argument, the second function is called.
By understanding how to use different function parameter types in C++, you can write more flexible and powerful functions that can handle a wider range of situations and inputs.