help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: global variables


From: Michal Studniarek
Subject: Re: global variables
Date: Tue, 21 Jan 2014 18:01:40 +0100

You are indeed right!!! Sorry for my mistake. It looks like the problem is solved:

You WILL MODIFY the value of a global variable by using it like this:

global var x=2;

function test()
global x;
x=4
endfunction;

and you WILL NOT MODIFY the value of it when you pass it as a parameter like this:


global var x=2;

function test(x)
x=4
endfunction;

The second way creates a copy for local use.

Regards,
Michal





2014/1/21 Mike Miller <address@hidden>
On Tue, Jan 21, 2014 at 17:39:35 +0100, Michal Studniarek wrote:
>
> I think the question was if you can define a global variable then change
> its value inside function and to have a new value available outside.
>
> In example:
>
> global x=2;
>
> function x=test()
>  global x;
>  x=4;
> endfunction
>
> Value of global variable x will be still 2. Function operates on global var x
> locally but can not change its value globaly.

In fact your example is almost exactly out of the manual and shows
that a function can indeed modify the value of a global variable, both
inside and outside the scope of the function.

See https://www.gnu.org/software/octave/doc/interpreter/Global-Variables.html

--
mike
_______________________________________________
Help-octave mailing list
address@hidden
https://mailman.cae.wisc.edu/listinfo/help-octave


reply via email to

[Prev in Thread] Current Thread [Next in Thread]