08_functions - User-defined Functions ENCMP 100 Overview...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
User-defined Functions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENCMP 100 ©2011 by ECE, UofA User-defined Functions Overview How to create and use MATLAB functions with both single and multiple inputs and outputs How variables are passed into functions How to declare and use global variables How to create and use subfunctions 2
Background image of page 2
ENCMP 100 ©2011 by ECE, UofA User-defined Functions Why functions? When working on a program to solve a given problem we often use so called top-down design We start with a statement of a problem and a list of inputs and outputs Next, we describe an outline of the problem and divide it into sub-problems (sub-tasks) some of the sub-tasks may be further subdivided Next, each sub-tasks is implemented and tested separately Finally, the codes that implement these sub-tasks are merged and tested together In prior lectures we combined the code into a single large 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENCMP 100 ©2011 by ECE, UofA User-defined Functions Scripts vs. Functions The M-files that we created so far have been scripts Collections of MATLAB statements stored in a file When executed, the result is the same as if these statements would be directly typed in the Command Window Scripts use the Command Window workspace variables in the workspace created before executing the script are available to this script and variables declared in the script remain in the workspace after the script is executed 4
Background image of page 4
ENCMP 100 ©2011 by ECE, UofA User-defined Functions Scripts vs. Functions MATLAB functions are special M-files that have their own workspace They do not have direct access to variables in the Command Window workspace they cannot change their values (which could happen by accident) They execute faster that the scripts They receive input data through input argument list They return results through output argument list they “communicate” with the Command Window workspace and other functions through the output arguments, which are stored in the Command Window workspace 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENCMP 100 ©2011 by ECE, UofA User-defined Functions Functions: benefits (1) MATLAB allows to package individual sub-tasks into separate functions , which provides certain benefits Independent testing of sub-tasks You can test each sub-task separately before combining them into large program when testing becomes more complex. This step is called unit testing . Reusable code If the same sub-tasks need to be executed multiple times (in multiple places) in your main program then you can reuse the same function, instead of copying code. In some cases sub-tasks (functions) can be reused in multiple programs. 6
Background image of page 6
ENCMP 100 ©2011 by ECE, UofA User-defined Functions Functions: benefits (2) MATLAB allows to package individual sub-tasks into separate functions , which provides certain benefits Isolation from unintended side-effects Each functions has its own workspace with its own variables. This is very important as it prevents accidental mistakes within a function from affecting the contents of the Command Window workspace
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 41

08_functions - User-defined Functions ENCMP 100 Overview...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online