Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a simple function that adds two numbers.
MATLAB
function result = addNumbers(a, b)
result = a [1] b;
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to use an operator.
✗ Incorrect
The plus sign + adds two numbers in MATLAB.
2fill in blank
mediumComplete the code to call the function 'addNumbers' with inputs 3 and 5.
MATLAB
sum = [1](3, 5);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong or misspelled function name.
Confusing function name with variable names.
✗ Incorrect
The function is named addNumbers, so calling it requires that exact name.
3fill in blank
hardFix the error in the function definition by completing the missing keyword.
MATLAB
[1] addNumbers(a, b)
result = a + b;
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python or other language keywords like
def.Omitting the keyword entirely.
✗ Incorrect
In MATLAB, functions start with the keyword function.
4fill in blank
hardFill both blanks to create a function that returns the square of a number.
MATLAB
function [1] = squareNumber([2]) [1] = [2]^2; end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for input and output variables.
Forgetting to use the power operator
^.✗ Incorrect
The function output variable is result and the input variable is num.
5fill in blank
hardFill all three blanks to create a function that checks if a number is positive.
MATLAB
function [1] = isPositive([2]) if [2] [3] 0 [1] = true; else [1] = false; end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Mixing up input and output variable names.
✗ Incorrect
The function returns result, takes input num, and checks if num > 0.