No such variable line 4
Code: [Local Link Removed for Guests]
test
wlog x
end
sub test
x=200
end sub
Code: [Local Link Removed for Guests]
test
wlog x
end
sub test
x=200
end sub
Code: [Local Link Removed for Guests]
test
wlog x
end
sub test
Local x
x=200
Test1
Wlog "after call test1",x
end sub
Sub test1
Wlog "sub test1",x
X=50
End sub
You can achieve your objective in this way :[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Tue Jan 07, 2025 5:53 pm OK,
but why can I define variables as local in subs if they are generally local?
If I define a variable as local in a sub and call another sub from this sub, this variable is known in the second sub and can also be changed, see example.
Am I making a mistake in my thinking?
Code: [Local Link Removed for Guests]
test wlog x end sub test Local x x=200 Test1 Wlog "after call test1",x end sub Sub test1 Wlog "sub test1",x X=50 End sub
Code: [Local Link Removed for Guests]
gosub test
wlog x, y
end
test:
x=200
y=100
return
Honestly it is not a good practice to declare public variables inside a function, as all the global variables should be always declared at the beginning of the main program. You risk to be lost and forget where are declared with the possibility to reuse the same name for another scope, breaking your program.[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Wed Jan 08, 2025 1:15 pm OK,
thanks for the explanation. I know from many other basic dialects that the command defines local variables which are then only known in the sub, independent of the main program, and others without locally declared variables can be used system-wide. This is often handled with public or local.