Warning: As a general rule of thumb when learning programming, it's best that you don't copy/paste the code directly. You will learn much better if you type everything out multiple times. Trust me, it makes a night and day difference in how fast you will grasp the material.




Functions can also be called sub routines. I prefer calling them functions, but with perl you're most likely going to hear people calling them sub routines. It's all a matter of choice. Functions are used to help clean up your code and keep it as small as possible. Instead of re-coding a 5 line task you can put it in a function and then reference it, dramatically reducing the number of lines in your code, which is always a plus. Today we'll be covering the basics of functions. How they work, how to create one, how to reference a function, and how variables behave inside of a function.





To start off with variables we'll be creating a very simple script to help you get a grasp for variables. To start off create a new perl d[php]ocument (see tutorial #1) and make it executable "chmod +x filename.plx". Your code should now look like this

PHP Code:
#!/usr/bin/perl 

Now we'll be creating out first function. Below you will find the basic structure of a function

PHP Code:
sub function_name {
    print 
"this is the function \n";


At the beginning you'll notice that we put "sub" before the function name. This stands for "Sub routine", which as I stated earlier, is just another name for function.

In the function name you'll notice that there is an underscore in between "function" and "test". This is very important if the name of your function is more than one word long. This lets Perl know that the next word is part of the functions name.

Similar to the if statements you learned earlier, you put the action you want completed when you reference the function in curly braces. Again, this is very important. This lets Perl know when the code to be completed starts and ends.

When this function if referenced, it will write out the text "This is the function" and then create a new line. If you aren't sure why this is go back to lesson #1 where I explain how to print data.



Now, it's time to reference the function we've just created. This is very simple, and can be done using the code below.

PHP Code:
function_name(); 

If you've learned a programming language before you may be wondering "why we parenthesis at the end?". While it may not be needed, It is recommended that you include them anyway for the sake of making your code easy to read.


This is what your code should look like when you're finished.

PHP Code:
#!/usr/bin/perl

sub function_name {
    print 
"this is the function \n";
}

function_name(); 




Variables in functions -------

In perl, all variables in functions are global. This means that the variable can be referenced anywhere below where the variable is written. You will need to be careful with this, as you can accidently end up referencing a variable from a function you didn't want to reference. For this reason, you can create a local variable. A local variable can only be referenced within a function. This enables you to generate a variable called $variable in a function and then create another variable later on in your code with different information.



Global Variables ---

PHP Code:
#!/usr/bin/perl

sub name_function {
    
$variable "Variable can be referenced";
}

name_function();

print 
"$variable"

Here we have a simple script with a function that contains a variable then the script prints the variable in the function. As you can see, we're referencing the $variable outside the function and we're still able to print out the data. This is possible because all variables in functions are global, unless otherwise stated. A global variable can be referenced anywhere blow where it was created.




Private Variables ---

PHP Code:
#!/usr/bin/perl
sub name_function {
    
me $variable "Variable can be referenced";
}

name_function();

print 
"$variable"
If you where to run this script, you would get nothing. That's because I've converted the variable into a private variable by adding "me" before I declare the variable. This will only allow the variable to be referenced inside the function.