System information

same => n(busy),VoiceMail(101@default,b)
same => n,Hangup()
If we were going to convert this to be used for a subroutine, it might look like this:
[subVoicemail]
exten => start,1,Dial(${JOHN},10)
same => n,GotoIf($["${DIALSTATUS}" = "BUSY"]?busy:unavail)
same => n(unavail),VoiceMail(101@default,u)
same => n,Hangup()
same => n(busy),VoiceMail(101@default,b)
same => n,Hangup()
Not much of a change, right? All we’ve altered in this example is the context name,
from [macro-voicemail] to [subVoicemail], and the extension, from s to start (since
there is no requirement that the extension be called anything in particular, unlike with
Macro(), which expects the extension to be s).
Of course, as in the example in the section “Macros” on page 204, we haven’t passed
any arguments to the subroutine, so whenever we call [subVoicemail], ${JOHN} will
always be called, and the voicemail box 101 will get used. In the following sections,
we’ll dig a little deeper. First we’ll look at how we would call a subroutine, and then
we’ll learn how to pass arguments.
Calling Subroutines from the Dialplan
Subroutines are called from the dialplan using the GoSub() application. The arguments
to GoSub() differ slightly than those for Macro(), because GoSub() has no naming re-
quirements for the context or extension (or priority) that gets used. Additionally, no
special channel variables are set when calling a subroutine, other than the passed ar-
guments, which are saved to ${ARGn} (where the first argument is ${ARG1}, the second
argument is ${ARG2}, and so forth).
Now that we’ve updated our voicemail macro to be called as a subroutine, lets take a
look at how we call it using GoSub():
exten => 101,1,GoSub(subVoicemail,start,1())
You’ll notice that we’ve placed a set of opening and closing parentheses
within our GoSub() application. These are the placeholders for any ar-
guments we might pass to the subroutine, and while it is optional for
them to exist, it’s a programming style we prefer to use.
Next, let’s look at how we can pass arguments to our subroutine in order to make it
more general.
208 | Chapter 10:Deeper into the Dialplan