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

def num( x = 5,  y = 25):
    sum = x + y
    print('Sum:', sum)
    
num(x = 8)
num(15, 22)
num()
Example of default arguments function in Python

Let’s run the code

Example of default arguments function in Python
Example of default arguments function

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

def 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')
Example of Keyword Argument

Let’s run the code

Example of Keyword Argument in Python
Example of Keyword Argument

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

def key( company_name): 
 print ("Company Name: ", company_name)
 
key("Basic Engineer")
Example of Required Argument

Let’s run the code

Example of Required Argument

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)

# *args arguments
def key(*argv):
	for arg in argv:
		print(arg)


key('Hello', 'Student', 'Basic', 'Engineer')
Example of *args (Non-Keyword Arguments)

Let’s run the code

Example of *args (Non-Keyword Arguments) in Python
Example of *args (Non-Keyword Arguments)

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')
Example of **kwargs (Keyword Arguments)

Let’s run the code

Example of  **kwargs (Keyword Arguments) in Python
Example of **kwargs (Keyword Arguments)

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


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *