|
Forum Index : Microcontroller and PC projects : Variable scope lost?
| Author | Message | ||||
| ville56 Senior Member Joined: 08/06/2022 Location: AustriaPosts: 259 |
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: 425 |
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: 2786 |
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: 1038 |
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 |
||||
| ville56 Senior Member Joined: 08/06/2022 Location: AustriaPosts: 259 |
Thanks to all for the answers. It's what I've suspected but couldn't find any restrictions in the manual either. Just wanted to make sure it's not a "new feature". And no, it's not my style of programming, but the GOSUB just came about handy to quickly test something within a function. Couldn't resist .... ![]() 73 de OE1HGA, Gerald |
||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 8228 |
GOTO has it's place when used with labels, IMHO, but GOSUB has been made completely obsolete by SUB. It's a program mangler. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
| ville56 Senior Member Joined: 08/06/2022 Location: AustriaPosts: 259 |
IMHO not necessarily completely obsolete ... i dislike gosubs as a way of programming, so far we agree. BUT ... if you have a lot of parameters to pass to a sub or function you easily run out of linespace in the definition of the sub/function as the line is limited to 255 chars and you have to specify every parameter (name, datatype). If you use gosub you don't have to take care about this as every variable is accessible as long as you are on main-level. I sometimes use it as an aid for debugging purposes. And yes, i could pass parameters as arrays of the same datatype, but i consider this as hardly readable and try to avoid that. 73 de OE1HGA, Gerald |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2786 |
I guess it depends on the situation. For Subs at least, if some of the variables you are inputting always have the same name (and are global) they don't need to be specified at the start of the Sub. As with GoSub just use the global name in the Sub. For those that have to be specified you can save some characters by using suffixes instead of "As Integer" etc. Dim A=1.1, B=2.2, C=3.3, D=4.4 Test A, B Sub Test X%, Y% Print X%, Y%, A, B, C, D End Sub > run 1 2 1.1 2.2 3.3 4.4 Works for Functions too. Dim A=1.1, B=2.2, C=3.3, D=4.4 Print Test(A, B) Function Test(X%, Y%) Print X%, Y%, A, B, C, D Test = C + D End Function > run 1 2 1.1 2.2 3.3 4.4 7.7 Edited 2025-10-10 22:20 by phil99 |
||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 8228 |
Eurgh..... I hate big long lines of arguments in a SUB or function call. It makes them difficult to follow. I also hate lines longer than 80 chars and I'll always try to keep them shorter. Once again, it's in the interests of program legibility. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |