C++ Function Parameters

In C++, function parameters are the variables declared in the function signature, that are used to pass data into the function. Functions can have any number of parameters, or none at all. There are two types of function parameters:

  • Value parameters: these are also called call-by-value, where the function receives a copy of the value passed as an argument, so changes made to the parameter in the function will not affect the original variable.
  • Reference parameters: these are also called call-by-reference, where the function receives the memory address of the variable, so changes made to the parameter in the function will affect the original variable.

Value Parameters

Value parameters are the most common type of function parameter. They are used to pass a copy of the value of the variable to the function. In this case, any changes made to the parameter inside the function will not affect the original variable. Here is an example of a function that takes two value parameters:

void doubleValue(int x, int y) {
    x = x * 2;
    y = y * 2;
}
int main() {
int a = 5, b = 7;
doubleValue(a, b);
cout << a << " " << b;
return 0;
}

Output:

5 7

In this example, the doubleValue function takes two int parameters and doubles their values. However, since the function is using value parameters, the changes made to the parameters do not affect the original variables a and b.

Reference Parameters

Reference parameters are used to pass the memory address of a variable to the function. In this case, any changes made to the parameter inside the function will affect the original variable. Here is an example of a function that takes two reference parameters:

void doubleValue(int &x, int &y) {
    x = x * 2;
    y = y * 2;
}
int main() {
int a = 5, b = 7;
doubleValue(a, b);
cout << a << " " << b;
return 0;
}

Output:

10 14

In this example, the doubleValue function takes two int reference parameters and doubles their values. Since the function is using reference parameters, the changes made to the parameters affect the original variables a and b.

Multiple Parameters

Functions can take multiple parameters of different types. For example:

int add(int x, int y, int z) {
    return x + y + z;
}

In this example, the add function takes three int parameters and returns their sum.

Functions can also take multiple parameters of the same type. For example:

int sum(int a, int b, int c, int d) {
    return a + b + c + d;
}

In this example, the sum function takes four int parameters and returns their sum.

It is important to note that when passing arguments to a function, the number and type of arguments must match the number and type of parameters in the function signature. If the number or type of arguments do not match the parameters, the program will not compile.

Additionally, C++ also supports default arguments and variadic functions, which allow for more flexibility in function parameters. Default arguments allow for parameters to have default values, in case no value is passed for that parameter. Variadic functions, on the other hand, allow for a variable number of arguments to be passed to the function.

Default Arguments

Default arguments can be used to provide a default value for a parameter in case no value is passed for that parameter. Here is an example of a function that uses a default argument:

int add(int x, int y, int z = 0) {
    return x + y + z;
}
int main() {
    cout << add(1, 2) << endl; 
    cout << add(1, 2, 3) << endl; 
    return 0;
}

Output:

3
6

In this example, the add function takes three int parameters and returns their sum. The third parameter, z, has a default value of 0. In the first call to the function in main, only two arguments are passed. The third argument is not provided, so the default value of 0 is used. In the second call to the function in main, all three arguments are provided.

Variadic Functions

Variadic functions are functions that take a variable number of arguments. C++ uses the ellipsis (…) to indicate a variable number of arguments. Here is an example of a variadic function that calculates the average of a variable number of numbers:

double average(int count, ...) {
    va_list args;
    va_start(args, count);
    int sum = 0;
    for(int i = 0; i < count; i++) {
        sum += va_arg(args, int);
    }
    va_end(args);
    return (double)sum / count;
}
int main() {
    cout << average(3, 1, 2, 3) << endl;
    cout << average(4, 1, 2, 3, 4) << endl;
    return 0;
}

Output:

2
2.5

In this example, the average function takes an int parameter indicating the number of arguments, followed by a variable number of int arguments. The function uses va_list, va_start, va_arg, and va_end to access the variable number of arguments. The function calculates the sum of the arguments and returns the average.

In conclusion, C++ functions can take various types of parameters, including value parameters, reference parameters, default arguments, and variable number of arguments. Understanding the different types of parameters and how they work is crucial in writing efficient and effective C++ code.

Leave a Reply

Your email address will not be published. Required fields are marked *