Perl Programming – Calling Subroutines



Perl Programming – Calling Subroutines

Perl Programming - Calling Subroutines

Perl Programming – Calling Subroutines
Get the entire Perl Programming course for 20% off: http://stoneriverelearning.com/courses/perl-programming-for-beginners-online-course?product_id=38639&coupon_code=YOUTUBE20
Welcome back again, guys. In this lecture, we’re going to be calling subroutines. This is section 5, lecture 3. Let’s jump right into it.
Calling subroutines. Why would we want to call subroutines? Of course, subroutines makes our job a lot easier. We simply use the and character & followed by the name of the subroutine. That’s all that we do to call a subroutine. For all of you previous programmers out there, the other terminology is methods and functions. If you’re using C++ in the past or C, they’re called functions. But in Perl, it’s called a subroutine.
For a quick example, this is how you define and call a subroutine. Again, we use the & character first followed by the subroutine name that we define. We have to use the Perl identifier rules. Me, I just use letters and an underscore character. So I use the mixture of both to define my subroutine which is print_name and I set up another one for add_numbers. I’ll show you guys how to use these in our examples as well. Let’s jump in right into our examples and I’ll show you some live demonstrations.
First thing first, let’s start by defining our subroutine. We’re going to use the keyword sub and it’s going to be followed by the name of the subroutine. I’ll call it print_name followed by a pair of parentheses. I skip a line from my parentheses instead of my curly braces so I can include some Perl instructions. I’ll put a tab to make it a lot more cleaner and readable. What I’ll do here is I’ll call a built-in subroutine by Perl which we’ve currently been using called print, add in my double quotation marks. Hello, you have created your first subroutine in Perl. I’ll do that. I love to copy and paste, so I’ll copy that subroutine and I’ll just paste it in and I’ll just modify bits and parts.
For the second subroutine that we went over in my lecture, it’s called add_numbers. I’ll modify our print statement. Let me just erase this part. What I’ll do as well right above the add_numbers, let me declare some scalar variables. I’ll just call this x = 6 and y = 8. I’ll define another result variable, so results = x * y. I’ll just print my results. You know what? Since it’s called add_numbers, let’s just do the addition operator. I’ll put in a pair of parentheses, add a new line character. I’ll do that. Let’s call our first subroutine which is called print_name. I’ll just copy our subroutine name but first I’ll put a comment calling our subroutines. Again we start with our & character followed by the name. I’ll do the same thing with our add_numbers. The & add_numbers. If I’m right, let me save my work. Let me add some new line characters as well to give it some space for our subroutines. Let’s go up to Run, click Run Script. Let’s see what happens. That’s exactly what we wanted Perl to do. At the bottom, it called our first subroutine that we defined which is called print_name. If we go to our command prompt, it says, “Hello, you have created your first subroutine in Perl.” It skipped two lines with our new line characters and it did some addition on our second subroutine which 6 + 8 = 14. If we go down to our second subroutine, here we have it. Of course we’re just giving it a print instruction or print subroutine to do some output for us.
In a nutshell, that’s how we call subroutines in Perl. We just use the & character first followed by the subroutine name and that’s it. If you guys have any questions, please feel free to let me know. I’ll see you guys in our next lecture

Comments are closed.