© Michael Stueben 2018
Michael StuebenGood Habits for Great Codinghttps://doi.org/10.1007/978-1-4842-3459-4_5

5. Function Design

Michael Stueben1 
(1)
Falls Church, Virginia, USA
 

I claim that using functions effectively is tricky, and will try to convince you of this claim with the following examples.

Most of the time you want to create functions that do only one task. No multi-purpose functions—most of the time. I once wrote a graphics program to read in an image file and print it in color (one function) or print it in gray-scale (another function). Much of the code in two functions was the same, or almost the same. Using a Boolean parameter (colorFlag), both the gray and color functions could be combined into one function. Thus, I had one less ...

Get Good Habits for Great Coding: Improving Programming Skills with Examples in Python 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.