The Wayback Machine - https://web.archive.org/web/20250317125156/https://www.geeksforgeeks.org/how-to-pass-optional-parameters-to-a-function-in-python/
Open In App

How to pass optional parameters to a function in Python?

Last Updated : 12 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In Python, functions can have optional parameters by assigning default values to some arguments. This allows users to call the function with or without those parameters, making the function more flexible. When an optional parameter is not provided, Python uses its default value. There are two primary ways to pass optional parameters:

  • Without using keyword arguments
  • By using keyword arguments

Both methods allow flexibility in function calls, but they have different rules regarding the order of parameters and argument passing.

Passing without using keyword arguments

When passing optional parameters without explicitly mentioning their names, certain rules must be followed:

  • The order of parameters must be maintained.
  • All required (non-optional) parameters must be provided.
  • The optional parameters can be either provided or omitted (in which case their default values are used).

Example 1: Using default values

Python
def fun(a, b=1098):
    return a + b

print(fun(2, 2))  # Passing both parameters
print(fun(1))     # 'b' takes the default value 1098

Output
4
1099

Explanation: fun(a, b=1098) takes two parameters a and b, where b has a default value of 1098. When both arguments are provided, the function returns their sum. If only a is passed, b takes its default value and their sum is returned.

Example 2: Default parameter with strings

Python
def fun(a, b="Geeks"):
    print(a + b)

fun('GeeksFor')        # Uses the default value of 'b'
fun('GeeksFor', "Hub") # Provides a different value for 'b'

Output
GeeksForGeeks
GeeksForHub

Explanation: fun(a, b=”Geeks”) takes two parameters a and b, where b defaults to “Geeks”. It prints the concatenation of a and b. If both arguments are provided, it prints their combined value otherwise, b takes its default value.

Passing with keyword arguments

Python allows function calls to explicitly specify parameter names when passing arguments. This has certain advantages:

  • The order of arguments does not need to be maintained.
  • Only required parameters must be passed , optional ones can be omitted.
  • The parameter names must match exactly as declared in the function.

Example 1: Using keyword arguments

Python
def fun(a,b="Geeks"):
    print(a + b)

# Passing arguments without maintaining order
fun(a='ForGeeks', b="Geeks")

Output
GeeksForGeeks

Explanation: fun(a, b=”Geeks”) takes two parameters a and b, where b defaults to “Geeks”. It prints their concatenation. Here, both arguments are explicitly passed using keywords, ensuring correct assignment.

Example 2: Keyword arguments with mixed data types

Python
def fun(a, b, c='geeks'):
    print(a, "type is", type(a))
    print(b, "type is", type(b))
    print(c, "type is", type(c))

fun(2, 'z', 2.0)  # All arguments passed explicitly

fun(2, 1)  # Default value of 'c' used

fun(c=2, b=3, a='geeks')  # Keyword arguments change order

Output

2 type is <class 'int'>
z type is <class 'str'>
2.0 type is <class 'float'>
2 type is <class 'int'>
1 type is <class 'int'>
geeks type is <class 'str'>
geeks type is <class 'str'>
3 type is <class 'int'>

Explanation: fun(a, b, c=’geeks’) takes three parameters, with c defaulting to ‘geeks’. It prints each argument along with its type. Arguments can be passed explicitly, use the default value or be reordered using keyword arguments.

Combining positional and keyword arguments

We can use both positional and keyword arguments in the same function call, but positional arguments must come before keyword arguments.

Example: Positional and Keyword Arguments Together

Python
def fun(a, b=2):
    if a < b:
        print("First smaller")
    elif a > b:
        print("Second smaller")
    else:
        print("Equal")

fun(1)        # Default 'b' used
fun(2, 1)     # Both parameters passed
fun(b=1, a=-1)  # Keyword arguments change order
fun(-1, b=0)  # Positional and keyword arguments combined

Output
First smaller
Second smaller
First smaller
First smaller

Explanation: fun(a, b=2) compares a and b, printing whether the first is smaller, the second is smaller or both are equal. It works with default, positional, keyword and mixed arguments.

How to pass optional parameters to a function in Python? – FAQs

How to pass optional parameters in Python function?

To pass optional parameters in a Python function, you can define default values for parameters in the function definition. This allows the function to be called with fewer arguments than defined.

How to give an optional parameter to a function?

An optional parameter is given to a function by assigning it a default value in the function definition. For example:

def example_function(param1, optional_param=None):
# Function body

In this case, optional_param is optional and defaults to None if not provided.

How to pass extra arguments to a function in Python?

You can pass extra arguments to a function using *args for positional arguments and **kwargs for keyword arguments. This allows the function to accept an arbitrary number of additional arguments.

How to skip positional arguments in Python?

Skipping positional arguments in a function call can be achieved by using keyword arguments. This way, you can specify only the arguments you want to pass, and the rest will use their default values.

def example_function(a, b=2, c=3):
# Function body

example_function(a=1, c=4) # b uses its default value of 2

What does args mean in Python?

*args in a function definition allows the function to accept a variable number of positional arguments. Inside the function, args is a tuple containing all the positional arguments passed. It is used when you want to create functions that can handle an arbitrary number of positional arguments.



Next Article

Similar Reads

three90RightbarBannerImg