Python Function Arguments (With Examples)

In Python, you can define a generic function that accepts arguments or parameters to perform specific tasks. Similarly, built-in functions also require arguments to function properly.

For example, you’ve seen the print() function, where a string like “Hello World” is passed as an argument to display that message.

Function Arguments

When defining and calling functions, providing the correct arguments is crucial. In this article, we’ll explore the complications that can arise if proper parameters are not provided, along with different types of function arguments.

Example:

				# Define Python function
def Name(FirstName, LastName):
    print("First Name:", FirstName)
    print("Last Name:", LastName)

# Call Python function with arguments
Name("Mayur", "Jadhav")

			
Python Function Arguments

In this example, both required arguments are provided when calling the Name() function. But if you provide only one argument, or none at all, you’ll encounter an error.

For example if you only pass one argument as:

Name("Mayur")

You will get the following error: TypeError: Name() missing 1 required positional argument: ‘LastName’.

In case if you don’t pass any argument at all then you’ll get this error: TypeError: Name() missing 2 required positional arguments: ‘FirstName’ and ‘LastName’.

Types of Function Arguments in Python

  1. Required Arguments
  2. Default Arguments
  3. Keyword Arguments
  4. Variable-Length (Arbitrary) Arguments

1. Python Required Arguments

In this type of argument, you must provide the exact number of required arguments with their correct data types for the function to work properly.

For example, consider the addition() function, which expects two numeric arguments. Different test cases show what happens when incorrect or missing arguments are provided:

				# Define function
def addition(a, b):
    result = a + b
    print("Addition result:", result)

# Test Cases
addition()  # Error: Missing arguments 'a' and 'b'

addition(10, "Character_String")  # Error: Cannot add int and str

addition(10, 20)  # Output: Addition result: 30

			
Python Function Arguments - adding two numbers

2. Python Default Arguments

In default arguments, you can assign default values to parameters. If you pass fewer arguments than expected, the default values will be used.

For example:

				# Define function with default argument
def addition(a, b=0):
    result = a + b
    print("Addition result:", result)

addition(10)  # Output: Addition result: 10

addition(10, 20)  # Output: Addition result: 30

			
Python Function with default Arguments

In the first case, the default value of b (0) is used. In the second case, the provided value (20) overrides the default.

3. Python Keyword Arguments

Keyword arguments allow you to assign values to specific parameters by name, regardless of their position.

For example:

				def addition(a, b):
    result = a + b
    print("Addition result:", result)

addition(a=10, b=20)  # Output: Addition result: 30
addition(b=20, a=10)  # Output: Addition result: 30

			
Python Function Keyword Arguments - adding two numbers

You can mix positional and keyword arguments, but positional arguments must come first. 

Also, assigning a value to the same variable twice (once positionally and once with a keyword) will cause an error:

addition(10, a=20)  # Error: Multiple values for argument 'a'

4. Variable-Length or Arbitrary Arguments

If you’re unsure how many arguments will be passed, you can use arbitrary arguments by defining a variable with an asterisk (*). These arguments are stored as a tuple.

For example:

				def addition(*a):
    sum = 0
    for var in a:
        sum += var
    print("Addition result:", sum)

# Call function with multiple arguments
addition(10, 20, 30, 15, 20)  # Output: Addition result: 95

			
Python Variable-Length or Arbitrary Arguments

You can also combine default and arbitrary arguments:

				def addition(b, *a):
    sum = 0
    for var in a:
        sum += var
    print("Addition result:", sum)
    print("Value of variable b:", b)

addition(10, 20, 30, 15, 20)  
# Output: Addition result: 85
# Output: Value of variable b: 10

			

In this example, the value 10 is assigned to b, and the remaining values are assigned to a, which is processed using a loop.

Summary

In Python, functions can take various types of arguments. Required arguments must be provided exactly as expected, both in number and data type.

Default arguments allow you to set default values, which can be overridden by passing new values.

Keyword arguments let you specify values by name, offering flexibility in their order.

Finally, variable-length (arbitrary) arguments handle an unknown number of inputs by collecting them into a tuple. Understanding these argument types helps in writing more flexible and error-free Python functions.

Leave a Comment