In this tutorial, we will learn about the Function Arguments in Python. Its types, and how to use them with the help of examples.
Function is a Sub-program within program that perform specific operation within program. After performing an operation function returns a value which may the result of operation.
Arguments are the values passe inside the parenthesis of the function. A function can have any number of arguments separated by a comma. Python supports various types of arguments.
Types of Function Arguments in Python
There are four types of argument
- Default argument
- Keyword argument
- Required argument
- Variable-length argument
Default Argument
Default argument has a default value. It will use in the absence of Passing Values at the time of calling.
The Example of default arguments function
Example of default arguments function in Pythondef num( x = 5, y = 25): sum = x + y print('Sum:', sum) num(x = 8) num(15, 22) num()
Let’s run the code
Keyword Argument
Keyword argument is use to pass the value with name of variable so that we can pass values without bothering the sequence of parameters.
The Example of Keyword Argument
Example of Keyword Argumentdef display_info(student_name, father_name): print('Student Name:', student_name) print('Father Name:', father_name) display_info(father_name = 'Roshan Kumar', student_name = 'Anu Raj')
Let’s run the code
Required Argument
Required arguments are the mandatory arguments of a function. These argument values must be pass in correct number and order during function call.
The Example of Required Argument
Example of Required Argumentdef key( company_name): print ("Company Name: ", company_name) key("Basic Engineer")
Let’s run the code
Variable-length Argument
In this we can pass arguments in different numbers of arguments in different function call. It will handle all the arguments using pointer.
There are two special symbols:
- *args (Non-Keyword Arguments)
- **kwargs (Keyword Arguments)
Non Keyword Argument
The Example of *args (Non-Keyword Arguments)
Example of *args (Non-Keyword Arguments)# *args arguments def key(*argv): for arg in argv: print(arg) key('Hello', 'Student', 'Basic', 'Engineer')
Let’s run the code
Keyword Arguments
Example of **kwargs (Keyword Arguments)
Example of **kwargs (Keyword Arguments)# *kwargs arguments def key(**kwargs): for key, value in kwargs.items(): print("%s = %s" % (key, value)) # Driver code key(first='Hello', mid='Student', last='Basicengineer')
Let’s run the code
If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.
To know more about Python Wikipedia please click here .
Stay Connected Stay Safe, Thank you
0 Comments