ICS 31 • David G. Kay • UC Irvine
The "Design Recipe" for Functions
Every time you write a function, you should follow these steps. They don't do the job for you automatically—if it could do that, software engineers wouldn't make high salaries—but it does help you get organized and avoid many common mistakes. Notice in particular how much you do before you actually start writing the code in the body of the function (in step 6 out of 7).
(The idea of a "design recipe" for programs comes from the Program By Design curriculum.)
name = input('Hello; name please:')
input()
function; a function that "prompts (or asks) the user for a number as input" does call input()
.
Deciding whether to design functions with parameters and returned values or alternatively with print statements or other direct interactions is a question that requires more sophistication than we usually expect in a first course. In most cases, then, the lab problem or exam question will tell you explicitly what to do (e.g., "take a number as a parameter and return its square root" or "prompt the user to input a number and then print that number's square root"). Read the problem carefully to find this vital information.
As a general principle, though, it's usually best to write every function you can with parameters and return values. That makes them flexible; the program that calls it can decide what to do with the returned result (print it, send it to a robot, whatever).
# Restaurant_price: rest: Restaurant -> float
or it can use actual Python annotations in the actual Python function header (also called the function signature):
def Restaurant_price(rest: Restaurant) -> float:
If you're not clear about what type(s) of data your function expects and what type it returns, you can't get any further.
" Take a total restaurant bill and compute the tip on that specified amount "
We will write this line as a quoted string and place it right after the function definition in the final code; that's called a "docstring"; Python will pick it up and display it when we call the help()
function.
""" Your docstring can be triple-quoted; among other things, this lets you spread it over two lines if you need to. """
assert
statement—the word assert
followed by a Boolean (true or false) expression that should be true if the function is correct:
assert triple(0) == 0
assert triple(5) == 15
Write enough different examples to convince yourself (and your TA) that your function works correctly on all expected kinds of data.
def
for the function.
def triple(value: int) -> int: " Return three times the parameter value " return 3 * value assert triple(0) == 0 assert triple(5) == 15