How to call a function with argument list in Python?

The purpose of a function is to perform a specific task using code blocks. Functions save time by eliminating unnecessary copying and pasting of code. If you need to make a change, you only update the function in one place rather than searching through your entire program. This follows the DRY (Don't Repeat Yourself) principle in software development.

Defining a Function in Python

Python functions are 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 parentheses. The names listed within these parentheses are known as parameters, whereas the values passed through them during a function call are known as arguments.

Calling a Function with Arguments

Functions are called using the function name followed by arguments enclosed in parentheses. A function can accept no arguments, one argument, or multiple arguments.

Example: Function with and without Arguments

In the following example, my_func() takes no arguments, while func(a) takes one argument ?

def my_func():
    print("Hello World")

def func(a):
    print(a + a)

my_func()
func(2)

The output of the above code is ?

Hello World
4

Passing an Argument List Using *args

A Python function can accept multiple arguments separated by commas. However, if the number of arguments is large or unknown, declaring each parameter individually becomes difficult. The *args syntax lets you define a single parameter that accepts a variable number of arguments. The unpacking operator * collects all positional arguments into a tuple −

def function_name(*args):
    function body

Example: Passing Multiple Lists Using *args

In the following example, we pass two lists as arguments using *args and iterate over them ?

def func(*args):
    for i in args:
        print(i)

list1 = [1, 2, 3]
list2 = [4, 5, 6]

func(list1, list2)

The output of the above code is ?

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

Example: Passing Multiple Strings Using *args

In this example, the function accepts a variable number of string arguments and prints each one ?

def func(*arguments):
    for i in arguments:
        print(i)

func("Hello", "world")

The output of the above code is ?

Hello
world

Example: Unpacking an Existing List as Arguments

If you already have a list and want to pass its elements as separate arguments to a function, use the * operator during the function call ?

def add(a, b, c):
    print("Sum:", a + b + c)

numbers = [10, 20, 30]
add(*numbers)

The output of the above code is ?

Sum: 60

Here, *numbers unpacks the list [10, 20, 30] into three separate arguments a=10, b=20, c=30.

Conclusion

Use *args in a function definition to accept a variable number of arguments, and use the * operator in a function call to unpack an existing list or tuple into individual arguments.

Updated on: 2026-03-13T21:10:52+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements