Functions in MATLAB
A function is a set of instructions when invoked does a specific task to the caller. MATLAB has many in-built functions that we can call in out programs. We can also write our own functions in MATLAB.
- MATLAB in-built Functions.
- User defined Functions.
User defined Function
The key components of a function in MATLAB are:
- Function Header
- Comments
- Function Body
Create a Function
To create a new function, go to the Home tab, click on the plus icon in the Tool strip.
Choose New >> Function menu option to create a new function.
This opens the function editor. We can define the function header, comments and the function body in the editor.
We can create, edit and save function using the editor.
Function Header
Function header specifies the function output arguments, function name and input arguments to the function. The output arguments are enclosed in square brackets. The input arguments are enclosed within parentheses. Sample function header :
[out] = foo(arg1,arg2)