Python Function Parameters: A Beginner’s Guide

Have you ever written a Python function, only to find it does the exact same thing every single time you call it? You might have a greet() function that always prints “Hello learners” or, as in our previous lesson, an even_or_odd() function that only checks the number 5. This rigidity makes functions feel limited. What if you could give your functions custom instructions each time you use them?

The solution lies in one of Python’s most powerful features: function parameters. They are the key to transforming your functions from one-trick ponies into versatile, reusable blocks of code. By the end of this guide, you will understand exactly how to define functions that accept input, making your code more dynamic and powerful.

What Are Python Function Parameters?

In simple terms, parameters are like placeholders for information you promise to give to a function. They are variables that you list inside the parentheses when you define a function.

Think of a function as a custom coffee machine. A machine without parameters can only brew one pre-set drink. But a machine with parameters lets you choose the coffee type, milk, and sugar each time, giving you a personalized result. Parameters give your functions this same level of customization.

You’ve already been using a function with parameters all along: the print() function. Every time you call print("Hello") or print(42), you are passing a different piece of data, or argument, to the print function’s parameter.

# The print() function has parameters! We pass different arguments to it.
print("This is an even number")  # The string is the argument
print("This is an odd number")   # A different string is the argument

The Problem: Functions Without Parameters

Let’s look at the problem from the video. We have a function that is hard-coded to check only one number.

def even_or_odd():
    number = 5  # The number is fixed inside the function
    if number % 2 == 0:
        print("This is an even number")
    else:
        print("This is an odd number")

# Calling the function multiple times is pointless.
even_or_odd() # Always checks 5
even_or_odd() # Always checks 5
even_or_odd() # Always checks 5

Output:

This is an odd number
This is an odd number
This is an odd number

As you can see, this isn’t useful. We want to check different numbers like 9, 10, or 12, but the function ignores our needs and stubbornly checks the number 5.

The Solution: Defining Functions with Parameters

The fix is simple: we remove the hard-coded number and instead add a parameter inside the function’s parentheses. Let’s call this parameter num. This tells Python, “When this function is called, it will be given one piece of data, and I want to refer to that data inside the function as num.”

How to Add a Parameter

Here’s how we rewrite the even_or_odd function:

# 'num' is the parameter. It's a placeholder for the actual value we will provide.
def even_or_odd(num):
    if num % 2 == 0:
        print(f"{num} is an even number")
    else:
        print(f"{num} is an odd number")

Now, our function is ready to accept input! The logic inside the function uses the num variable, which will contain whatever value we pass to it.

Passing Arguments to Your Function

When you call a function that has parameters, you must provide the actual values for those parameters. These values are called arguments.

# Calling the function with different arguments
even_or_odd(5)   # The argument is 5
even_or_odd(9)   # The argument is 9
even_or_odd(12)  # The argument is 12

Output:

5 is an odd number
9 is an odd number
12 is an even number

Hooray! The function is now dynamic. When we call even_or_odd(5), the value 5 is passed to the function and assigned to the parameter num. The function then checks 5 % 2. The same process happens for 9 and 12.

Understanding the Error: A Quick Lesson

What happens if you try to pass an argument to a function that isn’t defined to accept any parameters? You’ll get a clear error message, just like in the video.

def greet():
    print("Hello learners")

greet("Alice")  # Trying to pass an argument

Output:

TypeError: greet() takes 0 positional arguments but 1 was given

This error is Python’s way of saying: “You told me the greet() function needs no input, but you just tried to give it one (the string “Alice”).” To fix this, we would need to define greet with a parameter.

Your Turn to Practice: A Custom Greeting Function

Let’s solidify your understanding with another example. The video suggested updating a greet() function to accept a person’s name. Here’s how you can do it:

# Define the function with a 'name' parameter
def greet(name):
    print(f"Hello, {name}! Welcome to Python!")

# Call the function with different arguments
greet("Alice")
greet("Bob")
greet("Charlie")

Output:

Hello, Alice! Welcome to Python!
Hello, Bob! Welcome to Python!
Hello, Charlie! Welcome to Python!

See how much more personal and reusable this function is? The name parameter acts as a variable that gets filled with “Alice”, “Bob”, or “Charlie” each time the function is called.

Ready to Go from Basics to Pro?

You’ve just taken a significant step in your Python journey by learning how to make your functions interactive and reusable. Mastering parameters is a foundational skill that unlocks the true potential of functions, but there’s so much more to explore—like using multiple parameters, different types of arguments, and having functions return values instead of just printing.

If you’re serious about mastering Python, check out our pre-recorded python comprehensive video course.

Conclusion

In this guide, you’ve learned that function parameters are the essential tool for passing data into your functions, making them flexible and powerful. You saw how to define a function with a parameter, how to pass arguments when you call the function, and how to fix a common error when arguments and parameters don’t match. Remember, practice is key. Try modifying your old functions to use parameters and see how much more you can do with them. Keep coding, and you’ll be building complex programs in no time!

Common Questions About Python Function Parameters

What’s the difference between a parameter and an argument?
parameter is the variable listed inside the parentheses in the function definition. An argument is the actual value you pass to the function when you call it. In def greet(name):name is the parameter. In greet("Alice")"Alice" is the argument.

Can a function have more than one parameter?
Absolutely! You can define a function with multiple parameters by separating them with commas. For example, def introduce(name, age): would be a function that expects two pieces of data when it is called.

What happens if I don’t pass an argument to a function that requires one?
You will get a TypeError stating that the function is missing a required positional argument. Python expects you to provide an argument for every parameter you define.