definition of a local variable problem
When I define a local variable multiple times (see functions getNumber) an error occurs which causes the program to malfunction.
Additionally, if I remove the comment characters in the getNumber
function, the program will loop in and call the init function all the time.
The endless "Init start." Printout will appear on the console. and "Init end." will never be displayed. .
I think this is due to the unclear definition of local variables. If the program has a branch, it should be allowed to define a local variable multiple times - or it should be written in the documentation that the variable will be local (on each line of code below) even if the program execution does not pass through the place where this variable is defined as local.
Sample code:
getNumber = function( num_args )
if num_args == 0 then
local num = 12345
elsif num_args == 1 then
local num = 98765
elsif num_args == 2 then
// local num = 11111111
end
return num
end
getNumber2 = function( num_args )
//local num2 = 'string'
if num_args == 0 then
local num2 = 123
elsif num_args == 1 then
num2 = 987
elsif num_args == 2 then
num2 = 567
end
return num2
end
getNumber3 = function( num_args )
//local num2 = 'string'
if num_args == 0 then
num3 = 123
elsif num_args == 1 then
local num3 = 987
elsif num_args == 2 then
num3 = 567
end
return num3
end
init = function()
print( 'Init start.')
if getNumber( 1 ) == 98765 then
print( getNumber( 1 ) + ' == 98765 PASS')
else
print( getNumber( 1 ) + ' diff 98765 ERROR')
end
if getNumber( 0 ) == 12345 then
print( getNumber( 0 ) + ' == 12345 PASS')
else
print( getNumber( 0 ) + ' diff 12345 ERROR')
end
if getNumber( 2 ) == 11111111 then
print( getNumber( 2 ) + ' == 11111111 PASS')
else
print( getNumber( 2 ) + ' diff 11111111 ERROR')
end
print( 'getNumber2(2)=='+getNumber2(2) + ' num2 == '+num2)
print( 'getNumber2(1)=='+getNumber2(1) + ' num2 == '+num2)
print( 'getNumber2(0)=='+getNumber2(0) + ' num2 == '+num2)
print( 'getNumber3(2)=='+getNumber3(2) + ' num3 == '+num3)
print( 'getNumber3(1)=='+getNumber3(1) + ' num3 == '+num3)
print( 'getNumber3(0)=='+getNumber3(0) + ' num3 == '+num3)
print( ' Init end.')
end
update = function()
end
draw = function()
system.pause()
end