CS99 Fall 2002 Lecture 12 11/18 ------------------------------------------------------------------------------- (0) Announcements + Prelim 2 reminder + Final Project reminder + missing work issue ------------------------------------------------------------------------------- (1) Topics + review of function basics + MATLAB function syntax and rules + Scope ------------------------------------------------------------------------------- (2) Function Review + functions do tasks + functions may or may not take input values + functions may or may not return values + functions help you program by reducing redundancy and breaking programs into smaller problems ------------------------------------------------------------------------------- (3) Syntax of function M-files FUNCTION [output_args] = name(input_args) H1 comment line accessed by LOOKFOR comments accessed by HELP statements (optional) RETURN (optional) + name: name of function - similar rules for naming as variables - see help pages for additional rules + input_args: names of variables used inside function - visible only to function - not used from "outside", nor visible to "outside" why? each function has a separate workspace with its own names! - may have zero or many inputs + output_args: names of variables to return to caller - the value(s) of the outputs are "sent" back to the expression that invoked the function - may have zero or many outputs + keywords: function, return ------------------------------------------------------------------------------- (4) Handy Rules (from MASTERING MATLAB): + you should give the function file and its the name the same name + function names are used up to 31 chars + begin names with lowercase letter and follow other variable naming rules + the H1 line is FUNCTION DECLARATION LINE (for LOOKFOR) + the following comments are for HELP + BODY of function are the statements + code inside function stops when reaching the last statement or RETURN (RETURN acts like a BREAK because you can exit function "early") + use ERROR to abort execution: example) if length(val) > 1 error('wrong!') end see HELP WARNING to flag the user but continue with execution ------------------------------------------------------------------------------- (5) Scope + range or location where a name is visible (or not) + MATLAB partitions memory into WORKSPACES - each workspace has its own names (variables, scripts, functions) - the command window provides one workspace - each function has a different workspace + variables of a workspace are not shared in other workspaces - variables are called LOCAL VARIABLES when not shared] - use GLOBAL to get around this, but usually discouraged example) Your own function for adding, add.m: +-----------------------------------------+ | function x = add(a,b) | | % ADD(A,B) returns the sum of A and B | a and b are __________ | % ADD(A,B) adds A and B together. | | % This function is just a simple demo. | x is _________________ | x = a+b; | +-----------------------------------------+ At command window prompt: >> add(1,2) ans = >> add(1,2) - add(1,2) ans = >> add(1,2)*sqrt(16) ans = >> x = add(1,2); y = add(3,4); z = add(x,y) z = example) nested functions >> z = add( add(1,2) , add(3,4) ) z = example) design function to swap values In file called swap.m: +------------------------------+ | function [x,y] = swap(a,b) | | x = b; | | y = a; | +------------------------------+ In command window: >> a=1;b=2; >> [a b] = swap(a,b) >> a >> b Even shorter version: +--------------------------------+ | function ______ = swap(______) | +--------------------------------+ -------------------------------------------------------------------------------