defining global variables in SUBs is not possible

Here we can discuss about the problem found
Post Reply
Monki
Posts: 79
Joined: Mon Feb 27, 2023 12:56 pm
Location: Berlin
Has thanked: 29 times
Been thanked: 18 times

defining global variables in SUBs is not possible

Post by Monki »

Apparently defining global variables in SUBs is not possible. The following coder causes the error message
No such variable line 4

Code: [Local Link Removed for Guests]

 
test 
wlog x
end

sub test
	 x=200
end sub
User avatar
cicciocb
Site Admin
Posts: 2781
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 592 times
Been thanked: 1990 times
Contact:

Re: defining global variables in SUBs is not possible

Post by cicciocb »

image.png

If you want define global variables inside a sub, simply use GOSUB
You do not have the required permissions to view the files attached to this post.
Monki
Posts: 79
Joined: Mon Feb 27, 2023 12:56 pm
Location: Berlin
Has thanked: 29 times
Been thanked: 18 times

Re: defining global variables in SUBs is not possible

Post by Monki »

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
User avatar
cicciocb
Site Admin
Posts: 2781
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 592 times
Been thanked: 1990 times
Contact:

Re: defining global variables in SUBs is not possible

Post by cicciocb »

[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
You can achieve your objective in this way :

Code: [Local Link Removed for Guests]

gosub test 
wlog x, y
end

test:
 x=200
 y=100
return
The "local" command exists if you want avoid to modify a global variable having the same name.
You should always use it to be sure that your SUB will not impact the main program
For example, if you already used the variable "Z" in the "main" program and you want reuse this variable name inside the SUB without changing the global; declaring "local Z", another variable with the same name will be created and removed at the end of the SUB.
Without "local Z", the main variable will be modified, with some possible side effect
Monki
Posts: 79
Joined: Mon Feb 27, 2023 12:56 pm
Location: Berlin
Has thanked: 29 times
Been thanked: 18 times

Re: defining global variables in SUBs is not possible

Post by Monki »

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.
User avatar
cicciocb
Site Admin
Posts: 2781
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 592 times
Been thanked: 1990 times
Contact:

Re: defining global variables in SUBs is not possible

Post by cicciocb »

[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.
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.
In the "C" , "C++" or C# and in many other programming languages, for example, this is not possible; I do not encourage to do that but I shown you that this is possible in Annex and how this can be done
User avatar
karlkloss
Posts: 169
Joined: Fri Aug 18, 2023 12:21 pm
Has thanked: 24 times
Been thanked: 35 times

Re: defining global variables in SUBs is not possible

Post by karlkloss »

But the thing is, if you initialize gui elements inside a sub, their handles are also local, so you can't use them outside the sub.
If you stay in the sub, then all the gui events are blocked, as they are apparently handled globally, so you can't click anything.

Or maybe I'm doing it completely wrong.
Coming from the Visual Basic/WPS/Delphi world, I'm having a hard time adapting to Annex RDS.

I'd like to put self-made GUI elements like onscreen keyboards, popups, overlays, etc. into external subroutines, but I don't really know how to do it in a somewhat self-contained way, without putting a lot of additional global code and variables into the main program.

Any ideas?
User avatar
cicciocb
Site Admin
Posts: 2781
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 592 times
Been thanked: 1990 times
Contact:

Re: defining global variables in SUBs is not possible

Post by cicciocb »

if you want that your variables become global, do not use SUB but call them with GOSUB .

All the variables defined there will be available everywhere in the code
Post Reply