Function definition with required arguments:
For required arguments, the arguments are passed to a function in correct positional order. Also, the number of arguments in the function call is exactly matched with the number of arguments specified in the function definition.
Example 1:
def show(): print("Welcome") show("Hi")
Output:
Traceback (most recent call last): File "C:/Users/User/AppData/Local/Programs/Python/Python38-32/qqqqqqqq.py", line 3, in <module> show("Hi") TypeError: show() takes 0 positional arguments but 1 was given
Example 2:
def show(str): print("Welcome") show()
Output:
Traceback (most recent call last): File "C:/Users/User/AppData/Local/Programs/Python/Python38-32/qqqqqqqq.py", line 3, in <module> show() TypeError: show() missing 1 required positional argument: 'str'
Example 3:
def show(str): print("Welcome") show("Hi")
Output:
Welcome
Function definition with keyword arguments:
When we call a function with some values, the values are assigned to the arguments based on their position. Python also allows functions to be called using keyword arguments in which the order of the arguments can be changed. The values are not assigned to arguments according to their position but based on their name (or keyword).
Keyword arguments, when used in function calls, helps the function to identify the arguments by the parameter name.
Actually, for keyword arguments, we can skip arguments and can change the order of parameters in the function call.
Keyword arguments make the program code easier to read and understand.
Example 4:
def show(name,age,salary): print("Name:",name) print("Age:",age) print("Salary:",salary) p="John" q=25 r=90000 show(salary=r,name=p,age=q)
Output:
Name: John Age: 25 Salary: 90000
Function definition with default arguments:
Python allows us to specify function arguments that can have default values. This means that a function can be called with fewer arguments than it is defined to have. That is, if the function accepts three parameters, but the function call provides only two arguments, then the third parameter is assigned the default value. The default value to an argument is provided by using the assignment operator (=). We can specify a default value for one or more arguments.
A default argument assumes a default value if a value is not provided in the function call for that argument.
Example 5:
def display(name,course="B.Tech"): print("Name:",name) print("Course:",course) display(course="M.Tech",name="Smith") # keyword arguments display(name="Raina") # default argument for course
Output:
Name: Smith Course: M.Tech Name: Raina Course: B.Tech
Function definition with variable-length argument:
In some situations, it is not known in advance how many arguments are passed to a function. In such a case, Python allows us to make function calls with an arbitrary number of arguments.
When we use arbitrary arguments or variable-length arguments, then the function definition uses an asterisk (*) before the parameter name.
Actually, the arbitrary number of arguments to the function basically forms a tuple, and inside the called function, for loop is used to access the arguments.
The variable-length argument, if present in the function definition, is the last in the list of formal parameters.
Any formal parameter written after the variable-length arguments must be keyword-only arguments.
The syntax for a function using variable arguments is given below:
def functionname([arg1,arg2,…] *var_args_tuple): function statements return [expression]
Example 6:
def display(name,*fav_subjects): print("\n",name," likes to read ") for subject in fav_subjects: print(subject) display("C programming","C++ programming","Java programming") display("PHP programming","Datastructure","Python") display("Digittal Electronics")
Output:
C programming likes to read C++ programming Java programming PHP programming likes to read Datastructure Python Digittal Electronics likes to read
Leave a comment