in reply to Returning all data from subroutine

excuse me for the idiotic comment, but i have tried to do the same as the OP and return data from a sub. like say this code for instance:
use warnings; use strict; use diagnostics; my $string = ""; add_i_to_string($string); sub add_i_to_string { my ($string) = @_; my $new_string = $string . "i"; add_am_to_string($new_string); } sub add_am_to_string { my ($string) = @_; my $new_string = $string . " am"; add_ironman_to_string($new_string); } sub add_ironman_to_string { my ($string) = @_; my $new_string = $string . " ironman"; string_print($new_string); } sub string_print { my ($string) = @_; print $string; } #prints "i am ironman"
now my question is, would it not be better to just pass the data to another sub for printing, or is there a specific reason we should be returning data outside of a sub instead of just passing it to another?

Replies are listed 'Best First'.
Re^2: Returning all data from subroutine
by GrandFather (Saint) on Nov 05, 2014 at 02:40 UTC

    Your question makes no sense. In the code you show there is no point at which you are explicitly returning data from any of your subs and at no point is implicitly returned data from any sub used. Maybe you meant something like:

    use warnings; use strict; print addIronman(addAm(addI(""))); sub addI { my ($string) = @_; $string . "i"; } sub addAm { my ($string) = @_; return $string . " am"; } sub addIronman { my ($string) = @_; return "$string ironman"; }

    Note that addI returns its result implicitly because the result is the last value evaluated before the implicit return at the end of the sub. The other subs return explicit values.

    Perl is the programming world's equivalent of English
      i was trying to reason in my post, what would be the reason for returning anything from any sub if it can be dealt with in a sub? i think that is the question i was shooting for even tho my code didnt even show it. hopefully you see what i mean tho. EDIT: very good example code tho.

        It is good programming practice to have such a subroutine report back if it was able to successfully perform its task, or pass back any exceptions or error messages that were generated.

        1 Peter 4:10
Re^2: Returning all data from subroutine
by AnomalousMonk (Archbishop) on Nov 05, 2014 at 03:20 UTC

    Note that GrandFather's reply (++) includes an example of what I think of as 'classic' functional programming (update: actually, I think this would more formally be called procedural programming). Open almost any introductory CS textbook (hint, hint) and you will quickly encounter further, extensive discussion of this topic.

Re^2: Returning all data from subroutine
by Laurent_R (Canon) on Nov 05, 2014 at 08:21 UTC
    You are passing an argument to your subroutines, but using $string essentially as a global variable. That is defeatring the purpose of funnctions receiving arguments and returning values, which can thus be abstracted out as black boxes.