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

有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top