System information

Using Arguments in Subroutines
The ability to use arguments is one of the major features of using Macro() or GoSub(),
because it allows you to abstract out code that would otherwise be duplicated across
your dialplan. Without the need to duplicate the code, we can better manage it, and
we can easily add functionality to large numbers of users by modifying a single location.
You are encouraged to move code into this form whenever you find yourself creating
duplicate code.
Before we start using our subroutine, we need to update it to accept arguments so that
it is generic enough to be used by multiple users:
[subVoicemail]
exten => start,1,Dial(${ARG1},10)
same => n,GotoIf($["${DIALSTATUS}" = "BUSY"]?busy:unavail)
same => n(unavail),VoiceMail(${ARG2}@default,u)
same => n,Hangup()
same => n(busy),VoiceMail(${ARG2}@default,b)
same => n,Hangup()
Recall that previously we had hardcoded the channel variable ${JOHN} as the location
to dial, and mailbox 101 as the voicemail box to be used if ${JOHN} wasn’t available. In
this code, we’ve replaced ${JOHN} and 101 with ${ARG1} and ${ARG2}, respectively. In
more complex subroutines we might even assign the variables ${ARG1} and ${ARG2} to
something like ${DESTINATION} and ${VMBOX}, to make it clear what the ${ARG1} and
${ARG2} represent.
Now that we’ve updated our subroutine, we can use it for several extensions:
[LocalSets]
exten => 101,1,GoSub(subVoicemail,start,1(${JOHN},${EXTEN}))
exten => 102,1,GoSub(subVoicemail,start,1(${JANE},${EXTEN}))
exten => 103,1,GoSub(subVoicemail,start,1(${JACK},${EXTEN}))
Again, our dialplan is nice and neat. We could even modify our subroutine down to
just three lines:
[subVoicemail]
exten => start,1,Dial(${ARG1},10)
same => n,VoiceMail(${ARG2}@default,${IF($[${DIALSTATUS} = BUSY]?b:u)})
same => n,Hangup()
One difference to note between GoSub() and Macro(), however, is that if we left our
subroutine like this, we’d never return. In this particular example that’s not a problem,
since after the voicemail is left, we would expect the caller to hang up anyway. In
situations where we want to do more after the subroutine has executed, though, we
need to implement the Return() application.
GoSub() | 209