Argument Default value allow us to make function arguments optional.
If not passed a value, the argument is assigned its default before the function runs.
For example, here is a function that requires one argument and defaults two:
def f(a, b=2, c=3): print(a, b, c) # a required, b and c optional
When we call this function, we must provide a value for a, either by position or by keyword.
Providing values for b and c is optional.
If we don't pass values to b and c, they default to 2 and 3, respectively:
def f(a, b=2, c=3): print(a, b, c) # a required, b and c optional # from w ww .j a va 2s .c o m print( f(1) ) # Use defaults print( f(a=1) )
If we pass two values, only c gets its default, and with three values, no defaults are used:
def f(a, b=2, c=3): print(a, b, c) # a required, b and c optional print( f(1, 4) ) # Override defaults print( f(1, 4, 5) )
You can use both the keyword and default features.
def f(a, b=2, c=3): print(a, b, c) # a required, b and c optional # from www .j a va 2 s . com print( f(1, c=6) ) # Choose defaults
Here, a gets 1 by position, c gets 6 by keyword, and b, in between, defaults to 2.