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 will 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.

Example: Function with and without arguments

In the following example, my_func() is a function with no arguments, while func(a) takes one argument. Both are called below -

def my_func():
    print("Hello World")
def func(a):
    print(a+a)

my_func() # calling the function with no arguments
func(2)   # calling the function with arguments

Following is the output obtained -

Hello World
4

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 -

function_name(*args)

Example: Passing multiple lists using *args

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:
        print(i)

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

# function call passing two lists
func(list1, list2)

We get the output as shown below -

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

Example: Passing multiple strings using *args

In this example, the function func accepts a variable number of string arguments using *args and prints them. This method is useful when the number of inputs is not predetermined -

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

# function call
func("Hello", "world")

The output generated is as follows -

Hello
world
Updated on: 2025-05-14T17:09:34+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements