Open In App
Related Articles

Perl | return() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:




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


Output:

Return value is 250
Return value is 25 10 250

Example 2:




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


Output:

Return value is 15
Return value is 25 10 15

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 07 May, 2019
Like Article
Save Article
Previous
Next
Similar Reads