how can I take a global variable declared in a program, take it in a function, change its value inside the fuction and give it again to the program as global?

3 次查看(过去 30 天)
Hi! I have the following Problem: I have a variable declared in my program, whose value I want to Change in a function and then return it to the program as global, so that when I call the function again the value stored in there is the one I calculated the last time within the function. If anyone could help me, please...

采纳的回答

Adam
Adam 2015-3-5
You don't need to pass global variables anywhere.
You just keep putting
global myVariable
within any scope in which you wish to use it.
That said, it is terrible programming technique to use global variables. Just do something like:
value = doSomething( value );
in your main program with a normal (non-global) variable passed into a function and then passed out again after being updated. There are other techniques too, but that is probably the simplest.
  3 个评论
Adam
Adam 2015-3-6
I don't think that changes anything I said though. The whole purpose of functions is that they take input arguments and can send back output arguments. You can pass out the same thing you pass in and then pass it in again next time you call the function if that is what you wish to do.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by