function [z1, z2] = addsub (x, y) % only one returned variable % function z = addsub (x, y) % Alternative function form % ---------------------------------------------------------------------- % Usage: [sum, difference] = addsub (x, y) (Note the comma or blank space.) % Usage: [sum difference] = addsub (x, y) % % This function adds and substracts two numbers/matrices given in the argument % Demonstrate how to pass and output multiple arguments. % Calling Example #1: a=addsub(1,2) ... gives a=3 % Calling Example #2: [a b]=addsub(1,2) ... gives a=3 & b=-1 %----------------------------------------------------------------------- % Instructor: Nam Sun Wang %----------------------------------------------------------------------- % The first blank line above signifies the end point for help. % More comments go here, but this line will not be displayed when you issue % "help" from MATLAB. z1 = x + y; z2 = x - y; % An example to check for the number of arguments in the output in the calling % statement so that if the calling line has only one output (as in Calling % Example #1), there is no sense calculating the second variable. % if (nargout==2) z2 = x - y; end % The following is for the alternative function form % z = [z1 z2]; % No "end" or "end function" statement is needed as each function must be % compleletely contained in a separate .M file.