In the early stages of designing a function it might be useful to think of a function as a black box.
- Don’t worry about what goes on inside, but instead worry about the overall purpose of the function, what needs to be input into the process, and what is produced by the process.
The function purpose can give you some ideas for the function name.
- The inputs correspond to the parameters that you must supply in order for the function to do its work, and the output is what is returned from the function via the return statement.
- Only one output is allowed with functions.
Once you have those details resolved you can finally worry about what is required to convert the inputs into the desired output.
- This is the function body, and is type of coding that you’ve done prior to using functions.
One advantage of the black box approach is that if you later decide to rewrite the function, you can seamlessly replace the old version with the new version as long as the function name, inputs (parameters), and output remain unchanged.
- This is referred to as the function interface.
- In other words, you can pull out one "black box" and replace it with another.