When do we need to use default arguments and constant arguments in a function?
Answers
Answered by
2
Default Arguments in C++
A default argument is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn’t provide a value for the argument with default value.
Following is a simple C++ example to demonstrate use of default arguments. We don’t have to write 3 sum functions, only one function works by using default values for 3rd and 4th arguments.
#include<iostream>using namespace std; // A function with default arguments, it can be called with // 2 arguments or 3 arguments or 4 arguments.int sum(int x, int y, int z=0, int w=0){ return (x + y + z + w);} /* Drier program to test above function*/int main(){
cout << sum(10, 15) << endl; cout << sum(10, 15, 25) << endl; cout << sum(10, 15, 25, 30) << endl; return 0;}
Run on IDE
Output:
25 50 80
Key Points:
Default arguments are different from constant arguments as constant arguments can’t be changed whereas default arguments can be overwritten if required.
Default arguments are overwritten when calling function provides values for them. For example, calling of function sum(10, 15, 25, 30) overwrites the value of z and w to 25 and 30 respectively.
During calling of function, arguments from calling function to called function are copied from left to right. Therefore, sum(10, 15, 25) will assign 10, 20 and 25 to x, y and z. Therefore, default value is used for w only.
Once default value is used for an argument in function definition, all subsequent arguments to it must have default value. It can also be stated as default arguments are assigned from right to left. For example, the following function definition is invalid as subsequent argument of default variable z is not default.
A default argument is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn’t provide a value for the argument with default value.
Following is a simple C++ example to demonstrate use of default arguments. We don’t have to write 3 sum functions, only one function works by using default values for 3rd and 4th arguments.
#include<iostream>using namespace std; // A function with default arguments, it can be called with // 2 arguments or 3 arguments or 4 arguments.int sum(int x, int y, int z=0, int w=0){ return (x + y + z + w);} /* Drier program to test above function*/int main(){
cout << sum(10, 15) << endl; cout << sum(10, 15, 25) << endl; cout << sum(10, 15, 25, 30) << endl; return 0;}
Run on IDE
Output:
25 50 80
Key Points:
Default arguments are different from constant arguments as constant arguments can’t be changed whereas default arguments can be overwritten if required.
Default arguments are overwritten when calling function provides values for them. For example, calling of function sum(10, 15, 25, 30) overwrites the value of z and w to 25 and 30 respectively.
During calling of function, arguments from calling function to called function are copied from left to right. Therefore, sum(10, 15, 25) will assign 10, 20 and 25 to x, y and z. Therefore, default value is used for w only.
Once default value is used for an argument in function definition, all subsequent arguments to it must have default value. It can also be stated as default arguments are assigned from right to left. For example, the following function definition is invalid as subsequent argument of default variable z is not default.
Similar questions
Math,
8 months ago
Geography,
8 months ago
Math,
8 months ago
Computer Science,
1 year ago
Science,
1 year ago
Math,
1 year ago
Accountancy,
1 year ago