![]() |
Forum Index : Microcontroller and PC projects : Variable scope lost?
Author | Message | ||||
ville56 Senior Member ![]() Joined: 08/06/2022 Location: AustriaPosts: 242 |
I've a strange problem with a GOSUB within a FUNCTION. In the following example the scope of the variable in_param gets lost in the GOSUB, no matter where I place the code for the gosub. OPTION EXPLICIT OPTION DEFAULT NONE dim integer stat stat=test_f(22) end function test_f (in_param as integer) as integer gosub print_param exit function print_param: print "in funct ";in_param return end function it throws "Error : IN_PARAM is not declared" Are there any restrictions of using GOSUB within a FUNCTION? Version is PicoMite MMBasic RP2350A V6.01.00b20 Thanks for any hints .... Gerald Edited 2025-10-10 05:12 by ville56 73 de OE1HGA, Gerald |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 423 |
Hi P116 of the manual: "You must not jump into or out of a function using commands like GOTO. Doing so will have undefined side effects including ruining your day." Solution: Don't do it ;-) Got caught by that one a few times... Entropy is not what it used to be |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2765 |
Although "gosub print_param" and "print_param:" are both within the Function so is not jumping out, I think it has been mentioned that nesting a Sub within a Function is not supported. However I can't find it in the manual. Edited 2025-10-10 07:57 by phil99 |
||||
disco4now![]() Guru ![]() Joined: 18/12/2014 Location: AustraliaPosts: 1033 |
GOSUB, SUB and FUNCTION all increment the LocalIndex pointer when called, so they are only aware of Global variables and any Local variables created at their own level. In the function test_f the variable in_param is created as a Local variable as it is passed BYVAL. The GOSUB code cannot see this Local variable as the LocalIndex pointer has been incremented, it must be passed. GOSUB is obsolete and I don't think you can pass it parameters. Embedding the GOSUB within the Function as above does not put it at the same level. The Function tolerates it being there but never really sees it as part of the function because of Exit Function. The GOSUB is happy to go and find the label and execute its code and return, but it can't see any of the Functions variables. OPTION EXPLICIT OPTION DEFAULT NONE dim integer stat stat=test_f(22) end function test_f (in_param as integer) as integer gosub print_param end function print_param: print "in funct ";in_param return Use the more modern SUB allows passing the parameter Option EXPLICIT Option DEFAULT NONE Dim integer stat stat=test_f(22) End Function test_f(in_param As integer) As integer print_param in_param End Function Sub print_param(in_param As integer) Print "in funct ";in_param End Sub Edited 2025-10-10 11:32 by disco4now F4 H7FotSF4xGT |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |