Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions In Matlab.

Similar presentations


Presentation on theme: "Functions In Matlab."— Presentation transcript:

1 Functions In Matlab

2 What is a Function? Functions are text files containing one or more lines of code (similar to scripts) but where all of the variables are defined only within the function and are invisible outside the function (i.e. they are local in scope). Functions always start with the word function and then the function’s name. E.g. function MyFunction The name of the function must match the name of the m-file in which it is saved. E.g. MyFunction.m Functions may optionally take one or more input arguments and may return one or more output arguments.

3 These must be the same.

4

5 Try clicking on this line.
“y” is only defined within the function and is invisible to other functions or the command window.

6 This is called a “break point
This is called a “break point.” The function will pause execution at this point and temporarily make the variables accessible in the command window.

7 Now I can see that the input x = 3.

8 Clicking “Continue” causes Matlab to advance past the break point and complete execution of the program.

9

10 Functions with more than one input separate the inputs using commas.

11 Functions with more than one output also use commas to separate the variables.

12 Functions can also be defined to have a variable number of input arguments using “varargin.”
Within the function, “nargin” tells you the number of input arguments passed to the function. Each input argument is accessed as a cell in varargin.

13 Functions can also be defined to have a variable number of output arguments using “varargout.”
Within the function, “nargout” tells you the number of output arguments requested from the function. Each output argument is assigned as a cell in varargout.

14 Functions can be called from within scripts.
Here, any variable names that the script uses (e.g. x, y) are not overwritten by the function.

15 Functions can also be called within other functions.

16 More than one function can be place in the same file.
The first function is used to call the function from the command window, a script, or another function, and the remaining functions become sub-functions that can be called from within the file.

17 Variables can be accessed from within a function by making them global in scope.
This means that we don’t need to pass the variable as an input to the function and that changes to the variable within the function will persist outside of the function. The rest of the variables within the function will still remain invisible outside of the function.

18 Help The first commented text (%) in a function is treated as a help file. This text is printed to the commend window when someone types: help FunctionName.

19 Help Files A well-written help file can prevent a lot of headaches years after a function has been written. Good practices include: Say what the input and output variables are and how to define them. Give an example that works. Say who wrote the function and on what date.

20 Matlab’s function “lookfor” scans the first line of each help function for the searched keyword. Thus, a good practice is to start your help file with the function’s name in capitols followed by a concise description of what it does. This can help you find a function whose name you forget, if you remember what it does.

21 Advantages of Functions
Often programmers need to do the same thing over and over again. It is time-consuming to re-write the same code every time the same thing needs to be done, and the more times something is re-written, the more likely it is that one of those times will contain a mistake. Functions allow re-usable code to be written once in a convenient form that can be re-used over and over again, but will not interfere with any of the other variables in new programs. They only need to be checked once for correctness and then they can be re-used forever. Functions are a good way to compartmentalize code – they allow small pieces of code to be put together in different ways to make something bigger. Breaking up big programs into smaller pieces makes things easier to understand.


Download ppt "Functions In Matlab."

Similar presentations


Ads by Google