Chapter Review Questions#
Note
Source: Adapted from the C# edition (functions/reviewfunc.rst).
Python-specific questions (docstrings, import, implicit None) are
original additions.
The purpose of functions.
What is the purpose of a function?
Name two advantages of using functions over copying and pasting code.
What is the difference between a parameter and an argument?
A function can have a
returnstatement, or it can have none. What value does Python return if there is noreturnstatement?What does local scope mean? Why is it useful?
Which of the following names would Python allow as a function name? Which follow Python’s naming convention?
calculateAreacalculate_area2fastdefTOTAL
Using the
mathmodule.What does
import mathdo?How do you then call
math.sqrt?
Write a function
rectangle_area(length, width)that returns the area of a rectangle. Include a docstring.A temperature converter.
Write a function
celsius_to_fahrenheit(c)that converts a temperature from Celsius to Fahrenheit (\(F = C \times 9/5 + 32\)) and returns the result.Write a short program that calls that function and prints the result.
What is a docstring? Write a docstring for the following function:
def hypotenuse(a, b): return (a**2 + b**2) ** 0.5
Trace through the following code and predict the output:
def f(x): y = x * 2 return y + 1 a = 3 b = f(a) print(a, b)