Python Functions Explained Like a Recipe
Think about how you make a cup of coffee. You don't recreate the process every morning; you follow the same steps: boil water, add coffee, sugar, and that's it. A Python function works just like that: it's a set of steps that you write once and reuse whenever you need them.
What a Function Actually Is
A function is a named, reusable block of code that runs when you call it. Instead of writing the same instructions over and over again, you write them once and reuse them.
Nothing will happen for now. Simply writing down the recipe doesn't make the coffee, you have to follow the steps.
defis the keyword used to define a functionmake_coffeeis the name of the function
(Note: You'll learn more about naming functions later in this tutorial.)
Calling a Function
To actually run the steps, you call the function by its name followed by parentheses.
Every time you call make_coffee(), Python runs the whole recipe from top to bottom, just like following the same steps every morning.
Returning a Value
So far, our function only prints things. Most real functions need to hand something back to whoever called them, the same way a barista hands you the finished cup instead of just shouting "done!"
def make_coffee(): return "A hot cup of coffee" result = make_coffee()print(result)Here what happens:
make_coffee()creates a cup of coffee.returnsends "A hot cup of coffee" back.- We store that returned value in result.
print(result)displays it.
The return keyword sends a value out of the function and ends it immediately. Anything written after return inside the function never runs.
Why Bother With Functions at All
Without functions, a program that needs to make coffee three times would repeat the same four lines three times. With a function, you write the recipe once and just call it whenever you need coffee:
This is the core benefit: write once, reuse everywhere. It also means if you improve the recipe later, adding sugar for example, you only change it in one place.
A Quick Note on Naming
- Function names must begin with a letter or an underscore.
- Function names can only contain letters, numbers, and underscores.
- Function names are case-sensitive (
make_coffeeandmake_Coffeeare different)
Just as the title of a recipe describes the dish, the name of a function should also describe what it does. make_coffee() is clear. do_stuff() is not.
What's Next
Now you understand the basics: a function is a reusable recipe that can return a result. The next tutorial in this series explains how to customize that recipe with parameters and arguments, like asking for "coffee with two sugars" instead of the default.
Python Functions 7-Part Masterclass:
- Part 1: Python Functions Explained Like a Recipe (Beginner's Guide) (Current)
- Part 2: Python Function Parameters and Arguments (Like Ordering Food)
- Part 3: *args and **kwargs in Python Explained Like a Potluck Party
- Part 4: Python Lambda Functions Explained Like a Sticky Note
- Part 5: Python Variable Scope Explained Like an Office Building
- Part 6: Python Recursion Explained Like Russian Nesting Dolls
- Part 7: Python Closures and Decorators Explained Like Gift Wrapping