How to pass multiple arguments to function ?
Last Updated :
03 Jul, 2024
A Routine is a named group of instructions performing some tasks. A routine can always be invoked as well as called multiple times as required in a given program.

When the routine stops, the execution immediately returns to the stage from which the routine was called. Such routines may be predefined in the programming language or designed or implemented by the programmer. A Function is the Python version of the routine in a program. Some functions are designed to return values, while others are designed for other purposes.
We pass arguments in a function, we can pass no arguments at all, single arguments or multiple arguments to a function and can call the function multiple times.
Example:
Python
# no argument is passed
# function definition
def displayMessage():
print("Geeks for Geeks")
# function call
displayMessage()
Output:
Geeks for Geeks
In the above program, the displayMessage() function is called without passing any arguments to it.
Python
# single argument is passed
# function definition
def displayMessage(msg):
print("Hello "+msg+" !")
msg = "R2J"
# function call
displayMessage(msg)
Output:
Hello R2J !
In the above program, the displayMessage() function is called by passing an argument to it. A formal argument is an argument that is present in the function definition. An actual argument is an argument, which is present in the function call.
Passing multiple arguments to a function in Python:
- We can pass multiple arguments to a python function by predetermining the formal parameters in the function definition.
Python
# multiple arguments are passed
# function definition
def displayMessage(argument1, argument2, argument3):
print(argument1+" "+argument2+" "+argument3)
# function call
displayMessage("Geeks", "4", "Geeks")
Geeks 4 Geeks
- In the above program, multiple arguments are passed to the displayMessage() function in which the number of arguments to be passed was fixed.
- We can pass multiple arguments to a python function without predetermining the formal parameters using the below syntax:
def functionName(*argument)
- The * symbol is used to pass a variable number of arguments to a function. Typically, this syntax is used to avoid the code failing when we don't know how many arguments will be sent to the function.
Python
# variable number of non keyword arguments passed
# function definition
def calculateTotalSum(*arguments):
totalSum = 0
for number in arguments:
totalSum += number
print(totalSum)
# function call
calculateTotalSum(5, 4, 3, 2, 1)
15
- In the above program, the variable number of arguments are passed to the displayMessage() function in which the number of arguments to be passed is not predetermined. (This syntax is only used to pass non-keyword arguments to the function.)
- We can pass multiple keyword arguments to a python function without predetermining the formal parameters using the below syntax:
def functionName(**argument)
- The ** symbol is used before an argument to pass a keyword argument dictionary to a function, this syntax used to successfully run the code when we don't know how many keyword arguments will be sent to the function.
Python
# variable number of keyword arguments passed
# function definition
def displayArgument(**arguments):
for arg in arguments.items():
print(arg)
# function call
displayArgument(argument1 ="Geeks", argument2 = 4,
argument3 ="Geeks")
('argument2', 4)
('argument3', 'Geeks')
('argument1', 'Geeks')
- In the above program, variable number of keyword arguments are passed to the displayArgument() function.
Here is a program to illustrate all the above cases to pass multiple arguments in a function.
Python
# single argument, non keyword argument
# and keyword argument are passed
# function definition
def displayArguments(argument1, *argument2, **argument3):
# displaying predetermined argument
print(argument1)
# displaying non keyword arguments
for arg in argument2:
print(arg)
# displaying non keyword arguments
for arg in argument3.items():
print(arg)
arg1 = "Welcome"
arg3 = "Geeks"
# function call
displayArguments(arg1, "to", arg3, agr4 = 4,
arg5 ="Geeks !")
Output:
Welcome
to
Geeks
('agr4', 4)
('arg5', 'Geeks!')
The above program illustrates the use of the variable number of both non-keyword arguments and keyword arguments as well as a non-asterisk argument in a function. The non-asterisk argument is always used before the single asterisk argument and the single asterisk argument is always used before the double-asterisk argument in a function definition.
Similar Reads
Python - pass multiple arguments to map function
The map() function is a built-in function in Python, which applies a given function to each item of iterable (like list, tuple etc.) and returns a list of results or map object. Syntax : map( function, iterable ) Parameters : function: The function which is going to execute for each iterableiterable
3 min read
How to Call Multiple Functions in Python
In Python, calling multiple functions is a common practice, especially when building modular, organized and maintainable code. In this article, weâll explore various ways we can call multiple functions in Python.The most straightforward way to call multiple functions is by executing them one after a
3 min read
Passing function as an argument in Python
In Python, functions are first-class objects meaning they can be assigned to variables, passed as arguments and returned from other functions. This enables higher-order functions, decorators and lambda expressions. By passing a function as an argument, we can modify a functionâs behavior dynamically
5 min read
How to Pass Arguments to Tkinter Button Command?
When a user hits the button on the Tkinter Button widget, the command option is activated. In some situations, it's necessary to supply parameters to the connected command function. In this case, the procedures for both approaches are identical; the only thing that has to vary is the order in which
2 min read
Python function arguments
In Python, function arguments are the inputs we provide to a function when we call it. Using arguments makes our functions flexible and reusable, allowing them to handle different inputs without altering the code itself. Python offers several ways to use arguments, each designed for specific scenari
3 min read
Tuple as function arguments in Python
Tuples have many applications in all the domains of Python programming. They are immutable and hence are important containers to ensure read-only access, or keeping elements persistent for more time. Usually, they can be used to pass to functions and can have different kinds of behavior. Different c
2 min read
How to bind arguments to given values in Python functions?
In Python, binding arguments to specific values can be a powerful tool, allowing you to set default values for function parameters, create specialized versions of functions, or partially apply a function to a set of arguments. This technique is commonly known as "partial function application" and ca
3 min read
Passing Dictionary as Arguments to Function - Python
Passing a dictionary as an argument to a function in Python allows you to work with structured data in a more flexible and efficient manner. For example, given a dictionary d = {"name": "Alice", "age": 30}, you can pass it to a function and access its values in a structured way. Let's explore the mo
4 min read
Pass a List to a Function in Python
In Python, we can pass a list to a function, allowing to access or update the list's items. This makes the function more versatile and allows us to work with the list in many ways.Passing list by Reference When we pass a list to a function by reference, it refers to the original list. If we make any
2 min read
How to pass an array to a function in Python
In this article, we will discuss how an array or list can be passed to a function as a parameter in Python. Pass an array to a function in Python So for instance, if we have thousands of values stored in an array and we want to perform the manipulation of those values in a specific function, that is
4 min read