Function Recipes

Recipes for writing and using your own functions:

  1. Write Your Own Function: I want to easily reuse a particular calculation multiple times, with different givens.

  2. Make Arguments Optional: I want my code to decide what to do based on the circumstances.

  3. Execute Conditionally: I want my code to decide what to do based on the circumstances.

  4. Safety-Check Arguments: I want to make my function robust against incorrect or unexpected arguments.

  5. Factor Out Subfunctions: I want to break a complicated calculation into pieces to keep it manageable.

  6. Use Global Variables (Sparingly!): I need to get values deep into a function or subfunction, and passing them via arguments is tedious.

  7. 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.