Section 8.1 Introduction to Functions
In this chapter we’ll learn about the different ways in which MATLAB allows us to encapsulate code into a function that can be called upon with different input parameters. We will see that there are several differences to how we write function in C.
The biggest difference is that MATLAB does not have pointers (yeah!). Rather, instead of passing variables which we would like to modify by reference (as pointers) to functions as we had to do in C, MATLAB allows us to have multiple return values (in the form of a vector) from a function.
We shall also see that the comments in our functions can also be used as a form of help within MATLAB.
Once we make sure the function works properly, we no longer have to worry about the details of how it works.
By using functions we can reuse code. Blocks of code (statements) that perform a specific task can be used with different variables (inputs) over and over again. You can therefore view a function as a black box that transforms inputs into outputs.
Function code can use all MATLAB programming features.