Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

What are default arguments in python?

Does python allow function arguments to have default values?

Does the argument gets its default value if the function is called without the argument?


1 Answer
Manogna

Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default value

Default arguments:

Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during function call. The default value is assigned by using assignment (=) operator. Below is a typical syntax for default argument. Here, foo parameter has a default value Hi!

def defaultArg(name, foo='Come here!'):
    print name,foo
defaultArg('Joe')

OUTPUT

Joe Come here!

We see that in the above code there is one required argument and one default one in the declaration. In the output we see that both the arguments are printed even though only one argument was passed in the function call. The default argument is passed automatically and appears in the output of the function call.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.