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

Hey Monks
I was wondering what is the syntax you would use to get a subroutine to return a value and how would you assign that value to a variable in the routine that called it?
I presume it'd be something like this but am not sure.

sub main{ my $answer = &add(1,2); print $answer; } sub add{ my $value1 = $_[0]; my $value2 = $_[1]; my $result = ($value1 + $value2); return $answer; }

Thanks

j o h n i r l .

Sum day soon I'Il lern how 2 spelI (nad tYpe)

Replies are listed 'Best First'.
Re: Returning Values from Subroutines
by ignatz (Vicar) on Sep 06, 2002 at 15:17 UTC
    One thing to know about Perl subroutines is that you don't need to specify a return value, since Perl automatically returns the last expression evaluated. So sometimes you'll see something like:
    sub add{ my $value1 = $_[0]; my $value2 = $_[1]; my $result = ($value1 + $value2); }
    or
    sub add{ my $value1 = $_[0]; my $value2 = $_[1]; ($value1 + $value2); }
    or even
    sub add{ ($_[0] + $_[1]); }
    all of which return the same result.

    I would probably do it as

    sub add{ return ($_[0] + $_[1]); }
    so that it is clear what I am trying to return. But as the Scarecrow said, "People do tend to go both ways."
    ()-()
     \"/
      `                                                     
    
Re: Returning Values from Subroutines
by TStanley (Canon) on Sep 06, 2002 at 14:39 UTC
    You're close:
    sub main{ my $answer = &add(1,2); print $answer; } sub add{ my $value1 = shift; my $value2 = shift; my $result = ($value1 + $value2); return $result; }

    TStanley
    --------
    It is God's job to forgive Osama Bin Laden. It is our job to arrange the meeting -- General Norman Schwartzkopf
Re: Returning Values from Subroutines
by japhy (Canon) on Sep 06, 2002 at 14:45 UTC
    Your add() function stores the sum in $result, but you're trying to return $answer. Replace return $answer with return $result in add(), and you'll be fine.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Returning Values from Subroutines
by derby (Abbot) on Sep 06, 2002 at 14:42 UTC
    The relevant paragraph from perlsub:

    The return value of a subroutine is the value of the last expression evaluated. More explicitly, a "return" state­ment may be used to exit the subroutine, optionally speci­fying the returned value, which will be evaluated in the appropriate context (list, scalar, or void) depending on the context of the subroutine call. If you specify no return value, the subroutine returns an empty list in list context, the undefined value in scalar context, or nothing in void context. If you return one or more aggregates (arrays and hashes), these will be flattened together into one large indistinguishable list.

    So yes, the way you show is one way to do it. err ... but return $result.

    -derby

Re: Returning Values from Subroutines
by mojobozo (Monk) on Sep 06, 2002 at 14:51 UTC
    Hey there. I'm not entirely sure why it's not working as is as I am but a novice, but the following works for me:
    #!/usr/local/bin/perl my $answer = &add(1,2); print $answer; sub add{ return $result = ($_[0] + $_[1]); }

    As far as the original not working, will the initial sub ("main") even run without a call to it? I tried running it with a call for it and got an error. Also, in the the "add" sub, you are returning $answer, but it was never assigned a value in the sub.

    Anyone else want to chime in? I'd like to hear more on this one as well.

    mb++

    was hoping to get first return, but was beat out by six minutes... curses!
      A little more golf on this one:
      #!/usr/local/bin/perl print add(1,2); sub add{ return $result = ($_[0] + $_[1]); }

      Unless, of course, you wanted to save the value returned.
      mb
        And, using the example in ignatz's reposnse, even more strokes shaved:
        #!/usr/local/bin/perl print add(1,2); sub add{ return ($_[0] + $_[1]); }

        Man, this is getting fun. I wish I could shave this many strokes of my discgolf game!
        _____________________________________________
        mojobozo
        word (wûrd)
        interj. Slang. Used to express approval or an affirmative response to
        something. Sometimes used with up. Source

        In 9 strokes:

        # 123456789 sub add{pop()+pop}

        — Arien

Re: Returning Values from Subroutines
by fglock (Vicar) on Sep 06, 2002 at 14:40 UTC

    your sum is in $result, so return $result

    update: no need for '&' before add