Perl Programming - Working with subroutine return values
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, everyone! It’s good to have you again. In this lecture, we’re going to be working with subroutine return values. Again, this is section 5, lecture 4. Let’s jump right into it.
Subroutine return values. The subroutine does an operation on expression within the curly braces of our instructions and it returns the results. That’s the whole point and purpose of the return values and the subroutine. It’s just to calculate or do an operation on an expression within the curly braces and return some kind of value.
Once the subroutine returns the calculated results, it almost immediately exits the current block of code. Once it returns the value where it reads the return statement, it exits the block of code and goes to the next line of the Perl instructions and the program.
For a quick example, if we define our add_numbers subroutine and of course as we see we want the instructions to print our math operation which is 40 + 6 is 46. Create a new line character. If we go to the second Perl instruction, we have the return keyword. So it’s going to return this calculated expression which is 46 for the results. Once we call add_numbers, it’s going to display our output 46 and it’s going to return 46 as well. Again, we can save this value from add_numbers subroutine. If we go down here, I’ve created a scalar variable to save our return value. As we can see, we declare our scalar variable and we just assign it and call the add_numbers subroutine. Let’s do some quick examples before I can show you live how it works.
We first start by defining our user subroutine. I’ll start by defining that. We start with sub for the keyword. I’ll do add_numbers again, my pair of parentheses followed by my curly braces. Again, for our print statement we’ll use to display some output. I’ll just use the number 40, the math operator which is the + sign or addition, + 6 followed by a comma. We want to add a new line character. You know what? I’ll add two new line characters for displaying purposes. Now we’re going to add a return statement. I’ll just use my parentheses. The reason why I use parentheses is because I’m just used to pretty much math, PEMDAS. So it always looks at parentheses first. That’s just what I’m used to in math. I’ll do 40 + 6 for our return value. What I’ll do I want to save that value and a scalar variable. Let’s do added_numbers and I’ll assign it our add_numbers subroutine. I’ll add my & character to call it. That’s pretty much it.
Let’s go back up. We can click Run Script. Let’s see what happens. There we have it. We have the first one which is our print statement and then we have assigned our return value inside our added_numbers scalar variable, but we want to make sure that it is there. So let’s add another print statement to display the value inside added_numbers scalar variable. We can do print. Of course I love my double quotation marks because I can add escape characters which are new lines. I’ll just copy my scalar variable into our print statement. If I’m right, it should display 46 the first time at two new lines, and for the second one I’ll put return value just to make it look a little bit different. Return value. So let’s just say return value 46. I’ll go back up, click Run, Run Script. That’s what we have, the first 46 from our print statement. Then it went down. It calculated our return value. We called it again and we’re saving the value into our scalar variable and we’re just reprinting another string with our value of 46.
Pretty much that’s in a nutshell, guys. If you guys have any questions on return values, how they work, please feel free to let me know. I’ll see you guys in our next lecture.