Functions let you name a block of code and reuse it. Instead of copying the same logic in three places, you write it once as a function and call it wherever you need it.
What you'll learn
- How to define a function with
def - How parameters and return values work
- What scope means (local vs global variables)
- Default argument values
Instructions
- Write a function
celsius_to_fahrenheit(c)that converts Celsius to Fahrenheit.
- Formula:
F = C * 9/5 + 32
- Write a function
classify_temp(f)that returns a string:
- Below 32 →
"freezing" - 32–59 →
"cold" - 60–79 →
"comfortable" - 80 and above →
"hot"
- Read a Celsius temperature from stdin. Convert it, classify it, and print both.
Example output for input 22:
22°C = 71.6°F
Classification: comfortable
Key concepts
Defining a function:
def greet(name):
return f"Hello, {name}!"
defstarts the definitionnameis a parameter — a placeholder for the value you'll pass inreturnsends a value back to the caller
Calling a function:
message = greet("Alice") # "Hello, Alice!"
Default arguments let you make parameters optional:
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
Scope: Variables created inside a function only exist inside that function. They don't leak out.
Hints
- Use
round()if you want to limit decimal places:round(71.6, 1). - Your
classify_tempfunction should useif/elif/else— you already know how from the conditionals lesson. - Make sure you're calling the function and printing the result, not just defining it.