0
0
MATLABdata~10 mins

Why functions organize MATLAB code - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to use an operator.
2fill in blank
medium

Complete 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'
AsumNumbers
BaddNumbers
Cadd
DsumAdd
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong or misspelled function name.
Confusing function name with variable names.
3fill in blank
hard

Fix 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'
Afunction
Bdef
Cfunc
Dfun
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python or other language keywords like def.
Omitting the keyword entirely.
4fill in blank
hard

Fill 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'
Aresult
Bnum
Cvalue
Doutput
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for input and output variables.
Forgetting to use the power operator ^.
5fill in blank
hard

Fill 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'
Aresult
Bnum
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Mixing up input and output variable names.