Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a function named 'add' that takes two inputs.
MATLAB
function result = [1](a, b)
result = a + b;
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than the one intended.
Forgetting to write the function name after 'function'.
✗ Incorrect
The function name must match the name after the 'function' keyword. Here, 'add' is the correct function name.
2fill in blank
mediumComplete the code to define a function that returns the square of input x.
MATLAB
function y = [1](x) y = x^2; end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a misleading function name like 'squareRoot' when squaring.
Choosing a generic name like 'multiply' that doesn't specify the operation.
✗ Incorrect
The function name 'square' clearly indicates it returns the square of x.
3fill in blank
hardFix the error in the function header to correctly define a function named 'greet' with input 'name'.
MATLAB
function [1](name) disp(['Hello, ', name, '!']); end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than 'greet'.
Omitting the function keyword or parentheses.
✗ Incorrect
The function name must be 'greet' to match the intended function definition.
4fill in blank
hardFill both blanks to define a function 'multiply' that takes inputs 'a' and 'b' and returns their product.
MATLAB
function [1] = multiply([2], b) [1] = a * b; end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names between function header and body.
Forgetting to name the output variable.
✗ Incorrect
The output variable is 'result' and the first input variable is 'a'.
5fill in blank
hardFill all three blanks to define a function 'maxValue' that returns the maximum of two inputs 'x' and 'y'.
MATLAB
function [1] = maxValue([2], [3]) if [2] > [3] [1] = [2]; else [1] = [3]; end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for output and function name.
Mismatching input variable names in the function header and body.
✗ Incorrect
The output variable is 'maxVal', inputs are 'x' and 'y', and the function compares x and y to return the maximum.