Not enough input arguments. Am I declaring local functions properly?
1 view (last 30 days)
Show older comments
When I run the m code I get a not enough input arguments error. I have several local functions. Am I correct in putting any variables produced with the function[] in the square brackets and any variables called from other functions in round brackets after the functions name?
1 Comment
Star Strider
on 1 Apr 2014
That seems correct syntax but would not throw a ‘not enough input arguments’ error.
Answers (1)
Walter Roberson
on 1 Apr 2014
Local functions are the same as non-local functions in their syntax: the names you put in the "function" line are the names the function will use to refer to values, but the values need to be passed in by the caller. For example if you have
function main
L = 32;
function R = sqr(L)
R = L .* L;
end
disp(sqr(L))
end
then the "L" inside "sqr" is the local name for whatever gets passed as the first argument, and is not a reference to the "L" that exists in "main". Any variable named in a "function" line overrides use of that name coming from elsewhere.
You could, though, have
function main
L = 32;
function R = sqr(P)
R = P .* L;
end
disp(sqr(5))
end
in that, the "P" of "sqr" would refer to the parameter passed in, e.g., 5, and the "L" in "sqr" would search upwards in nesting context to find the variable in "main" (i.e., value 32) -- such a search only works in local functions, including anonymous functions.
0 Comments
See Also
Categories
Find more on Function Definition in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!