# How is the LOCAL dialplan function susposed to work?

Setting a regular variable with the same name as a LOCAL variable will overwrite its value. Consider the following example:

``````[subLocalTest]
exten => start,1,NoOp()
same => n,Set(LOCAL(number)=200)
same => n,NoOp(LOCAL(number)=\${LOCAL(number)})
; The following two statements (even in another Gosub) will overwrite LOCAL(number) in this sub:
same => n,Set(number=15)
;same => n,Set(ARRAY(name,number)=Eric,15)
same => n,NoOp(LOCAL(number)=\${LOCAL(number)} <expected 200>)
same => n,Return()
``````

gives you:

``````-- Executing [300@TheOffice:1] NoOp("SIP/qq-0000002c", "") in new stack
-- Executing [300@TheOffice:2] Answer("SIP/qq-0000002c", "") in new stack
-- Executing [300@TheOffice:3] Goto("SIP/qq-0000002c", "local") in new stack
-- Goto (TheOffice,300,4)
-- Executing [300@TheOffice:4] NoOp("SIP/qq-0000002c", "") in new stack
-- Executing [300@TheOffice:5] Gosub("SIP/qq-0000002c", "subLocalTest,start,1()") in new stack
-- Executing [start@subLocalTest:1] NoOp("SIP/qq-0000002c", "") in new stack
-- Executing [start@subLocalTest:2] Set("SIP/qq-0000002c", "LOCAL(number)=200") in new stack
-- Executing [start@subLocalTest:3] NoOp("SIP/qq-0000002c", "LOCAL(number)=200") in new stack
-- Executing [start@subLocalTest:4] Set("SIP/qq-0000002c", "number=15") in new stack
-- Executing [start@subLocalTest:5] NoOp("SIP/qq-0000002c", "LOCAL(number)=15 <expected 200>") in new stack
-- Executing [start@subLocalTest:6] Return("SIP/qq-0000002c", "") in new stack
-- Executing [300@TheOffice:6] Hangup("SIP/qq-0000002c", "") in new stack
``````

I don’t see the purpose of LOCAL if it’s value can be modified from another Gosub context. Am I missing something obvious here? (Asterisk 13.14.0-rc2)

The wiki[1] has a description of what it is for. It’s a locally scoped variable that after the Gosub returns it is returned to its previous state. Underneath it is doing a normal Set() but maintains state that it cleans up when Gosub returns so it return to the original state.

Thanks for clarifying. Looks like you need to use LOCAL in every sub to keep everything scoped. I was partway through doing that.