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:

print("hello") len("matty") range(10)

Now it’s time to create your own.

Step 2 — Defining Your First Function

A simple function looks like this:

def greet(): print("Hello there!")

To run it:

greet()

Breakdown:

  • def → tells Python you're defining a function

  • greet → the function name

  • () → takes no inputs

  • The indented block → code that runs when called

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 3 — Adding Parameters (Inputs)

Parameters allow functions to work with different data.

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

Calling it:

greet("Matty") greet("Alice")

Each call uses a different input.

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 4 — The Difference Between Printing and Returning

print() → Displays information for the user.

return → Sends data back to your program.

Example:

def add_print(a, b): print(a + b) def add_return(a, b): return a + b

Watch the difference:

add_print(2, 3) # prints 5, but you cannot use the value x = add_return(2, 3) print(x * 10) # 50

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: -

You can also download this example from my GitHub here and run it yourself.

Step 5 — Returning Values

A function can send a result back using return.

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

Use it like this:

result = add(5, 3) print(result) # 8

Return values unlock real computation and reusable logic.

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 6 — Variables Inside Functions (Local Scope)

Variables created inside a function exist only inside that function.

def test(): x = 10 print(x) test() print(x) # ERROR — x doesn't exist here

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:

def stats(numbers): return min(numbers), max(numbers)

When you call it:

low, high = stats([4, 9, 1, 7]) print(low, high) # 1 9

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: -

You can also download this example from my GitHub here and run it yourself.

Step 8 — Combining Functions Together

Functions can call other functions — this is how bigger programs are built.

def double(n): return n * 2 def triple_and_double(n): return double(n * 3)

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

def c_to_f(c): return (c * 9/5) + 32

Fahrenheit → Celsius

def f_to_c(f): return (f - 32) * 5/9

Celsius → Kelvin

def c_to_k(c): return c + 273.15

Kelvin → Celsius

def k_to_c(k): return k - 273.15

Fahrenheit → Kelvin (using other functions)

def f_to_k(f): return c_to_k(f_to_c(f))

Kelvin → Fahrenheit

def k_to_f(k): return c_to_f(k_to_c(k))

Notice how clean functions make everything reusable.

Step 2 — Menu Function

def show_menu(): print("Temperature Converter") print("---------------------") print("1. Celsius → Fahrenheit") print("2. Fahrenheit → Celsius") print("3. Celsius → Kelvin") print("4. Kelvin → Celsius") print("5. Fahrenheit → Kelvin") print("6. Kelvin → Fahrenheit")

Step 3 — Main Program

def main(): show_menu() choice = input("Choose an option (1-6): ") temp = float(input("Enter the temperature: ")) if choice == "1": print(c_to_f(temp)) elif choice == "2": print(f_to_c(temp)) elif choice == "3": print(c_to_k(temp)) elif choice == "4": print(k_to_c(temp)) elif choice == "5": print(f_to_k(temp)) elif choice == "6": print(k_to_f(temp)) else: print("Invalid choice.")

Run it:

main()

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

Popular posts from this blog

Math Behind Logic Gates

6502 - Part 2 Reset and Clock Circuit

Building a 6502 NOP Test