How to call a function with argument list in Python?

The purpose of a function is to perform a specific task using code blocks. In programming, functions save time by eliminating unnecessary and excessive copying and pasting of code. Hence, a function will be of great use if there is a common action that needs to be performed in different places and often.

The only thing you'll need to do is update that specific function, if you want to make a change. As a result, you no longer have to copy and paste the same piece of code that is scattered throughout your program in order to find it. This follows the don't repeat yourself principle in software development. Whenever a function is called, the code inside runs. After the code has run, functions may return value to callers as arguments or defaults.

Defining a Function in Python

Python functions are typically created using the following syntax −

def function_name(parameters):
   function body

A function is defined using the “def” keyword followed by the function name and its parameters enclosed in parenthesis. One must remember that the names listed within these parentheses are known as parameters; whereas the values passed through these parameters are known as arguments.

Arguments are used to pass information to functions during the function call. Multiple arguments can be passed to a function by separating them with commas.

Calling a Function

Functions are called using the function name followed by its arguments enclosed in parenthesis. It can contain no arguments, two arguments or more than two arguments.

def my_func():
    print("Hello World")
def func(a):
my_func() #calling the function with no arguments
func(2) #calling the function with arguments


Hello World

Passing Multiple Arguments

A Python function can accept no arguments, a single argument, or multiple arguments. These multiple arguments can be passed to a function by separating them using commas; but in such cases, same number of parameters must be defined in the function definition.

However, if the arguments to be passed are in huge numbers, declaring each parameter becomes difficult. Hence, using the *args keyword, we can define one parameter and unpack it to pass multiple arguments making it less cumbersome. The unpacking operator * is used when no separate arguments are available.

The syntax for the function defined using the *args keyword is as follows −



In the following example, we are defining a function “func” and passing two lists list1 and list2 as its arguments using the *args keyword. Then, using the “in” operator, these lists are displayed iteratively.

def func(*args): # here, *args can be of any name and not just args
    for i in args:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
#function call passing two lists
func(list1, list2)


[1, 2, 3]
[4, 5, 6]


In the case of non-predetermined elements, this method is useful. The formal parameters for a Python function don't have to be predetermined because we can pass multiple elements. In the case of an unknown number of arguments, using *args will prevent the code from failing. If there are no predetermined elements in a list, you can use this method.

# using arguments
def func(*arguments):
    for i in arguments:
# function call
func("Hello", "world")



Updated on: 22-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started