Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Doubt in functions

by monsoon (Pilgrim)
on Jul 06, 2012 at 00:18 UTC ( #980189=note: print w/replies, xml ) Need Help??

in reply to Doubt in functions

Quote from
"If no return is found and if the last statement is an expression, its value is returned."

Replies are listed 'Best First'.
Re^2: Doubt in functions
by ww (Bishop) on Jul 06, 2012 at 00:55 UTC

    update: inadvertently answered monsoon instead of OP. My mistake. I read the doc quotation as the element about which OP had "doubt."</update>

    Perhaps the example will help:

    #!/usr/bin/perl use 5.014; # 980189 sub add_with_return { my $in_in_sub; my $in2_in_sub; $in_in_sub = $_[0]; # For utter clarity; no doubts; # @_ is the array passed to the s +ub $in2_in_sub = $_[1]; # $_[0] & $_[1] are the first two + elements of @_ my $out_in_sub = $in_in_sub + $in2_in_sub; return $out_in_sub; } sub concat_w_NO_return { my ($in_in_sub, $in2_in_sub); $in_in_sub = $_[0]; # For utter clarity; no doubts; # but MANY simpler & BETTER ways +to do this my $in2_in_sub = $_[1]; my $concat = "$in_in_sub" . "$in2_in_sub"; # Last statement, no r +eturn; so the } # computed value of $c +oncat gets returned my $in = 1; my $in2 = 2; my $str1 = "foo"; my $str2 = "bar"; my $sum = add_with_return($in, $in2); say "\t \$sum: $sum"; my $output = concat_w_NO_return($str1, $str2); say "\t \$output: $output";


    $sum: 3 $output: foobar

    P.S.: perldoc -f return puts the explanation this way:

    (In the absence of an explicit "return", a subroutine, eval, or do FILE automatically returns the value of the last expression evaluated.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980189]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2016-10-27 07:03 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (353 votes). Check out past polls.