local function?

Home Forums General Programming local function?

Tagged: 

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #83424
    human fly
    Participant
      • Topics: 124
      • Replies: 1070
      • Total: 1194
      • ★★★★

      when you use a (sub-)function in a method, when do you
      make it ‘local’? ie:
      local function pselect()

      it seems to work without the ‘local’ – so what happens
      in either case?
      – – –
      as a side issue, i tried extracting sections of a method
      to a ‘library’ method, leaving just the function calls in
      the original method – and with some variables initialised
      as ‘global’, so the functions could interact –

      and this seemed to be running ok – until i restarted Ctrlr
      and my panel, and then nothing seems to work anymore: a key
      subfunction returns ‘nil’. i checked everything but it all
      seems ok.

      so now i’ve had to put back the functions in the original,
      and it works again. – so what’s going on there? was it because
      i was making those variables global, and they were still in memory?

      #83431
      Possemo
      Participant
        • Topics: 14
        • Replies: 638
        • Total: 652
        • ★★★

        I never used local functions so I cannot help you on that. But I can explain you what “local” means on variables: The var will only be available for the actual method. All other methods won’t see this var. If you don’t declare the var as local it will be automatically a global variable. This is not to be confounded with the Ctrlr global vars. Unfortunately they share the same name but this is something completely different.

        #83432
        human fly
        Participant
          • Topics: 124
          • Replies: 1070
          • Total: 1194
          • ★★★★

          can’t tell if there’s a difference if i make a
          ‘nested’ function ‘local’ or not. either seems to
          work with this.

          i suppose it keeps the function name local, so you
          could have two functions with the same name, as
          long as they are ‘local’ to the method.

          that’s actually what i’ve got at the moment, re-ordering
          the mess i’ve made.
          – – –
          update on the other thing, not being able to call external
          functions from a Library method: that was user error, partly.
          i had commented out a couple of lines because i forgot what
          they were for – turned out they were needed.
          however: it seems that some of my (remote?) functions work
          from the Library, and others i have to keep within the method
          for now: i can run them straight as part of the main script,
          or break it down into little local functions (not much point
          unless it’s called more than once?)
          i’ll probably find out why eventually. i had merged several
          operations sharing the same iteration – seemed like a good
          idea at the time, but now it means having to separate it
          again.

          #83438
          human fly
          Participant
            • Topics: 124
            • Replies: 1070
            • Total: 1194
            • ★★★★

            got it working. so my lengthy thing now looks like:

            function padSelect(mod,value)
            	-- This variable stops index issues during panel bootup
            	if panel:getRestoreState() == true or panel:getProgramState() == true then return end
            --	------------------------------------------------------
            	local function pselect()	--[ needs (mod,value) in main function ]--
            		psub=nil	--[ pad number ]--
            		local padName= L(mod:getName())
            		psub=tonumber(string.sub(padName,4))
            	end
            --	------------------------------------------------------
            	pselect()	--[ get psub pad# ]--
            --	------------------------------------------------------
            	findkit()	--[ get kitselct kit# ]--
            --	------------------------------------------------------
            	kgetcolour()	--[ get kit colours for psetcolour ]--
            --	------------------------------------------------------
            	findpad()	--[ get padselct pad# ]--
            --	------------------------------------------------------
            	psetcolour()	--[ set pad colour ]--
            --	------------------------------------------------------
            	keynumber() 	--[ get keyselct key# ]--
            --	------------------------------------------------------
            --[ optional: get pad letter and display pad data]--
            --	displaypad()
            --	------------------------------------------------------
            	findVoice()
            --	------------------------------------------------------
            	updateVoiceName()	--[ get voice name and number ]--
            --	--[ retrieve values ]---------------------------------
            --	fetchVoiceTable()	--[ restore voice params ]--
            --	------------------------------------------------------
            --	fetchKeyTable()		--[ restore keyassgn params ]--
            --	------------------------------------------------------
            end

            and the other part that works with it:

            function kitSelect(mod,value)
            	-- This variable stops index issues during panel bootup
            	if panel:getRestoreState() == true or panel:getProgramState() == true then return end
            -- ------------------------------------------------------------
            	local function kselect()	--[ needs (mod,value) in main function ]--
            		ksub=nil	--[ kit number ]--
                	local kitName= L(mod:getName())
            		ksub=tonumber(string.sub(kitName,-1)+1)	
            		console(String("ksub="..ksub-1))
            	end
            -- -----------------------------
            	kselect()	--[ get ksub ]--
            -- -----------------------------
            	findkit()	--[ get kitselct ]--
            -- -----------------------------
            	ksetcolour()
            -- -----------------------------
            	findpad()		--[ get padselct ]--
            -- -----------------------------
            	pgetcolour()
            -- -----------------------------
            	keynumber() --[ get keyselct ]--
            -- ------------------------------------------------------------
            --[ lcd display ]--	--[ optional ]-- --[ get pad letter ]--
            	--panel:getComponent("tempdisplay"):setPropertyString("uiLabelText","key="..keysel)
            --	panel:getComponent("tempdisplay2"):setProperty("uiLabelText","pad:"..(tbl_padalpha[pselect]).."/kit:"..(ksub-1).."/key:"..keysel,false)
            -- ------------------------------------------------------------
            --[ new library stuff ]--
            	findVoice()
            -- -----------------------------
            	updateVoiceName()	--[ get voice name and number ]--
            -- -----------------------------
            --	fetchVoiceTable()	--[ restore voice params ]--
            -- -----------------------------
            --	fetchKeyTable()	--[ restore keyassgn params ]--
            -- -----------------------------
            end
            • This reply was modified 5 years, 11 months ago by human fly. Reason: addendum
          Viewing 4 posts - 1 through 4 (of 4 total)
          • The forum ‘Programming’ is closed to new topics and replies.
          There is currently 0 users and 49 guests online
          No users are currently active
          Forum Statistics
          Threads: 2,495, Posts: 17,374, Members: 77,605
          Most users ever online was 12 on January 22, 2019 3:47 pm
          Ctrlr