nargin vs exist

前端 未结 6 2047
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-30 06:46

Given a function like:

function foo(myParam)
if nargin<1
  myParam = \'default value\';
end % if
end % function

I\'ve seen people use so

6条回答
  •  渐次进展
    2020-12-30 07:33

    I would go with nargin for two reasons:

    1. If you change the order of parameters to your function, fixing up the input checking code is going to be the least of your problems; you're going to have to go update all the call sites to your function as well.

    2. nargin is far cheaper. exist, even if scoped to just check for variables, has to scan the whole workspace doing a bunch of string comparisons along the way. the nargin method consists of just a scalar less than operation.

提交回复
热议问题