Perl | return() Function

return() function in Perl returns Value at the end of a subroutine, block, or do function. Returned value might be scalar, array, or a hash according to the selected context.

Syntax: return Value

Returns:
a List in Scalar Context

Note: If no value is passed to the return function then it returns an empty list in the list context, undef in the scalar context and nothing in void context.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl -w
  
# Subroutine for Multiplication
sub Mul($$) 
{
    my($a, $b ) = @_;  
    my $c = $a * $b;
      
    # Return Value    
    return($a, $b, $c);
}
  
# Calling in Scalar context
$retval = Mul(25, 10);
print ("Return value is $retval\n" );
  
# Calling in list context
@retval = Mul(25, 10);
print ("Return value is @retval\n" );

chevron_right


Output:

Return value is 250
Return value is 25 10 250

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl -w
  
# Subroutine for Subtraction
sub Sub($$) 
{
    my($a, $b ) = @_
      
    my $c = $a - $b;
      
    # Return Value    
    return($a, $b, $c);
}
  
# Calling in Scalar context
$retval = Sub(25, 10);
print ("Return value is $retval\n" );
  
# Calling in list context
@retval = Sub(25, 10);
print ("Return value is @retval\n" );

chevron_right


Output:

Return value is 15
Return value is 25 10 15


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.