Function Recipes
Recipes for writing and using your own functions:
-
Write Your Own Function: I want to easily reuse a particular calculation multiple times, with different givens.
-
Make Arguments Optional: I want my code to decide what to do based on the circumstances.
-
Execute Conditionally: I want my code to decide what to do based on the circumstances.
-
Safety-Check Arguments: I want to make my function robust against incorrect or unexpected arguments.
-
Factor Out Subfunctions: I want to break a complicated calculation into pieces to keep it manageable.
-
Use Global Variables (Sparingly!): I need to get values deep into a function or subfunction, and passing them via arguments is tedious.
-
Run or Import From a Script: I want to write a script that I can run, and I also want to use functions it defines in the console or other scripts.