Defining Our Own Functions

The built-in functions are useful but pretty generic. Often there aren’t built-in functions that do what we want, such as calculate mileage or play a game of cribbage. When we want functions to do these sorts of things, we have to write them ourselves.

Because we live in Toronto, Canada, we often deal with our neighbor to the south. The United States typically uses Fahrenheit, so we convert from Fahrenheit to Celsius and back a lot. It sure would be nice to be able to do this:

 >>>​​ ​​convert_to_celsius(212)
 100.0
 >>>​​ ​​convert_to_celsius(78.8)
 26.0
 >>>​​ ​​convert_to_celsius(10.4)
 -12.0

However, function convert_to_celsius doesn’t exist yet, so instead we see this (focus only on the last line of ...

Get Practical Programming, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.