1 nargin
nargin 用于调控function的执行中的变量的输入个数
用于判定输入变量的个数,从而在不同变量个数的 状况下实现不同的 功能
因此一般在function里面会设置好默认值
只有一个输出变量,和多个输入变量
如果输入变量只有a个,那么在run这个function的时候,只能run的变量必须是a个或者少于a个,但不能多于a个,这样matlab是跑不了的
例子:
function c = kk(a,b)
switch nargin
case 2
c = a + b;
case 1
c = a*2;
otherwise
c = 0;
end
case 2
c = a + b;
case 1
c = a*2;
otherwise
c = 0;
end
2 nargout
和nargin的作用相似,但是是调节输出的变量的
function [dif,absdif] = subtract(y,x)
dif = y - x;
if nargout < 0
disp('Calculating absolute value')
absdif = abs(dif);
end
disp('Calculating absolute value')
absdif = abs(dif);
end
end