ENG 06 – Lecture 8 notes.docx

6 Pages
Unlock Document


ENG 06 – Lecture 8; 1/31/2013 Custom Functions  Concept: Procedural Abstraction o Permits a code block that solves a particular sub problem to be packaged and applied to different data inputs  Hides details of code  Concept: Encapsulation o Putting a wrapper around a collection you want to protect from outside influence o Encapsulate code in 2 ways:  Variables declared within function are not visible from outside  Functions can only change values of variables within its own code body  Don‟t confuse scripts with functions o M-file scripts:  Sequence of matlab command lines  No required beginning or end line  Sequential execution of commands, top to bottom  Can be called by other m-file scripts  No passing of variables as arguments; when called  Variables go to the workspace  Pre-exisiting variables with same name overwritten  Function as a black box o Parameters:  Formal parameters are names given to incoming data in function  are actual parameters provided to function  may have names different from formal parameter  correlated to formal parameter names by position  Variable names outside and inside a function referring to same parameter can be different 1. A variable in a program is known as orange 2. A variable name outside the function is apple 3. Apple is placed in an input “drawer” 4. Whatever is placed in this “input drawer” can be known as something else, like orange 5. Orange, and other inputs, are manipulated inside the “chest of drawers” and additional variables are defined, like banana 6. Manipulation results in output variables that are placed in “output drawers” e.g pear 7. Whatever is placed in this output drawer, once outside the drawer, can be known as something else, like banana  Functions o M-file functions  First line is function declaration  Function elements to be returned are identified  Variables can be imported as arguments  Function name should correspond to m-file name  First line format for m-file function_name.m o Function [ret1, ret2] = function_name(a1,a2)  Computed function element values returned  Can be called by other m-file functions  Can call themselves (recursion)  Have their own local workspaces o Example of function #1  Create “doubler” which doubles some number x when you type doubler(x)  Type following code  Function y = doubler(x)  Y=2*x  End o Important: use semicolon to suppress output!!!  Good Practices for Functions o Make sure function is easy to use o User should not have to read the code to figure out how to “call” the function o When you type help sin or any other function, instructions are displayed  You should also create help functions  Immediately following official “first line” of function (function output = name(input), create comments  Comments displayed when you type help function Name  Ex:  function [s p] = sumandproduct(x,y)  % [s p] =sumandproduct(x,y) calculates the sum and product from two scalars  % Inputs: x, y – two scalar inputs
  Outputs: s–scalar equal to sum of x & y
  % p – scalar equal to the product of x & y  s = x+y;  % however, this line is not is not part of documentation  p = x*y;  end  Define purpose, input, and output in these comments  Error Checking o Make sure functions work as intended even for bad inputs o Ex. if a user tries to use doubler(„cow‟), we don‟t want it to return a vector, but an error message o Define an error message with if-else:  function y = doubler(x)
  % y = doubler(x doubles a scalar
  % Input: x value, type scalar
  % Output: y doubled value, type scalar
  if(isnumeric(x)) % isnumeric returns 1 only if x is a number  y = 2*x;  else  error('x is not numeric. doubler(x) only works when x is a number')  end  end  Example of Function #2 o Sometimes, need more than 1 input, more than 1 output o With sumandproduct function, if user does not ask for 2 outputs, function only returns 1 o Ex.  if a user types sumandproduct(5,3), ans=8  if a user types [a,b] = sumandproduct(5,3), a = 8 and b = 15 o good practice to make it so user does not have to ask for 2 outputs o This can be accomplished by having one output but making that output a variable  function [s] = sumandproduct2(x,y)  % [s] =sumandproduct(x,y) calculates the sum and product from two scalars  % Inputs: x, y – two scalar inputs
  % Outputs: s(1)equals to sum of x & y
  % s (2) equals to the product of x & y  s(1) = x+y;
  % however, this line is not is not part of documentation  s(2) = x*y;
  end  Example of function #3 o Slightly more advanced function that accepts input in either Celsius or Fahrenheit (specify which you want), and returns result converted to other system  function converted = TempConverter(temp,system)  % Converts Fahrenheit to Celsius, or vice versa
  % TempConverter accepts two arguments
  % temperature, and system ('F' or 'C')  As written, the function has a problem if system is
 not exactly „F‟ and „C‟. This should be corrected by adding error correction.  % TempConverter(15,
More Less

Related notes for ENG 6

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.