Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The function returns two outputs: 'a' is x plus 1, and 'b' is x multiplied by 2.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The function returns two outputs, so we capture the second output in 'out2'.
3fill in blank
hardFix 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'
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.
✗ Incorrect
In MATLAB, multiple outputs are enclosed in square brackets in the function definition.
4fill in blank
hardFill 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'
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.
✗ Incorrect
The function returns two outputs named 's' for sum and 'p' for product.
5fill in blank
hardFill 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'
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.
✗ Incorrect
We assign outputs to 'sumVal' and 'prodVal', then display the sum using 'sumVal'.