In the last section I mentioned “all the things functions are good for.” About this time, you might be wondering what exactly those things are. Here are some of the reasons functions are useful:
By giving a name to a sequence of statements, you make your program easier to read and debug.
Dividing a long program into functions allows you to separate parts of the program, debug them in isolation, and then compose them into a whole.
Functions facilitate both recursion and iteration.
Well-designed functions are often useful for many programs. Once you write and debug one, you can reuse it.
For example, instead of writing the 53 lines of code below…
..you would reduce it to the 21 lines of code below, making it easier to read, debug, and use the function many times with rewriting it each time.
Create a function called
absoluteValue, which returns the absolute value of a parameter
num. Assume you do not have access to