Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nargin vs exist

Tags:

Given a function like:

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

I've seen people use something like the following in the place of the nargin version

if ~exist('myParam', 'var')
  myParam = 'default value';
end %if

I'm wondering if there is any preference either way?

The "~exist..." version to me has the advantage that if I change the order of my function parameters then it should still work. However my concern with this approach is that I might inadvertently pick up variables that are defined globally or in the scope of a surrounding function in the case of nested functions.

Any thoughts on the issue?