[Scilab-users] Changing elements of "global" structure/vector in function

Lamy Alain Alain.Lamy at cnes.fr
Thu Nov 29 12:46:22 CET 2012


Changing elements of a "global" (i.e. visible) variable in a function may be dangerous.
A new structure or vector is created that only contains the elements that have been changed.

I would expect the whole structure/vector to be copied locally before changing part of them.

A workaround is to create a local copy in the function by an instruction like:
A=A
(see fun3)
which looks a bit strange at first sight as it seems to do nothing.

Has anyone an opinion on whether this is considered as correct behaviour or not ?
Or is it a bug ?


---------------------------------------------

A = [1, 2, 3];
B = struct("a", 2, "b", 3);

function fun1()
A(2) = 5;
disp(A);
endfunction

function fun2()
B.a = 10;
disp(B);
endfunction

function fun3()
A=A;
A(3) = 6;
disp(A);
endfunction


-->fun1()
    0.
    5.

-->fun2()
   a: 10

-->fun3()
    1.    2.    6.






More information about the users mailing list