Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Doubt in functions

by vyeddula (Novice)
on Jul 06, 2012 at 00:00 UTC ( #980184=perlquestion: print w/ replies, xml ) Need Help??
vyeddula has asked for the wisdom of the Perl Monks concerning the following question:

Hi perl monks.I am new to perl programming .I am a beginner.Here is my first question

#!/usr/bin/perl use strict; use warnings; my $variable=total(1,2,3); print"the variable is :$variable\n"; sub total { my $element=0; foreach (@_) { $element=$element+$_; } element; } Output is: The variable is : 6 Now i changed the code to #!/usr/bin/perl use strict; use warnings; my $variable=total(1,2,3); print"the variable is :$variable\n"; sub total { my $element=0; foreach (@_) { $element=$element+$_; } element; print"the value of element is:$element\n"; } The output is the value of element is:6 the variable is :1 why the variable in this case is not showing as 6 is my question.

Comment on Doubt in functions
Download Code
Re: Doubt in functions
by monsoon (Pilgrim) on Jul 06, 2012 at 00:18 UTC
    Quote from http://perldoc.perl.org/perlsub.html
    "If no return is found and if the last statement is an expression, its value is returned."

      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";

      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.)
Re: Doubt in functions
by BrowserUk (Pope) on Jul 06, 2012 at 00:28 UTC

    The value 1 in the second example is the successful return code from print;


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Doubt in functions
by toolic (Chancellor) on Jul 06, 2012 at 00:50 UTC
    It is good practice to use an explicit return from a sub. See also perldoc perlsub.

    By the way, you really want to use a sigil in front of "element": $element

    Also, please use separate "code" tags so it is easier for us to distinguish your code from your output and your question.

      Thank you all for the quick response The explicit usage with return command solved my problem Thanks again

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://980184]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (14)
As of 2014-07-25 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (169 votes), past polls