Script and Function Creation 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 Script and Function Creation flashcards as text
What is the purpose of the `end` keyword at the close of a function definition in MATLAB?
Answer: It marks the boundary of the function so local functions can follow
Using `end` to close a function is required when the file contains more than one function, to delineate each function's scope.
Which of the following correctly calls a function named `calcArea` with input `5` and captures its output?
Answer: result = calcArea(5)
Standard MATLAB function call syntax assigns the returned value using the equals sign.
Anonymous functions in MATLAB are created using which syntax?
Answer: f = @(x) x^2
The `@(args) expression` syntax defines an anonymous function inline without a separate file.
What is the difference between a MATLAB script and a function regarding variable scope?
Answer: Scripts share the base workspace; functions have their own private workspace
Functions execute in their own workspace, isolating their variables from the base workspace, while scripts do not.
How do you add a persistent variable inside a MATLAB function so it retains its value between calls?
Answer: persistent varName
`persistent` declares a variable that retains its value between successive calls to the function.
What does `nargout` allow a function to determine?
Answer: How many output arguments the caller requested
`nargout` returns the number of output arguments requested by the calling expression, enabling conditional output generation.
Which file extension is required for MATLAB function and script files?
Answer: .m
Standard MATLAB scripts and functions are saved with the `.m` extension; `.mat` is for data, `.mlx` is for live scripts.