../_images/book_cover.jpg

This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.

The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!

< Chapter 2 Summary and Problems | Contents | 3.1 Function Basics >

Chapter 3. Functions


Motivation

Programming often requires repeating a set of tasks over and over again. For example, the math.sin function in Python is a set of tasks (i.e., mathematical operations) that computes an approximation for sin (x). Rather than having to retype or copy these instructions every time you want to use the sin function, it is useful to store this sequence of instruction as a function that you can call over and over again.

Writing your own functions is the focus of this chapter, and it is the single most powerful use of computer programming. By the end of this chapter, you should be able to declare, write, store, and call your own functions.