?
Solved

Return Statements in subs

Posted on 2007-07-22
5
Medium Priority
?
326 Views
Last Modified: 2010-04-16
Hi guys hope you can assist.

Im just trying to learn how to use the return statement in subroutines.

I want to see how I can print out different return values from a sub, and having issues.

Here is my code:

==========================================================================

sub UsingReturn{

$First = 3;
$Second = 5;

      if ($total = $First * $Second){
            return;
      }      
      elsif ($total2 = "100"){
            return;
      }


}
UsingReturn();

print "$total\n";      
print "$total2\n";

=================================== Output:

15

Im not sure what im doing wrong here, because I can only get the first return to print.

Any help appreciated.

S
0
Comment
Question by:Simon336697
5 Comments
 
LVL 1

Assisted Solution

by:michaelshavel
michaelshavel earned 800 total points
ID: 19543216
Hi,

The way I would do it is this:

sub UsingReturn {
$First = 3;
$Second = 5;

      if ($total = $First * $Second){
            return $total;
      }      
      elsif ($total2 = "100"){
            return $total2;
      }


} ## End sub

$myreturn =UsingReturn();
print "$myreturn";

In this case, the return value will be stored in the $myreturn variable. The value of $total2 will be returned if  it equals 100, based on the code above. Only one value will be returned in the sub above though because of the if/elseif statment.

Mike



0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 200 total points
ID: 19543375
ther're couple of problems with your code:
  1. you're using global variables which survive the function call
  2. you're using an assignment within the if condition (which returns the same boolean result, obviously:)
  3. your function doesn't return anything, and you're calling it once

michaelshavel's suggestion will work arround some of these problems, but I doubt that this is what you want to achieve
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19543449
Thanks guys for your help.

AHoff, I just want to be able to print out different return values, just to get a grasp of what its doing.
0
 
LVL 48

Accepted Solution

by:
Tintin earned 1000 total points
ID: 19543770
Perhaps the following modified example will make it clearer.

#!/usr/bin/perl
use strict;
use warnings;

my $result;

$result = UsingReturn(10);
print "Result=$result\n";

$result = UsingReturn(5);
print "Result=$result\n";

sub UsingReturn {
  my $value = shift;
  return $value * 3;
}
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19544763
Thanks Tintin....that makes it really clear.

Thanks to ALL of you.

0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Six Sigma Control Plans
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question