Keep your place in this quest

Log in or sign up for free to subscribe, follow lesson progress, and access more learning content.

By now, you’ve probably noticed that some code patterns repeat.
What if you could wrap that code into a single package, give it a name, and run it whenever you want without rewriting it?

That’s exactly what functions are for.

You can think of a function as a mini-program living inside your program.
It has a name, can take input, does some work, and (optionally) gives something back.


Why Use Functions?

Functions are one of the most powerful tools in programming because they:

  • Avoid repetition — Write once, use anywhere.
  • Organize your code — Break big problems into smaller, manageable pieces.
  • Make testing easier — You can check one piece of logic without running your whole program.

TIP: Functions are like kitchen appliances. You could mix dough by hand every time, but a mixer does it faster, more consistently, and you can reuse it whenever you need.

Defining Your First Function

In Python, you create (or define) a function with the def keyword, followed by the function name, parentheses for any inputs (called parameters), and a colon.
Then, you indent the code that belongs to the function.

Example:

def square(number):
    return number * number

print(square(4))  # 16

Here’s what’s happening:

  1. We define square, which takes one parameter: number.
  2. Inside, we multiply number by itself and return the result.
  3. We call square(4), which replaces number with 4.
  4. The function returns 16, which print() displays.

IMPORTANT!: The return statement sends a value back to where the function was called. If you forget it, the function will return None. You will learn in the future that not every function returns something, maybe you just want it to do a few actions and that's it.

Functions With Multiple Parameters

Functions can take as many inputs as you need. These values are placed inside the parentheses, separated by commas.

Example:

def add(num1, num2):
    return num1 + num2

result = add(5, 6)
print(result)  # 11

Here, num1 becomes 5 and num2 becomes 6 when we call the function. Inside, they’re added together and the sum is returned.

Note: The names num1 and num2 only exist inside the function — they’re local variables.

Default Parameter Values

Sometimes you want a parameter to have a default value so the user doesn’t always have to provide it.

Example:

def greet(name, greeting="Hello"):
    print(f"{greeting}, {name}!")

greet("John")                   # Uses default greeting
greet("Maria", "Good evening")  # Uses custom greeting

If you don’t pass a greeting, it defaults to "Hello". If you do, it overrides the default.

TIP: Default values must come after all required parameters in the function definition.

Wrapping Up

Functions are your toolbox for building bigger, cleaner, and smarter programs. You can call them as many times as you want, pass different inputs, and expect them to handle the job consistently.

In the next lesson, we’ll explore recursion — a special kind of function that calls itself. It might sound strange, but it opens the door to elegant solutions for certain problems.