Perl return Function

Advertisements


Description

This function 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.

Syntax

Following is the simple syntax for this function:

return EXPR

return

Return Value

This function returns in Scalar Context: List, which may be interpreted as scalar, list, or void context.

Example

Following is the example code showing its basic usage:

#!/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);
}

When above code is executed, it produced following result

Return value is 15
Return value is 5 10 15


Advertisements
Advertisements