Challenge - 5 Problems
Varargin Varargout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a function using varargin
What is the output of the following MATLAB function call?
function result = sumAll(varargin)
result = 0;
for k = 1:length(varargin)
result = result + varargin{k};
end
end
output = sumAll(2, 4, 6); disp(output);MATLAB
function result = sumAll(varargin)
result = 0;
for k = 1:length(varargin)
result = result + varargin{k};
end
end
output = sumAll(2, 4, 6); disp(output);Attempts:
2 left
💡 Hint
Remember varargin is a cell array containing all input arguments.
✗ Incorrect
The function sums all input arguments passed via varargin. Each varargin{k} is a number, so the sum is 2 + 4 + 6 = 12.
❓ Predict Output
intermediate2:00remaining
Output when using varargout to return multiple outputs
What will be the output of the following MATLAB code?
function varargout = splitNumber(x)
varargout{1} = floor(x);
varargout{2} = x - varargout{1};
end
[a, b] = splitNumber(5.75);
disp([a, b]);MATLAB
function varargout = splitNumber(x)
varargout{1} = floor(x);
varargout{2} = x - varargout{1};
end
[a, b] = splitNumber(5.75);
disp([a, b]);Attempts:
2 left
💡 Hint
varargout allows returning multiple outputs as a cell array.
✗ Incorrect
The function returns the integer part and fractional part of the input number. floor(5.75) is 5, fractional part is 0.75.
🧠 Conceptual
advanced2:00remaining
Understanding varargin behavior with no inputs
Consider the function below:
What is the output of this code?
function count = countInputs(varargin)
count = length(varargin);
end
result = countInputs(); disp(result);What is the output of this code?
MATLAB
function count = countInputs(varargin)
count = length(varargin);
end
result = countInputs(); disp(result);Attempts:
2 left
💡 Hint
Think about what varargin contains when no inputs are given.
✗ Incorrect
When no inputs are passed, varargin is an empty cell array, so length(varargin) is 0.
❓ Predict Output
advanced2:00remaining
Output of function with mixed varargin and varargout
What is the output of the following MATLAB code?
function varargout = processData(varargin)
n = length(varargin);
varargout{1} = n;
if n > 0
varargout{2} = varargin{1} * 2;
else
varargout{2} = 0;
end
end
[a, b] = processData(3, 5, 7);
disp([a, b]);MATLAB
function varargout = processData(varargin)
n = length(varargin);
varargout{1} = n;
if n > 0
varargout{2} = varargin{1} * 2;
else
varargout{2} = 0;
end
end
[a, b] = processData(3, 5, 7);
disp([a, b]);Attempts:
2 left
💡 Hint
varargin contains all inputs; varargout returns outputs based on inputs.
✗ Incorrect
The function returns the number of inputs (3) and twice the first input (3*2=6).
🔧 Debug
expert2:00remaining
Identify the error in using varargin and varargout
What error will the following MATLAB function produce when called as
myFunc(1,2)?function varargout = myFunc(varargin)
varargout{1} = varargin{1} + varargin{2};
varargout{2} = varargin{3};
endMATLAB
function varargout = myFunc(varargin)
varargout{1} = varargin{1} + varargin{2};
varargout{2} = varargin{3};
end
myFunc(1,2);Attempts:
2 left
💡 Hint
Check how many inputs are passed and what varargin contains.
✗ Incorrect
The function tries to access varargin{3} but only two inputs are given, causing an index error.