These questions follow Functions and
Parameters, Returns, and Scope. A function is a named piece of
thinking: information goes in through parameters, an answer comes back
through return, and everything else stays inside.
Reading
In def letter_grade(mark):, called as letter_grade(78), name
these four things: the definition, the parameter, the argument, and
what return "B" does.
def average(values): total = 0 for value in values: total = total + value total / len(values)print(average([78, 91, 46, 63]))
Why does this fail, and what should the last line be instead?
def total_of(values): total = 0 for value in values: total = total + value return totaltotal_of([1, 2])print(total)
Writing
Write kilograms_to_pounds(kilograms), which returns the mass in
pounds (multiply by 2.2). Print the result for 6.5 kg to one
decimal place.
Write is_overdue(days_late), which returns True or False. Show
it being used directly inside an if.
Write greet(name, greeting="Hello") so that greet("Priya") and
greet("Priya", "Welcome back") both work.
Challenge. These two lines appear in four places in a program:
hours = minutes // 60rest = minutes % 60
Turn them into a function, then write a second function that uses
the first to produce Priya: 3 h 5 min.
Answers
Answer 1
def letter_grade(mark): is the definition; mark is the parameter,
a name that only exists inside the function; 78 is the argument,
the value supplied at the call; and return "B" hands "B" back to
whoever called and ends the function immediately. Leave the argument
out and Python is precise about it:
10, then None. The function prints but never returns, so
result receives Python’s “no value at all”. A function that prints
can only ever do that one thing; a function that returns can be
stored, compared, or passed on.
Answer 3
The last line of the function computes the average and throws it
away — there is no return, so the call produces None. Add it:
def average(values): total = 0 for value in values: total = total + value return total / len(values)print(average([78, 91, 46, 63]))
69.5
Answer 4
total is local to total_of — created when the function starts and
gone when it ends:
NameError: name 'total' is not defined
The caller has to catch the returned value:
print(total_of([1, 2])), which prints 3. That restriction is a
feature: nothing outside the function can be damaged by what happens
inside it.
The function returns the full-precision number; the rounding belongs
at the point of display, not inside the calculation.
Answer 6
def is_overdue(days_late): return days_late > 0if is_overdue(3): print("Send the reminder.")
days_late > 0 is already True or False, so there is no need to
write if days_late > 0: return True. Naming a condition like this
is the cheapest readability win in the language.
The awkward arithmetic now exists once. When the coach asks for
3h05 instead, one function changes and every line of output follows
— which is the entire argument for writing functions in the first
place.