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 ☕️:
- You press a button (
"make espresso"). - The machine grinds beans, heats water, and pours espresso.
- Every time you press the same button, you get the same result.
A Python function works exactly like that button:
Interactive Code EditorExample Code
pythonThis 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.
Interactive Code EditorExample Code
pythonThis 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
- Re-use: Write once, call many times.
- Readability: Code is shorter and expresses
intent:
greet_user("Alice")says what happens without drowning you in how. - Maintainability: Want “Hi” instead of “Hello”? Change it in one place.
- Decomposition: Big problems shrink into bite-sized subtasks (each subtask = a function).
Worked example: Greet users, the clean way
Interactive Code EditorExample Code
pythonThis 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 minutesCheck 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
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.
Use the formula F = C * 9/5 + 32 to convert Celsius to Fahrenheit.
Self-check & reflection
After you attempt the challenge:
- Test your function with
0,37, and100. - If an answer looks wrong, add
print()inside the function to see what’s happening. - 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!
- Functions help eliminate repetition and boost code clarity
- Using
deflets 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
AI Tutor
Solve first — tutor's here to guide.
Runtric might be wrong. Think critically.