0
0
MATLABdata~10 mins

Multiple outputs in MATLAB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function that returns two outputs.

MATLAB
function [a, b] = myFunc(x)
    a = x + 1;
    b = x[1]2;
end
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' will add 2 instead of doubling.
Using '/' will divide x by 2, which is incorrect here.
2fill in blank
medium

Complete the code to call the function and get both outputs.

MATLAB
[out1, [1]] = myFunc(5);
Drag options to blanks, or click blank then click option'
Aout2
Bresult
Coutput
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one output variable will ignore the second output.
Using a variable name that does not match the function outputs can cause confusion.
3fill in blank
hard

Fix the error in the function definition to correctly return two outputs.

MATLAB
function [1] = myFunc(x)
    a = x + 1;
    b = x * 2;
end
Drag options to blanks, or click blank then click option'
A[a, b]
B<a, b>
C{a, b}
D(a, b)
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or braces instead of square brackets causes syntax errors.
Omitting brackets leads to only one output being returned.
4fill in blank
hard

Fill both blanks to create a function that returns the sum and product of two inputs.

MATLAB
function [[1], [2]] = sumProd(x, y)
    [1] = x + y;
    [2] = x * y;
end
Drag options to blanks, or click blank then click option'
As
Bp
Csum
Dprod
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sum' or 'prod' as variable names can conflict with MATLAB functions.
Mismatching output names in the function signature and body causes errors.
5fill in blank
hard

Fill all three blanks to call the function and display both outputs.

MATLAB
[[1], [2]] = sumProd(3, 4);
disp(['Sum: ', num2str([3])]);
Drag options to blanks, or click blank then click option'
AsumVal
BprodVal
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable in disp causes incorrect output display.
Not converting number to string before concatenation causes errors.