Announcement

Collapse
No announcement yet.

Declare variable yes or no

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Declare variable yes or no

    Hello,

    Should the variable used in a for loop be declared before use?
    In the code example I get the error


    Error: 'x' not found (line 27 file:Game_of_Life.4dg)

    func countNeighbours(var row, var col)
    var count:=0;
    //var x,y; <==== declare yes or no
    for(x:=-1; x <=1; x++) <===== gives no an error
    for(y:=-1; y <= 1; y++)
    // see for example "BIGARRAY"
    if ((board+Index(row+x,col+y) == 1) && (x != 0 || y != 0))
    count++;
    endif
    next
    next
    return(count);
    endfunc

    In this function I don't get an error!!!

    func drawBoard()
    for (row := 0; row < (rows - 2); row++)
    for (col := 0; col < (cols - 2); col++)
    if board[Index(row + 1,col + 1)]
    myGLCD.fillRect(offset_x+col * blockSize, offset_y+row * blockSize, offset_x+(col * blockSize) + blockSize, offset_y+(row * blockSize) + blockSize);
    endif
    next
    next
    endfunc

    What do I wrong?

    Cheers,

    Jan

  • #2
    You must always declare variables, convention states at the start of each function, although they can be declared anywhere.

    I suspect in your second example those variables are declared as globals somewhere
    Mark

    Comment

    Working...
    X