Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a function that takes one input argument.
MATLAB
function result = square[1] result = x^2; end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] instead of parentheses for inputs.
Using curly braces {} which are for cell arrays, not inputs.
✗ Incorrect
In MATLAB, input arguments are enclosed in parentheses ().
2fill in blank
mediumComplete the code to define a function that returns two output arguments.
MATLAB
function [sum, diff] = addAndSubtract[1]
sum = a + b;
diff = a - b;
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets for input arguments.
Using curly braces or angle brackets which are invalid.
✗ Incorrect
Input arguments are always enclosed in parentheses (). Output arguments are enclosed in square brackets [].
3fill in blank
hardFix the error in the function definition to correctly specify output arguments.
MATLAB
function [1] = addAndSubtract(a, b)
sum = a + b;
diff = a - b;
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using square brackets for multiple outputs.
Placing output variables outside brackets or parentheses.
✗ Incorrect
Output arguments must be enclosed in square brackets before the equals sign.
4fill in blank
hardFill both blanks to create a function that returns the square and cube of input x.
MATLAB
function [[1], [2]] = powers(x) [1] = x^2; [2] = x^3; end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names that don't match the assignments inside the function.
Mixing up the order of outputs.
✗ Incorrect
The output variables should be named square and cube to match the assignments inside the function.
5fill in blank
hardFill all three blanks to define a function that returns the sum, product, and difference of two inputs.
MATLAB
function [[1], [2], [3]] = calc(a, b) [1] = a + b; [2] = a * b; [3] = a - b; end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic names like result instead of descriptive output names.
Mixing up the order of output variables.
✗ Incorrect
Output variables must be sum, prod, and diff to match the calculations inside the function.