How do I get a main function to recognize all variables in a function?
显示 更早的评论
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 个评论
Rik
2018-4-25
What is your goal? Because your two nested functions are not used, nor a and b. The line below would display d.
d=0.5;main(d)
Brian Rreid
2018-4-25
John D'Errico
2018-4-25
编辑:John D'Errico
2018-4-25
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.
@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.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!