Question

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

Était-ce utile?

La solution

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top