0
0
MATLABdata~20 mins

Input and output arguments in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Input and Output Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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]);
A[3 6]
B[4 6]
C[4 3]
D[6 4]
Attempts:
2 left
💡 Hint
Remember the function returns two outputs: a = x+1 and b = x*2.
Predict Output
intermediate
2: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;
A5
B16
C17
D8
Attempts:
2 left
💡 Hint
Calculate 4 squared plus 1.
🔧 Debug
advanced
2: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);
AError: Variable 'b' is undefined.
BError: Function syntax error.
CNo error, outputs are [7, 0].
DError: Not enough output arguments.
Attempts:
2 left
💡 Hint
Check if the function returns all requested outputs.
📝 Syntax
advanced
2:00remaining
Correct syntax for multiple output arguments
Which option shows the correct syntax to define a function with two output arguments in MATLAB?
Afunction [out1, out2] = myFunc(input)
Bfunction (out1, out2) = myFunc(input)
Cfunction out1 out2 = myFunc(input)
Dfunction out1, out2 = myFunc(input)
Attempts:
2 left
💡 Hint
Look for square brackets around output variables.
🚀 Application
expert
2: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);
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
The number of outputs depends on the left side of the call.