Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:58 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Variable Scope

Author Message
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1132
Posted: 12:30am 09 Oct 2020
Copy link to clipboard 
Print this post

(This pertains to the CMM2 but probably applies to others too.)

A quick test shows that LOCAL variables are only visible in the subroutine or function in which they are declared and not in subroutines or functions called by said sub or function.

For example, in the following
test1  ' call first subroutine
? " main:v1 =" v1
end

sub test1   ' first subroutine
 local v1

 test2    ' call second subroutine
 ? "test1:v1 =" v1
end sub

sub test2   ' nested subroutine
 v1 = 5544  ' set a value for test1 to use - sorry, denied
 ? "test2:v1 =" v1
end sub
there is no way for the second subroutine to set the value of v1 for use by the first subroutine. Instead, test2 can only alter the global v1.

I suppose that is the way of things and there is no way around this without using global variables...
Visit Vegipete's *Mite Library for cool programs.
 
mkopack73
Senior Member

Joined: 03/07/2020
Location: United States
Posts: 261
Posted: 12:33am 09 Oct 2020
Copy link to clipboard 
Print this post

Correct... If you want/need it accessible/modifyable by the 2nd level function, you have to pass it in as a parameter...
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025