Python Function Examples with Parameters

I will explain everything about Python function examples with parameters in today’s tutorial. Parameters are useful in various Python functions, and as a developer, you should know about them.

To define a function in Python with parameters, you use the def keyword followed by the function name and parentheses containing the parameters. For example, def greet(name): defines a function greet that takes a single parameter name. Inside the function, you can use this parameter to perform operations, such as returning a greeting message: return f"Hello, {name}!". When you call greet("Alice"), it outputs “Hello, Alice!”. This method allows you to create dynamic and reusable code blocks.

What is a Python Function?

In Python, a function is a block of reusable code designed to perform a specific task. Functions help break our program into smaller, modular chunks, making it easier to manage and understand. The basic syntax for defining a function in Python is:

def function_name(parameters):
    # function body
    return result

What are the Parameters of a Python Function?

Parameters are the variables listed inside the parentheses in the function definition in Python. They allow us to pass information into functions, making them more flexible and dynamic.

Python Function Examples with Parameters

Now, let me show you various examples of Python functions that contain parameters. Let us start from a basic example.

Example 1: Basic Function with a Single Parameter

Let’s start with a simple example. Suppose we want to create a function that greets a user by their name. For this, we can create a function that takes one parameter as a name. Here is the complete Python code.

def greet(name):
    return f"Hello, {name}!"

# Calling the function
print(greet("Alice"))

In this example, name is the parameter. When we call greet("Alice"), it returns “Hello, Alice!”.

I executed the above Python code, and you can see the output in the screenshot below:

python function example with parameters

Check out How to Call a Function in Python?

Example 2: Function with Multiple Parameters

Python Functions can also accept multiple parameters. Here’s an example of a function that adds two numbers:

def add(a, b):
    return a + b

# Calling the function
print(add(3, 5))

Here, a and b are parameters. When we call add(3, 5), it returns 8.

Here is the exact output in the screenshot below.

python function multiple parameters

Example 3: Default Parameter Values

Sometimes, we might want to provide default values for parameters in a Python function. This can be useful when we don’t want to specify all arguments every time we call the function.

def greet(name, message="Hello"):
    return f"{message}, {name}!"

# Calling the function with both parameters
print(greet("Bob", "Good morning"))

# Calling the function with only the name parameter
print(greet("Charlie"))

In this case, message has a default value of “Hello”. If we don’t provide a second argument, the function uses the default value.

You can see the output in the screenshot below:

python function examples with parameters

Check out Call a Variable from Another Function in Python

Example 4: Keyword Arguments

Python also allows us to use keyword arguments in a Python function, which can make our function calls more readable. Below is an example and the complete code.

def describe_pet(pet_name, animal_type="dog"):
    return f"I have a {animal_type} named {pet_name}."

# Using keyword arguments
print(describe_pet(animal_type="hamster", pet_name="Harry"))

By using keyword arguments, we can specify the values for parameters by name, making our code more explicit.

Example 5: Variable-Length Arguments

Sometimes, we might not know in advance how many arguments we need to pass to a function in Python. Python provides a way to handle this using *args and **kwargs.

Using *args for Non-Keyword Arguments

Here is an example.

def make_pizza(*toppings):
    return f"Making a pizza with the following toppings: {', '.join(toppings)}"

# Calling the function with multiple arguments
print(make_pizza("pepperoni", "mushrooms", "green peppers"))

Using **kwargs for Keyword Arguments

Here is an example and the complete code.

def build_profile(first, last, **user_info):
    profile = {"first_name": first, "last_name": last}
    profile.update(user_info)
    return profile

# Calling the function with multiple keyword arguments
print(build_profile("Albert", "Einstein", location="Princeton", field="Physics"))

Conclusion

I hope you now know the parameters of a Python function. Here, I have shown some Python function examples with parameters. Here, I covered basic functions, multiple parameters, default values, keyword arguments, and variable-length arguments. Does this help you?

You may also like:

Leave a Comment