Chapter 1
Python Basics 3 – Functions and Reuse

Why use functions

15 minutes read

Beginner friendly

Includes practice

👋 Hey there! Ready to unlock one of the biggest productivity boosts in coding?
Today we’ll see why writing your own functions makes programming easier, cleaner, and waaay more fun.

Runtic's Learning Tips

  • Think about functions as tiny machines you build once, then reuse anytime

  • Each function you write reduces copy-paste and bugs

  • Functions help team members (or future you!) understand your code faster

What is a function?

Picture a coffee machine ☕️:

  1. You press a button ("make espresso").
  2. The machine grinds beans, heats water, and pours espresso.
  3. Every time you press the same button, you get the same result.

A Python function works exactly like that button:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

def declares (defines) the new button.
• Inside, you put the steps.
• Later you “press” the button by writing make_espresso().

Why not just copy-paste code everywhere?

Below is a mini story that shows the pain of not using functions.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Looks okay… until:

• You must greet 300 users.
• You decide to change the greeting from “Hello” to “Hi”.
• You miss one line, ship the program, and Bob gets “Hello” while the others get “Hi” 😅.

This is where functions swoop in to save you.

Quick Summary

  • Functions reduce repetition and the chance of mistakes

  • Copy-pasting makes code brittle and hard to scale

  • Reusable code improves long-term maintainability

Four superpowers functions give you

  1. Re-use: Write once, call many times.
  2. Readability: Code is shorter and expresses intent:
    greet_user("Alice") says what happens without drowning you in how.
  3. Maintainability: Want “Hi” instead of “Hello”? Change it in one place.
  4. Decomposition: Big problems shrink into bite-sized subtasks (each subtask = a function).

Worked example: Greet users, the clean way

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Run it. Notice:

• Your script is 3 lines of action instead of 6 repetitive lines.
• If a typo sneaks in, you fix it once.

Quick Practice

2 minutes

Check your understanding about Python functions.

Fill in the blank: The keyword used to create a function is ___

A function may be called before it's defined in the same file.

Name two advantages of using functions over copy-pasting code.

Tiny coding challenge (5 min)

Practice Challenge

Interactive Code Editor
📝 Your Task:

Write a function called convert_c_to_f(celsius)that takes a temperature in Celsius and returns the Fahrenheit equivalent. For example, if the input is 37,the function should return 98.6.Finally, store the result in convert_c_to_f_resultand print it.

python

Use the formula F = C * 9/5 + 32 to convert Celsius to Fahrenheit.

Self-check & reflection

After you attempt the challenge:

  1. Test your function with 0, 37, and 100.
  2. If an answer looks wrong, add print() inside the function to see what’s happening.
  3. Ask yourself:
    • “Did writing a function make testing easier?”
    • “How would I reuse this conversion in a larger weather app?”

Big take-away

Functions let you turn messy, repetitive code into clear, reusable building blocks—much like turning chores into handy buttons you can press any time. Master them early, and future you will thank you!

Lesson Complete!
100%
Progress
What You've Learned:
  • Functions help eliminate repetition and boost code clarity
  • Using def lets you declare logic once and reuse it many times
  • Packaging steps inside a function improves both testing and maintenance
  • Reusable blocks make your programs more scalable and robust

Sign in to track your progress

Chapter Progress

0 secs

AI Tutor

Powered by Runtric

Solve first — tutor's here to guide.

Runtric might be wrong. Think critically.