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 MATLAB anonymous function?
Answer: A short function defined inline using the @() syntax
Anonymous functions use the `@(args) expression` syntax and are defined inline without a separate file.
Which of the following correctly creates an anonymous function that squares its input?
Answer: sq = @(x) x^2
The anonymous function syntax is `@(input_args) expression`, so `@(x) x^2` is correct.
What does `nargout` return inside a MATLAB function?
Answer: Number of output values requested by the caller
`nargout` returns how many output arguments the caller requested when invoking the function.
In MATLAB, what is a nested function?
Answer: A function defined inside another function's body
A nested function is defined within the body of another (parent) function and shares its workspace.
How does MATLAB handle a `while` loop when the condition is false from the start?
Answer: Never executes the loop body
If the while condition is false initially, MATLAB skips the loop body entirely without executing it.
Which statement correctly ends a function definition in a MATLAB function file containing multiple functions?
Answer: end
The `end` keyword closes a function definition when multiple functions share one file.
What is the purpose of `varargin` in a MATLAB function definition?
Answer: Specifies a variable number of input arguments
`varargin` allows a function to accept a variable number of input arguments, collected into a cell array.