Default Arguments in C++ (shared)

51 篇文章 2 订阅

Default Arguments in C++ - GeeksforGeeks

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesn’t provide a value for the argument with a default value.

Following is a simple C++ example to demonstrate the 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.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

#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);

}

  

/* Driver 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;

}

Output:

25
50
80

When Function overloading is done along with default values. Then we need to make sure it will not be ambiguous.
The compiler will throw error if ambiguous. Following is the modified version of above program.

#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); 

int sum(int x, int y, float z=0, float w=0

    return (x + y + z + w); 

/* Driver 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; 

}

Error:

prog.cpp: In function 'int main()':
prog.cpp:17:20: error: call of overloaded 
'sum(int, int)' is ambiguous
  cout << sum(10, 15) << endl; 
                    ^
prog.cpp:6:5: note: candidate: 
int sum(int, int, int, int)
 int sum(int x, int y, int z=0, int w=0) 
     ^
prog.cpp:10:5: note: candidate: 
int sum(int, int, float, float)
 int sum(int x, int y, float z=0, float w=0) 
     ^

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, 15 and 25 to x, y, and z. Therefore, the 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.

// Invalid because z has default value, but w after it 

// doesn't have default value

int sum(int x, int y, int z=0, int w)

Extra reading

1. C default arguments - Stack Overflow

==> be creative, use wrapper/base functions

2. c++ - Use of 'const' for function parameters - Stack Overflow

==> implicit documentation is important 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值