Confused with the switch-case outputs with nargin

22 views (last 30 days)
I am a bit confued about the nargin function's behavior.
For the first example: narginExample(2, 5); ans = 7
Second example: narginExample(2, 5); ans = 4
My question is:
  1. Why the function chooses the second case to execute?
  2. What is the use of "otherwise"? I was assuming, when I pass 3 arguments (or no arguments), it would be considered as the "otherwise" case. But it's not.
% Example 1
function x = narginExample(y, z)
switch nargin
case 2
x = y + z;
case 1
x = y + y;
otherwise
x = x;
end
end
% Example 2
function x = narginExample(y, z)
switch nargin
case 1
x = y + z;
case 2
x = y + y;
otherwise
x = x;
end
end
  12 Comments
per isakson
per isakson on 27 Nov 2019
R2018b ignores x=x; and throws an error for x=x

Sign in to comment.

Answers (3)

Stephen23
Stephen23 on 30 Aug 2019
Edited: Stephen23 on 30 Aug 2019
"Why the function chooses the second case to execute?"
Because that is exactly what you wrote your functions to do.
You call both of your functions with two input arguments. That means nargin==2. That means that case 2 will run. So this is the code that runs when nargin==2:
1st function:
case 2
x = y + z;
2nd function:
case 2
x = y + y;
Your example input arguments are y=2 and z=5. So:
1st function: what does y+z equal? (hint: 7)
2nd function: what does y+y equal? (hint: 4)
"What is the use of "otherwise"?
otherwise runs when none of the cases run, exactly as the switch documentation states.
"...when I pass 3 arguments (or no arguments), it would be considered as the "otherwise" case. But it's not."
Lets look at those two situations:
  • If you pass zero input arguments then otherwise will run, but in both of your functions you try to access x which is undefined at that point, and so an error will be thrown.
  • If you pass more than two input arguments an error will be thrown because you defined the function signature with two input arguments (i.e. y and z). MATLAB knows that you only defined the inputs y and z, so there is no point in it trying to guess what to do with extra input arguments that your function was not written to handle.
"I am a bit confued about the nargin function's behavior."
I don't see that - you seem to be much more confused about switch and how functions work.

Steven Lord
Steven Lord on 30 Aug 2019
Why the function chooses the second case to execute?
Because you called the function with two inputs. MATLAB first checks the case 1 case but nargin is not equal to 1, so that case is skipped. Next it checks the case 2 case and since nargin is equal to 2, MATLAB executes the body of that case.
One key point to know, if you're familiar with other languages, is that cases in a switch / case do not "fall through". At most one case in a switch / case block will be executed.
What is the use of "otherwise"? I was assuming, when I pass 3 arguments (or no arguments), it would be considered as the "otherwise" case. But it's not.
The otherwise case would have been executed, if you called your function with 3 arguments and MATLAB had reached that point in your code. But the way you've defined your function:
function x = narginExample(y, z)
tells MATLAB that you will call this function with at most two input arguments. If you try to call it with 3, MATLAB has no "place" to assign that third input argument, and so it throws an error before even trying to execute any of the lines of code inside the function.
Calling this function with 0 input arguments would reach your otherwise case. Just because you've defined your function to accept one or more input arguments doesn't mean you have to call it with that many. You can call it with fewer.
You can write a function that accepts an arbitrary number of inputs, if you don't want to limit it by how many variable names you want to type in your function definition. To do that, use varargin as the last input argument in your function definition. That identifier is handled differently than any other identifier. See the "varargin and Declared Inputs" example on that documentation page. Even though the definition "looks like" it only accepts three inputs, because it accepts varargin you can call it with five inputs and any past the second get stored in the varargin cell array.

Enrico Anderlini
Enrico Anderlini on 30 Aug 2019
nargin tells you how many inputs your function has.
So, both examples will give you nargin = 2 because you are always entering two function inputs. So case=2 for both examples.
But because you change the order of the cases, in the first example, you have x = y + z and in the second case x = y + y.
Otherwise will work if you call the function with 0 or 3+ inputs. However, the function will return an error in both examples because x is not defined.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!