## Varargout - How to realize function call

### Dario Walter (view profile)

on 21 Oct 2019 at 13:43
Latest activity Answered by Dario Walter

### Dario Walter (view profile)

on 1 Nov 2019 at 19:28

### Stephen Cobeldick (view profile)

Hey guys,
let us assume the following function:
function [out1,out2,out3]=test(j,k)
out1= nargin;
out2 = j-k;
if out2 == 10
out3 = 5;
end
end
The function call [val1,val2,val3] = test(10,11) returns an error because c is obviously undefined. How do I make this code run so that out3 is returned when it exists?

Guillaume

### Guillaume (view profile)

on 21 Oct 2019 at 15:15
The function call [val1,val2,val3] = test(10,11)
will always require 3 outputs from the function, so test must always return at least 3 outputs for that call to be valid.
That's independent of whether or not the function only returns some outputs under some conditions.

### Products ### Stephen Cobeldick (view profile)

on 21 Oct 2019 at 16:38

Simpler:
function [out1,out2,out3]=test(j,k)
out1= nargin;
out2 = j-k;
out3 = [];
...
if out2 == 10
out3 = 5;
end
end

### per isakson (view profile)

on 21 Oct 2019 at 14:31
Edited by per isakson

### per isakson (view profile)

on 21 Oct 2019 at 18:07

varargout exercise. Try something like this
function varargout = test( j, k )
out1= nargin;
out2 = j-k;
if out2 == 10
out3 = 5;
end
if exist( 'out3', 'var' ) == 1
varargout = { out1, out2, out3 };
else
varargout = { out1, out2, [] };
end
end
or
function varargout = test( j, k )
out1= nargin;
out2 = j-k;
if out2 == 10
out3 = 5;
else
out3 = [];
end
varargout = { out1, out2, out3 };
end