23. User-defined functions

23. User-defined functions - User-defined Functions For an...

This preview shows pages 1–5. Sign up to view the full content.

©2009 by L. Lagerstrom User-defined Functions • For an introduction to the basic concepts, see the associated video clip • Matlab functions and toolboxes • The idea of user-defined functions • The modularization principle • Creating a function (the "sin_deg" example) • Local variables • Storing function files • Multiple inputs and outputs • Miscellaneous (but important) advice

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

View Full Document
©2009 by L. Lagerstrom Matlab Functions and Toolboxes So far in our study of Matlab we have been introduced to a number of different functions, such as sqrt, sin, log10, residue, etc. etc. These functions are all "built-in" to Matlab. They're there for us to use. We just have to use the correct syntax, etc. Sometimes, however, Matlab doesn't have what we need. In any given field of science and engineering there are many calculations that are done often, but only in that field. So it's unlikely that Matlab would have them as part of its basic set of functions. Instead, Matlab has what are known as "toolboxes." These are sets or libraries of functions that you can add to your copy of Matlab. For example, there is a "digital signal processing" toolbox that has many functions useful in the analysis of digital signals. (These toolboxes are available for purchase from mathworks.com.)
©2009 by L. Lagerstrom The Idea of User-defined Functions Matlab's toolboxes are nice, because it means we don't always have to reinvent the wheel. But sometimes what we need isn't available, or we only need one or two functions, not a whole toolbox. So Matlab allows us to create our own functions (i.e., "user-defined functions"), as most programming languages do. Consider a script file that you are writing. In it there is perhaps a chunk of code that you use several times throughout the script. You also think that it is highly possible you will need to use that chunk of code in future scripts as well. That chunk of code is then a perfect candidate to be put into a user- defined function. Once you have stored it in its own function with some name, then any time you need to use it you don't have to write it again, or go back and try to find it in an earlier script so that you can copy and paste it. Instead, you just refer to it by its name, just like we do when we need to calculate a square root, or a logarithm, or whatever.

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

View Full Document
©2009 by L. Lagerstrom The Modularization Principle This idea of storing often-used chunks or modules of code in a separate location with some identifying name is a big one in computer programming. This modularization principle is especially important for large programs, which can have thousands and even millions of lines of code. The only practical way to handle such large programs is to divide them into key components. Each component (module) is then written and debugged separately. Then all the modules can be linked together (requiring more testing and debugging, of course) to make the final program. (A rough analogy comes from writing. If you tried to write a 10-page
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 26

23. User-defined functions - User-defined Functions For an...

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

View Full Document
Ask a homework question - tutors are online