computing‎ > ‎

Some scilab tips

Beware of un-declared variables

Scilab has a unique //feature// which is potentially dangerous and can make debugging very difficult. This has to do with un-initialized variables. Let me give an example. In the code below, ''func1'' calls ''func2'' and ''func2'' is supposed to return the average value of its input vector ''x''.

function y = func1()

n = 10
x = linspace(0,1,n)
y = func2(x)

endfunction

function y = func2(x)

y = sum(x)/n
printf("n = %d\n", n)

endfunction

Note that in ''func2'' I did not initialize the value of ''n'' but scilab will not complain. It checks if there is a variable of the same name in the calling function and if it finds it, then it uses the value of that variable. In my example, it will not be a problem since the correct value of ''n'' in ''func2'' is the same as in ''func1''. But you can imagine how disastrous this can be.
Comments