Can you have more than one argument with a default value? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can you have more than one argument with a default value?

If you do, how does the interpreter decide which parameter goes to which argument?

26th Jul 2016, 9:53 PM
Barcode
Barcode - avatar
3 Answers
0
Your default value should be last when you define your function parameters. As long as the order is the same as the function call arguments it will work fine. You normally use positional arguments with multiple parameters. If you want to override the default parameter, you can use keyword arguments. It's just the name of the parameter put into the argument and set equal to a value.
28th Jul 2016, 11:22 PM
daniel fancher
daniel fancher - avatar
0
Not an answer I'm afraid, but u second the question. For instance, if you had def function(a, b=1, c=2) and you passed f(4,5), would Python always assume that b was passed as 5 and c is default?
31st Aug 2016, 9:26 PM
Andrew Rose
Andrew Rose - avatar
- 1
For a function to work without error, you need to pass exact number of arguments as the number of parameters used for function defination. eg. def add(a,b): s=a+b print s add(5,10) # works without error add(3) # Error
28th Jul 2016, 8:09 AM
Prashant Shahi
Prashant Shahi - avatar