Tip 28: Create Default Parameters
No matter how much planning you do, things change. This is especially true of function parameters. When you write a function, you expect a few parameters. Then, as code grows and edge cases emerge, suddenly the parameters you supplied aren’t enough.
In the next several tips, you’ll learn different techniques for handling parameters. Nearly all of these techniques can help you cope in some way with changing requirements. But to start, you’ll learn the easiest trick: setting default parameters.
Handling parameters: Example
Consider a basic helper function. All you want to do is convert pounds to
kilograms. That seems simple. You simply need to take the
weight as an input
and divide the
weight in pounds by
2.2 to get kilograms. (Apologies to non-
Americans who don’t have to deal with this silliness. I’m sure you also get
stuck converting other measurements.)