Challenge - 5 Problems
MATLAB Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple MATLAB function call
What is the output of the following MATLAB code when the function
squareIt is called with input 4?MATLAB
result = squareIt(4); disp(result); function y = squareIt(x) y = x^2; end
Attempts:
2 left
💡 Hint
Remember that the function squares the input number.
✗ Incorrect
The function
squareIt takes input x and returns x^2. So, calling it with 4 returns 16.🧠 Conceptual
intermediate1:30remaining
Why use functions in MATLAB code?
Which of the following is the main reason to organize MATLAB code using functions?
Attempts:
2 left
💡 Hint
Think about how functions help when you write big programs.
✗ Incorrect
Functions help organize code by making it reusable and easier to understand, which is important for managing complexity.
🔧 Debug
advanced2:00remaining
Identify the error in this MATLAB function
What error will this MATLAB code produce when run?
MATLAB
result = addNumbers(3, 5); function y = addNumbers(a, b) y = a + b; end
Attempts:
2 left
💡 Hint
Check if missing semicolons cause errors or just suppress output.
✗ Incorrect
In MATLAB, missing semicolons do not cause errors; they just print output. The function runs correctly and returns 8.
❓ Predict Output
advanced2:30remaining
Output of nested function calls in MATLAB
What is the output of the following MATLAB code?
MATLAB
result = addThreeAndDouble(4); disp(result); function y = multiplyByTwo(x) y = x * 2; end function z = addThreeAndDouble(x) z = multiplyByTwo(x + 3); end
Attempts:
2 left
💡 Hint
First add 3 to 4, then multiply by 2.
✗ Incorrect
The inner call adds 3 to 4 = 7, then multiplyByTwo doubles 7 to 14.
🧠 Conceptual
expert2:00remaining
Why is function scope important in MATLAB?
Why does MATLAB use function scope to organize variables inside functions?
Attempts:
2 left
💡 Hint
Think about how variables inside functions do not affect variables outside.
✗ Incorrect
Function scope means variables inside a function do not interfere with variables outside, which helps avoid mistakes and keeps code clean.