Functions
A function is a named recipe. Define it once with def, then call it whenever you need it:
def square(n):
return n * n
print(square(4)) # 16
print(square(10)) # 100
defstarts the definition; the body is indented.nis a parameter: a placeholder for the value passed in.returnsends the result back and ends the function.
Functions can take several parameters, and give them default values:
def describe(name, job="student"):
return f"{name} is a {job}"
describe("Ana") # "Ana is a student"
describe("Tino", "teacher") # "Tino is a teacher"
return is not print
print only shows a value. return gives it back so your program can store it, compare it or pass it on. A function without return gives back None. The checks for this lesson call your functions and look at what they return.
Your task
Write three functions:
greet(name)returnsHello, <name>!celsius_to_fahrenheit(c)returns the temperature in Fahrenheit (c * 9 / 5 + 32).largest(a, b, c)returns the biggest of three numbers, usingifstatements (notmax).