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 required arguments of a function in python?

I have this python code.

def fooArg(str, num):
            print str, num
fooArg('Hi', 15)
fooArg('Hello')

When this code is run, I get an error for the second function call with a single argument. In this context, what are required arguments of a function in python?


1 Answer
Rajendra Dharmkar

Required arguments are the mandatory arguments of a function. These argument values must be passed in correct number and order during function call. 

If you run the given code you get the following output

Hi 15
Traceback (most recent call last):
  File "requiredarg1.py", line 4, in <module>
    requiredArg('Hello')
TypeError: requiredArg() takes exactly 2 arguments (1 given)

EXPLANATION

In the output above for the first function call with two required arguments we get the output as Hi 15. But for the second function call with only one argument, we get a TypeError saying the function takes exactly 2 arguments. This shows the importance of required arguments and their mandatory nature

Advertisements

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