Challenge - 5 Problems
Input and Output Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a function with multiple outputs
What is the output of the following MATLAB code?
MATLAB
function [a, b] = example(x)
a = x + 1;
b = x * 2;
end
[a_out, b_out] = example(3);
disp([a_out, b_out]);Attempts:
2 left
💡 Hint
Remember the function returns two outputs: a = x+1 and b = x*2.
✗ Incorrect
The function example adds 1 to input x for output a, and multiplies x by 2 for output b. For x=3, a=4 and b=6, so the output is [4 6].
❓ Predict Output
intermediate2:00remaining
Value of output argument after function call
What is the value of variable y after running this code?
MATLAB
function y = squarePlusOne(x)
y = x^2 + 1;
end
result = squarePlusOne(4);
y = result;Attempts:
2 left
💡 Hint
Calculate 4 squared plus 1.
✗ Incorrect
4 squared is 16, plus 1 equals 17, so y is 17.
🔧 Debug
advanced2:00remaining
Identify the error in function output assignment
What error will this MATLAB code produce when run?
MATLAB
function [a, b] = faultyFunc(x)
a = x + 2;
end
[a_out, b_out] = faultyFunc(5);Attempts:
2 left
💡 Hint
Check if the function returns all requested outputs.
✗ Incorrect
The function defines two outputs but only assigns one. Calling with two outputs causes 'Not enough output arguments' error.
📝 Syntax
advanced2:00remaining
Correct syntax for multiple output arguments
Which option shows the correct syntax to define a function with two output arguments in MATLAB?
Attempts:
2 left
💡 Hint
Look for square brackets around output variables.
✗ Incorrect
MATLAB requires square brackets around multiple outputs in function definition: function [out1, out2] = myFunc(input)
🚀 Application
expert2:00remaining
Number of output arguments returned by a function call
Given the function below, how many output arguments does the call [x, y] = computeValues(2) return?
MATLAB
function [a, b, c] = computeValues(n)
a = n;
b = n^2;
c = n^3;
end
[x, y] = computeValues(2);Attempts:
2 left
💡 Hint
The number of outputs depends on the left side of the call.
✗ Incorrect
Even though the function returns 3 outputs, the call requests only 2 outputs, so only 2 are returned.