Functions and Flow Control Flashcards
7 cards from real MATLAB practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Functions and Flow Control flashcards as text
What is a recursive function in MATLAB?
Answer: A function that calls itself
A recursive function calls itself within its own body, typically with a simpler input, until a base case is reached.
Which of the following correctly implements factorial recursively in MATLAB?
Answer: function n = fact(n); if n>1; n = n * fact(n-1); end; end
The correct recursive factorial checks for the base case (n<=1 returns 1) and multiplies n by fact(n-1).
What is a local function in MATLAB?
Answer: A helper function defined after the main function in the same .m file
Local functions are additional functions defined at the bottom of a script or function file and are only accessible within that file.
Which MATLAB statement evaluates a block of code only if all previous `elseif` and `if` conditions were false?
Answer: else
The `else` clause runs when no preceding `if` or `elseif` condition evaluated to true.
What is `cellfun` used for in MATLAB?
Answer: Apply a function to each element of a cell array
`cellfun(func, C)` applies the function `func` to each element of cell array `C` and returns the results.
In MATLAB, what happens if a `switch` expression matches multiple `case` values listed in a cell array like `case {1, 2}`?
Answer: The case block executes if the expression equals any value in the cell array
When a case uses a cell array, MATLAB executes that block if the switch expression matches any value in the cell array.
What is the key difference between `break` and `return` in MATLAB?
Answer: `break` exits the innermost loop; `return` exits the current function
`break` only exits the innermost loop construct, while `return` exits the entire current function and returns to the caller.