A very useful aspect of many programming languages, including Matlab, is the ability to create functions. (Note: the term, function, is used here as a computer programming concept. This is not the same thing as a function in mathematics.)
Already, we have looked at how to use built-in functions including: sqrt, abs, log10, audioread, audiowrite, plot, etc. These functions help us as programmers accomplish complicated tasks using a single command.
Additionally, we can create our own user-generated functions. In this case, we are writing the code to perform the task of a function. Then, we can execute the entire process by using a single command.
A user-generated function is created by putting the keyword, function, at the start of an m-file. This gives a special designation to the m-file and makes it possible for the file to have input and output variables.
There are several benefits of creating functions. First, functions can make code easier to read by putting a sequence of complicated steps into a separate file. Rather than having to look at a hundred lines of code to save a sound file to our computer’s hard drive, we can perform this process with a single command, which also has a name that describes what it will do.
Additionally, functions make code easily reusable. For audio engineers, this is similar to having a DAW plug-in that can be instantiated dozens of times within the same session. Functions can be used as often as we want in our code. This makes the process of programming faster and more efficient.