Page 1 of 1

scope of dim statements

Posted: Wed Apr 16, 2014 12:50 pm
by PaPi
I`ve an problem:
In this program:
`--------------------------------
`dimtest by PaPi 16-04-2014
dim i as integer
dim j as integer
i=10
j=20
sub testsub(jj as integer)
dim i as integer
i=jj+jj
showmessage str$(i)
end sub

testsub(j)
showmessage "end"+str(i)
`-----------------------------
The fnxcompiler say :
error 1 line 8 Variable `i` already exists.

Try this, Marco!

But, I know that the outside and inside written
" dim i as integer "
instructions independent of each other. Is not that right?

PaPi

scope of dim statements

Posted: Wed Apr 16, 2014 3:13 pm
by Marco
They are independed if you usedthem not already, a global dimmed var in the root of the program overrules the ones in the subs.
To keep the global var global you can not use it in the subs.

if you have this posibility

dim i as integer
i=10
sub test(i as intger)
  i=20
  print i `` no acces to global that`s why it is blocked by an already exists error
end sub

best regards

scope of dim statements

Posted: Wed Apr 16, 2014 4:00 pm
by PaPi
Thank you very mutch.
In short, can not use the same name for a global variable and a local variable at the same time. So this is a FNXBASIC features, as it is usually not the case (in other languages[ch8203][ch8203]).

best regards
PaPi