Location of the "end" statement of the main function in a m. file?
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Marek Gradzki
il 7 Ago 2020
Commentato: Marek Gradzki
il 7 Ago 2020
Hello,
could someone write what is the difference between possible location of the "end" statement of the main function: before first local function vs end of a file? What is the difference?
Thank you!
Marek
0 Commenti
Risposta accettata
Stephen23
il 7 Ago 2020
Modificato: Stephen23
il 7 Ago 2020
"before first local function"
any functions defined after the main function are local functions:
"vs end of a file?"
any functions defined within the main function are nested functions:
These can be combined too, e.g.:
function mymain()
... code
function mynest()
... code
end % mynest()
end % mymain()
function mylocal()
... code
end % mylocal()
Nested functions have access to the parent function's workspace. Local functions do not.
Più risposte (1)
Sudheer Bhimireddy
il 7 Ago 2020
3 Commenti
Bruno Luong
il 7 Ago 2020
Modificato: Bruno Luong
il 7 Ago 2020
function caller
a = 1;
function c = foo(d)
eval('b = d;'); % error "forbidden"
c = a + b;
end
function c = bar(d)
b = [];
eval('b = d;'); % OK
c = a + b;
end
fprintf('bar(2) = %g\n', bar(2));
fprintf('foo(2) = %g\n', foo(2)); % this won't run
end
Vedere anche
Categorie
Scopri di più su Environment and Settings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!