How do I get a main function to recognize all variables in a function?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am stuck on this problem. How do I get the main function to display d?
function main(d)
a=1
b=2
disp(d)
function Math1
c=a+b
end
function Math2
d=c+2
end
end
4 comentarios
John D'Errico
el 25 de Abr. de 2018
Editada: John D'Errico
el 25 de Abr. de 2018
RETURN THE VALUE d!!!!!!!
function d = Math2(c)
Then use it like a function inside Main.
d = Math2(c);
You need to learn to use functions.
Stephen23
el 25 de Abr. de 2018
Editada: Stephen23
el 25 de Abr. de 2018
@Brian Rreid: how to call functions is explained in the introductory tutorials:
How to define functions with output arguments is shown in the function help.
"How do I get a main function to recognize all variables in a function?"
In general that would be a very bad way to write code. Functions have independent workspaces, and that is how they are supposed to be. Trying to pass all variables from one workspace to another will be slow, complex, and buggy. The recommended way to write code is to pass the variables that you need.
Respuestas (0)
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!