Ok, so now you've made it this far through the Handbook, and are ready to make a killer script. This could be one of the most important sections, because without functions, your script just relies on Events; which aren't so bad themselves but are only interactive with IRC.
Let's say that you have several channels that you want to join. But you only want to issue one command. You could do something like this.
proc joinChan {} {
join myChan0
join myChan1
join myChan2
}
But that certainly isn't that elegant. Instead you could do this.
proc joinChan {} {
foreach channel { myChan0 myChan1 myChan2 }{
join $channel
}
This will cycle through each channel and join it.
So after coding awhile, you realize that you are rewriting a single line of code for each proc, but you being the kind of person who would rather reuse code would like to be able to reuse it instead of retyping it. Lets say that the piece of code in question gets the 'active' channel.
GetChannel $active($me(id)) $me(id)
So how would we reuse it? We could do this.
proc activeChan {} {
global active me
GetChannel $active($me(id)) $me(id)
}
But that alone doesn't do much. In order to put it to use we could create a proc that ops, otherwise known as the command /op.
proc op { nick } {
set getChan [activeChan]
mode $getChan +o $nick
}
Now you can /op <nick> anyone that you want (when your already opped of course).