Using the return statement. : Return « Subroutine « Perl






Using the return statement.

   
#!/usr/local/bin/perl 

$total = &get_total; 
if ($total eq "error") { 
   print ("No input supplied.\n"); 
} else { 
   print("The total is $total.\n"); 
} 

sub get_total { 
   $value = 0; 
   $inputline = <STDIN>; 
   $inputline =~ s/^\s+|\s*\n$//g; 
   if ($inputline eq "") { 
      return ("error"); 
   } 
   @subwords = split(/\s+/, $inputline); 
   $index = 0; 
   while ($subwords[$index] ne "") { 
      $value += $subwords[$index++]; 
   } 
   $retval = $value; 
} 

   
    
    
  








Related examples in the same category

1.The return values of the ref function: hash
2.Return reference from a function
3.Return reference to variable
4.Return two array references from a subroutine
5.Return value from subroutine reference
6.A subroutine that returns a scalar or a list.
7.A subroutine that returns a value
8.Return Value
9.Return a reference from a sub
10.Return a subroutine from a subroutine
11.Return hash value from subroutine
12.Return more than one value from subroutine
13.Return two arrays from subroutine
14.Return value from subroutine without using the return statement
15.Returning arrays from subroutines
16.Returning data from subroutines
17.The last statement is the value to return
18.Using return statement