lblake has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am just learning perl and I have a question about return values? In the following perl script the message is printed out with or without the 'return' function why is that?

use strict; sub HowdyEveryone { my($name1, $name2) = @_; return "Hello $name1 and $name2.\n" . "Where do you want to go with Perl today?\n"; } print &HowdyEveryone("bart", "simpson");

Replies are listed 'Best First'.
Re: Returning values
by egga (Monk) on Sep 09, 2011 at 14:13 UTC
    perlsub:

    If no return is found and if the last statement is an expression, its value is returned. If the last statement is a loop control structure like a foreach or a while , the returned value is unspecified. The empty sub returns the empty list.

Re: Returning values
by afoken (Chancellor) on Sep 10, 2011 at 06:21 UTC
    print &HowdyEveryone("bart", "simpson");

    BTW: Please don't use the Perl 4 notation for calling subs. It has a very different meaning on Perl 5. And you don't want that most of the times.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Returning values
by Anonymous Monk on Sep 09, 2011 at 16:23 UTC
    ... and it is very important to point out that the returned value can be a list, or any other structured type. If you want to return multiple things, just return a list or structure containing them.

      OK thanks for explaining this to me