Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
community creator

What are function arguments?

KotiNeha

Function

A function in Python is a block of reusable code that will be executed when it is called. We can call a function any number of times. Arguments are a type of information or value that are specified within parentheses and can be passed to a function.

Functions can have zero, one, or more arguments. Arguments are optional in Python.

Types of arguments

There are four types of function arguments:

  1. Required or Positional Arguments
  2. Default arguments
  3. Keyword arguments
  4. Variable-length arguments

Required or positional arguments

Required arguments must be passed in the correct positional order. The number of required arguments in the function call and function definition must be the same.

def printing(name, age):
    print("My name is:", name)
    print("My age is:", age)
    
# main code
printing("neha", 20)
# arguments will be passed to the function based on the order
printing(20, "neha")

If we pass more or less required arguments than those in the function definition, we get an error. Required arguments must be given before the default, keyword, and variable-length arguments.

def printing(name, age):
    print("My name is:", name)
    print("My age is:", age)
    
# main code
printing("neha", 20, 10)
# the code below also gives an error if we pass less no of arguments
printing("neha")

Default arguments

Default arguments are those arguments that are only initialized in the function definition. These arguments are optional in the function call. If any value is passed in the functional call, that value is taken; otherwise, the value initialized during the function call is taken.

def printing(name, age = 10):
    print("My name is:", name)
    print("My age is:", age)
    
# main code
print("When default argument not passed age = 10 is taken")
printing("neha")
print("When default argument is passed age = 29 is taken")
printing("neha", 29)
def printing(name = "neha", age = 10):
    print("My name is:", name)
    print("My age is:", age)
    
# function call 
printing()
printing("riya")

In the function definition, required arguments must be defined before the default arguments; otherwise, we will get an error.

# non-default argument follows default argument in the header
def printing(name, age = 10, address):
    print("My name is:", name)
    print("My age is:", age)
    print("My address is:", address)
    
# function call    
printing("riya", 20, "hyderabad")

Keyword arguments

Keyword arguments are related to the function calls. We can give keyword arguments in any order as they will be identified by the parameter names and be passed according to the Python interpreter’s function.

def printing(name, age = 10):
    print("My name is:", name)
    print("My age is:", age)
    
# function call
printing(age = 29, name = "neha")
printing(name = "neha")

Keyword arguments and default arguments must be passed after required arguments. In the example below, name is the required argument, age is the default argument, and address="chandanagar" is a keyword argument. Since “neha” is passed after age and address, we get an error.

def printing(name, address, age = 10):
    print("My name is :", name)
    print("My age is:", age)
    print("My address is:", address)
    
# function call
printing(age = 29, address = "chandanagar", "neha")

Variable-length arguments

In some projects, we may not know the number of arguments to be passed while coding. In such a case, we can use these variable-length arguments. We can pass any number of arguments according to our requirements.
We have two kinds of variable-length arguments:

a) *args
b) **kwargs

*args

*args is a tuple datatype argument that holds the values of all non-keyword variable arguments. The values passed to this argument are optional.

def printing(*names):
    for i in names:
        print(i)
    print(type(names))
        
# function call
print("passing 3 arguments")
printing("neha", "riya", "ravi")
print("passing 5 arguments")
printing("neha", "riya", "ravi", "surya", "kaushika")
def printing(age, address = "Hyderabad", *names):
    print("age = ", age)
    print("address = ", address)
    for i in names:
        print(i)
    print(type(names))
        
#function call
printing(20, "chandanagar", "neha", "riya", "ravi")
print("****another example****")
printing(20, "chandanagar", "neha", "riya", "ravi")

**kwargs

**kwargs is a dictionary datatype argument that holds the values of all key-value pair arguments. Values passed to this argument are optional.

def printing(age, address, *names, **kwargs):
    print("age = ", age)
    print("address = ", address)
    print("names = ", names)
    print("kwargs = ", kwargs)
#function call
printing(20, "chandanagar")
def printing(age, address, *names, **kwargs):
    print("age = ", age)
    print("address = ", address)
    print("names = ", names)
    print("kwargs = ", kwargs)
#function call
printing(20, "chandanagar", "neha", "riya", "ravi", fruit = "orange", Number = 78, Vegetable = "brinjal")
def printing(age, address = "Hyderabad", *names, **kwargs):
    print("age = ", age)
    print("address = ", address)
    print("names = ", names)
    print("kwargs = ", kwargs)
#main code
printing(20, "neha", "riya", "ravi", fruit = "orange", Number = 78, Vegetable = "brinjal")

Important points

  1. The order and number of required arguments in a function call must match the order and number of required arguments in the function definition.

  2. Default arguments must be given after the required arguments.

  3. Keyword arguments must be given after the required arguments in the function call.

  4. Default arguments may or may not be given in the function call.

  5. Keyword arguments can be given in any order in the function call.

  6. *args is of tuple datatype.

  7. **kwargs is of dictionary datatype.

RELATED TAGS

python
community creator
RELATED COURSES

View all Courses

Keep Exploring