Reputation: 2451
We are working on a Scilab project for our math class and we're having trouble using global variables. We're trying to use a global variable as a counter. The counter needs to be modified in multiple functions but everytime the counter doesn't save the new value and reverts to the initialized one. Why does the counter not get adjusted properly?
Concretely the situation is as follows.
counter = 0
function checkForA()
// Do some stuff
counter = counter + 1
endfunction
function checkForB()
// Do some stuff
counter = counter + 3
endfunction
function printCounter()
disp(counter)
endfunction
Thanks in advance
Upvotes: 1
Views: 2498
Reputation: 12773
As far as I can tell you need to specify that variables are global explicitly in scilab;
global counter
counter = 0
function checkForA()
global counter
// Do some stuff
counter = counter + 1
endfunction
function checkForB()
global counter
// Do some stuff
counter = counter + 3
endfunction
function printCounter()
global counter
disp(counter)
endfunction
Upvotes: 4