0
0
MATLABdata~10 mins

Function definition syntax 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 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'
Aadd
Bsum
Cplus
Dcalculate
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than the one intended.
Forgetting to write the function name after 'function'.
2fill in blank
medium

Complete 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'
Asquare
Bpower
CsquareRoot
Dmultiply
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.
3fill in blank
hard

Fix 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'
Ahello
Bgreeting
CsayHello
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than 'greet'.
Omitting the function keyword or parentheses.
4fill in blank
hard

Fill 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'
Aresult
Ba
Coutput
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names between function header and body.
Forgetting to name the output variable.
5fill in blank
hard

Fill 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'
AmaxVal
Bx
Cy
DmaxValue
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.