So far you have worked with conditional and looped code blocks such as the following; ``` perl if { ... } elsif { ... } else { ... } unless { ... } while (boolean) { ... } for (@list) { ... } ``` You can also indicate that a code block should be called only when you as the coder ask it in what is known as a named subroutine. ``` perl sub errorMessage { print "There was an error.\n"; return; } ``` Oftentimes these subroutines are combined with conditions. ``` perl # preceeding code gets the user to enter a yes or no value in $iWantHelp if ($iWantHelp ne 'yes' && $iWantHelp ne 'no') { errorMessage; } # more code for the rest of the program sub errorMessage { print "There was an error.\n"; exit; } ``` You'll notice that the subroutine was added at the bottom of the entire code. This is largely organizational, you can place a subroutine anywhere, BUT it is not called unless you explicitly call it. Most coders put all their subroutines at the bottom of the file. # Passing Parameters and the Local Variable You can also pass parameters to your subroutines; ``` perl # preceeding code gets the user to enter a yes or no value in $iWantHelp if ($iWantHelp ne 'yes' && $iWantHelp ne 'no') { errorMessage("You must type either yes or no in lowercase."); } # more code for the rest of the program sub errorMessage { ($message) = @_; print "$message$\n"; exit; } ``` As you can see we added a value to the `errorMessage` call. That value was received in the subroutine through the *Local Variable `@_`*. The Local Variable is an automatically created array of all the parameters you send to it. It can be unlimited in how many values it carries. In our example above, we only sent one variable, so `@_` only had one entry in its list. We chose to give that entry a name through `($message) = @_;` If we sent multiple variables, we could do the same with `($var1, $var2, $var3 ...) = @_;` or we could just choose to use `@_` in for loops exactly as it is. # Using subroutines to deal with repeated tasks If you know there is something you're going to do a lot, you can turn it into a subroutine. For example, if I know I'm going to need to print someone's name a lot; ``` perl for $person (@listOfPeople) { #each item in @listOfPeople is a hash of info print fullName($person); #we send fullName the whole hash of info } sub fullName { ($thisPerson) = @_; #receives the hash of info for one person return $thisPerson->{first} . ' ' . $thisPerson->{last}; } ``` # Return, Exit or Die A subroutine will always return back to the place where you called it from. However you can control if and how it returns back. `exit` is a command that simply stops the process. Any code that comes after it doesn't happen. It's an easy way to say "stop everything". `return` lets you send information back. You can send any single value you want, or you can send a bunch of values in either a hash or an array. See the `fullName` example above, or this one below; ``` perl $fullName = fullName($thomas); #thomas->{first} = "Thomas", ->{last} = "Engine" print "Their name is $fullName"; #will print "Their name is Thomas Engine" sub fullName { ($thisPerson) = @_; $thingToReturn = $thisPerson->{first} . ' ' . $thisPerson->{last}; return $thingToReturn; } ``` `die` can be accompanied by a string that explains why you chose to kill the code. ``` perl die "This error occurred because you told it to."; ``` # Assignment For your Evil Certification and your Dungeon assignments, add an error checker that captures invalid inputs by the user and explains their errors.