Perl Programming - Working with subroutine return values

preview_player
Показать описание
Perl Programming - Working with subroutine return values
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.
Рекомендации по теме
Комментарии
Автор

hello i am having trouble with my perl code i can not execute the output of a user entering a first number and second in a text box where they have they option of selecting in radio buttons to add or subtract the two numbers that they will imput. so far i have this code:

use CGI qw (:standard);
print header;
print start_html;
if (!param)
{
print "<form action='$ENV{SCRIPT_NAME}' method=get>\n", br;
print "Enter the first number: <input type='text' size='10' name='x'>\n", br;
print "Enter the second number:<input type='text' size='10' name='y'>\n", br;
print "<input type='radio' name='select' value='add'>Add\n", br;
print "<input type='radio' name='select' value='sub'>Sub\n", br;
print "<input type='submit' value='Submit'>\n", br;
print "</form>";
}

sub add()
{
print "The sum is ('x' + 'y')";
return ($x + $y);
$add=&add();
}
print end_html;

i really don' t get this part the form plus these subroutines mixed up confuse me can u help me out?

anthonygonzalez
Автор

why not just type return instead of return (40 + 6)

esthermannabanda
visit shbcf.ru