Local functions in MATLAB - Time & Space Complexity
When using local functions in MATLAB, it's important to see how their execution time grows as input size changes.
We want to know how calling a local function inside a main function affects overall running time.
Analyze the time complexity of the following code snippet.
function result = mainFunction(n)
total = 0;
for i = 1:n
total = total + localAdd(i);
end
result = total;
function y = localAdd(x)
y = x + 1;
end
end
This code sums numbers from 1 to n, adding 1 to each number using a local function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop runs from 1 to n, calling the local function each time.
- How many times: The local function is called exactly n times, once per loop iteration.
As n grows, the number of times the local function runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calls to localAdd |
| 100 | About 100 calls to localAdd |
| 1000 | About 1000 calls to localAdd |
Pattern observation: The work grows directly with n; doubling n doubles the calls.
Time Complexity: O(n)
This means the total time grows in a straight line as the input size increases.
[X] Wrong: "Local functions run only once, so they don't affect time much."
[OK] Correct: Even though the function is local, it runs every time it is called inside the loop, so its cost adds up with n.
Understanding how local functions impact time helps you explain code efficiency clearly and shows you can analyze function calls inside loops.
"What if the local function itself contained a loop running n times? How would the time complexity change?"
Practice
local function in MATLAB?Solution
Step 1: Understand local function placement
Local functions are defined inside the same file as the main function or script, not separately.Step 2: Compare with other options
Options A, B, and C describe behaviors that do not match local functions in MATLAB.Final Answer:
It is defined inside the same file as the main function or script. -> Option DQuick Check:
Local function location = same file [OK]
- Thinking local functions need separate files
- Believing local functions run automatically
- Assuming local functions can only be called from command window
Solution
Step 1: Identify MATLAB function syntax
MATLAB local functions start with the keywordfunction, followed by output, name, and input, and end withend.Step 2: Check each option
function y = localFunc(x) y = x^2; end uses correct MATLAB syntax withfunctionandend. def localFunc(x): return x^2 is Python syntax. localFunc(x) = x^2; is invalid MATLAB syntax. function y = localFunc(x) y = x^2; misses theendkeyword.Final Answer:
function y = localFunc(x) y = x^2; end -> Option CQuick Check:
MATLAB function syntax = function y = localFunc(x) y = x^2; end [OK]
- Omitting the 'end' keyword
- Using Python or other language syntax
- Trying to assign function like a variable
mainFile.m:
function result = mainFile(x)
result = squarePlusOne(x);
function y = squarePlusOne(n)
y = n^2 + 1;
end
end
What is the return value of mainFile(3)?Solution
Step 1: Understand the local function call
The main function callssquarePlusOne(3). The local function computes3^2 + 1 = 9 + 1 = 10.Step 2: Confirm output assignment
The result from the local function is assigned toresult, which is returned bymainFile. SomainFile(3)returns10.Final Answer:
10 -> Option AQuick Check:
3 squared plus 1 = 10 [OK]
- Forgetting to add 1 after squaring
- Thinking local functions are inaccessible
- Confusing variable names
function output = calc(x)
output = doubleValue(x);
function y = doubleValue(n)
y = n * 2
end
result = calc(5);
What is the error in this code?Solution
Step 1: Check function endings
The local functiondoubleValuehas anend, but the main functioncalcdoes not have its ownendkeyword.Step 2: Verify MATLAB syntax rules
Each function in MATLAB must end withend. Missingendforcalccauses a syntax error.Final Answer:
Missingendkeyword for the main functioncalc-> Option BQuick Check:
Every function needs 'end' [OK]
- Assuming semicolon omission causes error
- Thinking local functions are inaccessible
- Believing local functions can't return values
Solution
Step 1: Identify correct local function usage
function f = factorialMain(n) f = helper(n); function val = helper(k) if k <= 1 val = 1; else val = k * helper(k-1); end end end defines the local functionhelperinsidefactorialMainand uses recursion properly with base case.Step 2: Check other options for errors
function f = factorialMain(n) f = helper(n); end function val = helper(k) if k <= 1 val = 1; else val = k * helper(k-1); end defineshelperoutside main function (not local). function f = factorialMain(n) f = factorial(n); function val = helper(k) val = k * helper(k-1); end end callsfactorial(built-in) and has incomplete helper. function f = factorialMain(n) f = n * factorialMain(n-1); end is a recursive main function but lacks base case and local function.Final Answer:
function f = factorialMain(n) f = helper(n); function val = helper(k) if k <= 1 val = 1; else val = k * helper(k-1); end end end -> Option AQuick Check:
Local recursive helper inside main function = function f = factorialMain(n) f = helper(n); function val = helper(k) if k <= 1 val = 1; else val = k * helper(k-1); end end end [OK]
- Defining helper function outside main function
- Missing base case in recursion
- Calling built-in factorial instead of local helper
Ready to prove it?
Pass the test and this pattern moves unknown
