Global vars
Publié : mer. sept. 06, 2017 3:35 pm
Hi, how can I save global variables, but when I run a program call again, these again do not lose their content
Thanks
Thanks
Xcas: un logiciel libre de calcul formel
https://xcas.univ-grenoble-alpes.fr/forum/
https://xcas.univ-grenoble-alpes.fr/forum/viewtopic.php?f=19&t=1866
Code : Tout sélectionner
x:=0;
f():={
x:=x+1; // global
print(x);
}:;
Code : Tout sélectionner
f1():={
y:=y+1;
print(y);
}:;
Code : Tout sélectionner
f1():={
local y; purge(y);
y:=y+1;
print(y);
}:;
Code : Tout sélectionner
f1():={
Symbs y;
y:=y+1;
print(y);
}:;
Code : Tout sélectionner
x:=0; // global
f():={
x:=x+1;
print(x);
}:;
f1():={
local y; purge(y); // Symbs y
y:=y+1;
print(y);
}:;
f2():={
local x; purge(x); // Symbs x
x:=x+1;
print(x);
}:;
First the compiler should not check that x is assigned, because that may change later, second it is essential to throw a warning message, because functions should not use global variables, only local variables. Global variables are tolerable for symbolic variables, but it's better to use a symbolic local variable, as explained in the warning. If you really want a global variable, you can still declare it as global.compsystems a écrit :solved by putting x: = 0 before the definition of the function.
but the compiler should detect that the x variable has been defined previously and not throw the warning message Warning: x, declared as global variable(s)
Code : Tout sélectionner
f1():={
local y:='y';
y:=y+1;
print(y);
}:;
Code : Tout sélectionner
f1():={
local y; purge(y);
y:=y+1;
print(y);
}:;