Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1
Functions: There’s a Name for That 153

Figure 7-2: The RandomSmileys.py program produces a
happy result.

r turn: e it’s What You Give Back


that counts


We can send information to a function using arguments, but
what if we want to receive information back from a function? For
example, what if we build a function to convert inches to centime-
ters, and we want to store the converted number to use in further
calculations, rather than just printing it directly to the screen? To
pass information from a function back to the rest of our program,
we use a return statement.

Returning a Value from a Function


There are lots of times when we want to get information back from
a function. For example, let’s actually build the function to convert
inches to centimeters and call it convert_in2cm(). We can imagine
the parameter that we might want to accept in the function: a
measurement in inches. But this function is a perfect candidate
for giving information back to the rest of our program—namely,
the converted measurement in centimeters.
Free download pdf