in reply to [SOLVED] Return statement in subroutine(s) is necessary when not returning anything?

There is always something to return from a sub (in some cases, even  undef); if no  return() is explicitly stated, it'll return the value of the last expression evaluated implicitly. It'll do this even if the sub is empty. A return even if you return void (nothing) is one way to show that you specifically mean to exit it, whether due to a condition, or to emphasize where the end of your sub is supposed to be. Sometimes it's handy to say  if ! defined sub();

Replies are listed 'Best First'.
Re^2: Return statement in subroutine(s) is necessary when not returning anything?
by thanos1983 (Parson) on Nov 02, 2015 at 22:56 UTC

    Hello stevieb,

    Thank you for your time and effort reading and replying to my question. Well yeah you are right, it is easier and better way of coding to place a return; (void) to show that your subroutine ends there.

    But also as Anonymous pointed out, it is better to return (void) instead of implicit return.

    That been said I have my answer to my question. Thank you for your time and effort it helped to clear out my thought.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Yes, explicit return with void is better than implicit return. It makes your API more reliable. Later tonight I'll dig up and update this post as to why explicit exits are important as well...

        Hello again stevieb,

        Yeah that would be great, please do post anything you find. More info is always welcome. :D

        Seeking for Perl wisdom...on the process of learning...not there...yet!