runtric-logo
Python Basics 3 – Functions and Reuse“Calculator Functions” or “Temperature Converter” (6/6)
Chapter 6
Python Basics 3 – Functions and Reuse

“Calculator Functions” or “Temperature Converter”

24 minutes read

Beginner friendly

Includes practice

Hi there! 👋 In this lesson, you’ll write small, reusable functions to add calculator features, convert temperatures between Celsius and Fahrenheit.

By the end, you should be able to:

  • Explain what a function is, why we wrap code in a function, and how to call one
  • Write your own functions using def, parameters, and return
  • Combine simple functions to build a slightly bigger program

Take it step-by-step, run the examples, and try the practice before peeking ahead. You’ve got this!

Runtic's Learning Tips

  • Think of functions as reusable tools — once you build them correctly, you can use them anywhere in your code

  • Test each function by calling it with inputs and checking the output immediately

  • Functions help break problems into smaller chunks — don’t try to solve everything at once

Why Use Functions?

Imagine copying the same 3 lines every time you want to add two numbers — sounds repetitive and error-prone, right? That’s where a function comes in handy.

We can:

  • Give the task a name (add)
  • Hide the details inside the function body
  • Reuse it with a short call anywhere

This keeps your code clean and efficient.

Quick Summary

  • Functions group related logic into a reusable component

  • They reduce repetition and allow for easier debugging

  • Defining a function makes code neater and more readable

Calculator Functions – Core Examples

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Notice:

  • def introduces the function with name and input parameters
  • Inside, the return keyword sends back a value
  • Once defined, we can call the function just like print() or len()

Quick Practice

2 minutes

Check what you understand about how return and parameter work in functions.

What does the return keyword do?

What happens if you don’t write return in a function?

Practice: Add multiply() and divide()

Practice Challenge

Interactive Code Editor
📝 Your Task:

Write two functions: multiply(a,b)that returns the product of two numbers, and divide(a,b)that returns the result of dividing aby b.If bis 0, return "Error".Then call both functions with 5 and 3 (for multiply) and 5 and 2 (for divide). Finally, print both results to verify.

python

Use * for multiplication and / for division. Handle divide-by-zero carefully.

A Menu-Driven Calculator Function

By building small functions, we can plug them into a larger “menu” system:

Practice Challenge

Interactive Code Editor
📝 Your Task:

Complete the functions for add,subtract,multiply,divide,and implement calculator(a,b,operator)that uses them. Handle divide-by-zero and unknown operators. Then test it with multiple operators and store each outcome in separate result variables (result,result2,result3,result4).

python

Implement calculator(a, b, operator) that performs addition, subtraction, multiplication, and division using helper functions.

Quick Practice

3 minutes

More quiz-style checks to boost recall before moving on.

True or False: A function MUST take at least one parameter.

What keyword immediately exits a function and hands back a result?

What error will calling multiply(4) (with one input) trigger?

Temperature Converter – Reuse More Logic

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Math Recap:

  • °C to °F = multiply by 9/5, then add 32
  • °F to °C = subtract 32, then multiply by 5/9

Practice: Add Kelvin Support

Practice Challenge

Interactive Code Editor
📝 Your Task:

Define two functions: c_to_k(c)and k_to_c(k)to convert between Celsius and Kelvin using the formulas Kelvin=Celsius+273.15and Celsius=Kelvin-273.15.Then create variables cand k,call the functions, and print the results rounded to two decimal places.

python

Use Kelvin = Celsius + 273.15 and Celsius = Kelvin - 273.15. Round the results for clean output.

Lesson Complete!
100%
Progress
What You've Learned:
  • Defined and used functions to return answers like add/subtract
  • Saw how functions let us reuse and build onto clean logic
  • Wrote practical temperature + calculator tools using functions
  • Practiced quick checks to boost long-term memory

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.