0
0
MATLABdata~20 mins

Variable arguments (varargin, varargout) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Varargin Varargout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A12
B246
CError: Undefined function or variable 'varargin'
D0
Attempts:
2 left
💡 Hint
Remember varargin is a cell array containing all input arguments.
Predict Output
intermediate
2: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]);
A[5 0.75]
B[6 0.25]
CError: Too many output arguments.
D[5.75 0]
Attempts:
2 left
💡 Hint
varargout allows returning multiple outputs as a cell array.
🧠 Conceptual
advanced
2:00remaining
Understanding varargin behavior with no inputs
Consider the function below:

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);
AError: varargin is undefined
B1
C0
DNaN
Attempts:
2 left
💡 Hint
Think about what varargin contains when no inputs are given.
Predict Output
advanced
2: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]);
A[3 10]
BError: Index exceeds array bounds.
C[7 14]
D[3 6]
Attempts:
2 left
💡 Hint
varargin contains all inputs; varargout returns outputs based on inputs.
🔧 Debug
expert
2: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};
end
MATLAB
function varargout = myFunc(varargin)
    varargout{1} = varargin{1} + varargin{2};
    varargout{2} = varargin{3};
end

myFunc(1,2);
ANo error, returns [3, []]
BIndex exceeds array bounds because varargin{3} does not exist
CSyntax error: Missing semicolon
DError: varargout must have at least one output argument
Attempts:
2 left
💡 Hint
Check how many inputs are passed and what varargin contains.