Copyright © tutorialspoint.com

PERL return Function


Syntax

return EXPR

return


Definition and Usage

Returns EXPR at the end of a subroutine, block, or do function. EXPR may be a scalar, array, or hash value; context will be selected at execution time. If no EXPR is given, returns an empty list in list context, undef in scalar context, or nothing in a void context.

Return Value

  • Returns in Scalar Context: List, which may be interpreted as scalar, list, or void context

Example

Try out following example:

#!/usr/bin/perl -w

$retval = Sum(5,10);
print ("Return value is $retval\n" );

@retval = Sum(5,10);
print ("Return value is @retval\n" );

sub Sum($$){
   my($a, $b ) = @_; 

   my $c = $a + $b;
   
   return($a, $b, $c);
}

It will produce following results:

Return value is 15
Return value is 5 10 15

Copyright © tutorialspoint.com