CS99 Fall 2002 Lecture 13 11/15/2002 ------------------------------------------------------------------------------- (0) Announcements + Prelim 2 -- follow instructions in e-mail and on News (reasons for doing this?) + Reminder about Final Project: due 12/6 + missing work issue ------------------------------------------------------------------------------- (1) Overview + a bit more function syntax + varying returns and inputs + subfunctions + function functions + function advice ------------------------------------------------------------------------------- (2) Inputs and Returns + can have as many inputs and returns as you want - may even have zero of either or both - MATLAB actually allows a call to a function with fewer inputs and returns than required by the function, though you may run into trouble with too few inputs + can have different amounts of inputs and returns + NOTE: returns and outputs are different! ================= 1 input, 1 return ================= function v=y(x) v = x+1; <-----------------------no semicolon gives output, too! >> y(1:2) ans = ======================== multiple input, 1 return ======================== function v=y(x1,x2) v=x1+x2; >> y(1,2) ans = ------------------------------------------------------------------------------- ================================ multiple inputs, multiple return ================================ function [v,w] = y(x1,x2) v = x1+x2; w = x1.*x2; >> y(1,2) % assumes you mean just the FIRST output ans = >> z = y(1,2) % assumes you mean just the FIRST output z = >> [a b] = y(1,2) % now you get multiple outputs a = b = ========= no return ========= function y(x1,x2) x1+x2 >> y(1,2) ans = But did ans really get assigned? Which ans got assigned? function y() % could also say "function y" disp('hi'); >> y % you must enter with no "()" ans = ------------------------------------------------------------------------------- (3) Subfunctions: + can put other functions in a function M-File + these functions are written below the first function + the first function is usually the name of the M-File and is visible to other functions in the same path + the other functions are called SUBFUNCTIONS - they are NOT visible to "outside functions" - they are strictly visible to the first function and other functions in the same M-File example) function x = blah(a,b) x = blah2(a,b); >> blah(1:2,2:3) >> blah2(1:2,2:3) function y = blah2(a,b) % subfunction of blah y = 2.*a+b ------------------------------------------------------------------------------- (4) Function function: + function with an input argument the name(s) of other function(s) ex) ezplot('cos(x)') ezplot plots function without having you to figure out other inputs + usually input names are strings function demofunfun % DEMOFUNFUN demonstration of function functions disp('Starting demo of function functions...'); name = input('Enter the name of a function as a STRING: '); % example: user enters $'cos'$ plotf(name,linspace(-10,10,10000)); function plotf(name,x) % evaluate the function with name $name$ with input $x$ % $feval$ evaluates a function with given inputs y = feval(name,x); % could also use less efficient $eval(string)$ which % evaluates $string$ as a script command % now do something interesting with the values plot(x,y); ------------------------------------------------------------------------------- (5) Recursion: + function calls itself (Yes, MATLAB and other languages can do that!) example) function result = rsum(n) if n==1 result = 1; else result = n + rsum(n-1); end % Actually this case handled much quicker by MATLAB itself % sum(1:n) % could also do loop ------------------------------------------------------------------------------- (6) Reminders/Advice + How to know when to use a function? - verb/action in problem statement - task (or related group of tasks) that gets repeated often + How to write? - decide on inputs, returns, tasks - test individually with dummy inputs -------------------------------------------------------------------------------