Learn Python in 30 Days — Day 15: Functions (Basics)
Learn Python in 30 Days — Day 15: Functions (Basics)
Welcome to Day 15 of the Learn Python in 30 Days series!
You’ve spent two weeks learning how to store data, loop through things, and make decisions. This week, we take a big step forward: writing your own functions. Functions help you organise your code, avoid repeating yourself, and build programs that grow without becoming messy.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
Today you’ll learn:
-
How to define and call functions
-
How parameters work
-
How return values work
-
The difference between printing and returning
-
Local variables (scope)
-
Returning multiple values (a super useful beginner trick)
-
How to combine functions to build real programs
And you'll finish by making a Temperature Converter that handles Celsius, Fahrenheit, and Kelvin.
Let’s begin!
Step 1 — What Is a Function?
A function is a named block of code that performs a specific job.
You’ve already used built-in functions:
Now it’s time to create your own.
Step 2 — Defining Your First Function
A simple function looks like this:
To run it:
Breakdown:
-
def→ tells Python you're defining a function -
greet→ the function name -
()→ takes no inputs -
The indented block → code that runs when called
Step 3 — Adding Parameters (Inputs)
Parameters allow functions to work with different data.
Calling it:
Each call uses a different input.
Try it yourself, hopefully you'll see something like that shown below: -
Step 4 — The Difference Between Printing and Returning
print() → Displays information for the user.
return → Sends data back to your program.
Example:
Watch the difference:
If your function calculates something, it should almost always return the value, not print it.
This single idea separates beginner code from real-world code.
Try it yourself, hopefully you'll see something like that shown below: -
Step 5 — Returning Values
A function can send a result back using return.
Use it like this:
Return values unlock real computation and reusable logic.
Try it yourself, hopefully you'll see something like that shown below: -
Step 6 — Variables Inside Functions (Local Scope)
Variables created inside a function exist only inside that function.
Explain simply:
-
Variables inside a function = local
-
They disappear when the function finishes
-
They can't be accessed from outside the function
This prevents accidental bugs and keeps code clean.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Step 7 — Returning Multiple Values
Python allows you to return more than one value at once:
When you call it:
This works because Python packs the values into a tuple and unpacks them into variables.
It’s simple but extremely powerful.
Try it yourself, hopefully you'll see something like that shown below: -
Step 8 — Combining Functions Together
Functions can call other functions — this is how bigger programs are built.
Small pieces → bigger behaviours.
Mini-Project: Temperature Converter
Let’s use everything you've learned.
We’ll support:
-
Celsius ↔ Fahrenheit
-
Celsius ↔ Kelvin
-
Fahrenheit ↔ Kelvin
Step 1 — Conversion Functions
Celsius → Fahrenheit
Fahrenheit → Celsius
Celsius → Kelvin
Kelvin → Celsius
Fahrenheit → Kelvin (using other functions)
Kelvin → Fahrenheit
Notice how clean functions make everything reusable.
Step 2 — Menu Function
Step 3 — Main Program
Run it:
Congrats you’ve just built your first modular program using functions!
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Summary
Today you learned:
-
How to define and call functions
-
How parameters work
-
How return values work
-
The crucial difference between printing and returning
-
Why local variables matter
-
How to return multiple values
-
How to combine functions to solve bigger problems
-
Built a working Temperature Converter
You now have the foundation for writing clean, organised, professional Python code.
Next Up — Day 16: Arguments & Default Values
You’ll learn keyword arguments, default values, docstrings, and clean function design.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!
Hope you have enjoyed this post, thanks Matty






Comments
Post a Comment