Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return value from subroutine 1

Status
Not open for further replies.

ejaggers

Programmer
Feb 26, 2005
148
US
Is the rtn value from sub() in a special variable, so I don't have to call sub() twice?
i.e.: $x = $1 if sub();


I'm doing this: $x = sub() if sub();

I don't want $x set if sub() returns false.
 
ejaggers said:
I don't want $x set if sub() returns false

For example, you can assign the result of a function to a variable in your if statement
and then work with that variable only if it evaluates to true - see below the sub print_if_positive().

Eventually, you can pass an argument by reference to a function:
The subs func() and func2() return a logical value and change an argumment passed by reference.
So you can evaluate first the result of the function and when true, than use the changed argument.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[maroon]&print_if_positive[/maroon][red]([/red]-[fuchsia]3[/fuchsia][red])[/red][red];[/red]
[maroon]&print_if_positive[/maroon][red]([/red][fuchsia]0[/fuchsia][red])[/red][red];[/red]
[maroon]&print_if_positive[/maroon][red]([/red][fuchsia]5[/fuchsia][red])[/red][red];[/red]

[gray][i]# passing argument by reference[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$num[/blue] = -[fuchsia]5[/fuchsia][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][maroon]&func[/maroon][red]([/red]\[blue]$num[/blue][red])[/red][red])[/red] [red]{[/red]
  [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]result = [blue]$num[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[blue]$num[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][maroon]&func[/maroon][red]([/red]\[blue]$num[/blue][red])[/red][red])[/red] [red]{[/red]
  [black][b]print[/b][/black] [red]"[/red][purple]result = [blue]$num[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[blue]$num[/blue] = [fuchsia]7[/fuchsia][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][maroon]&func[/maroon][red]([/red]\[blue]$num[/blue][red])[/red][red])[/red] [red]{[/red]
  [black][b]print[/b][/black] [red]"[/red][purple]result = [blue]$num[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[gray][i]# passing input argument by value and output argument by reference[/i][/gray]
[blue]$num[/blue] = -[fuchsia]5[/fuchsia][red];[/red]
[black][b]my[/b][/black] [blue]$result[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][maroon]&func2[/maroon][red]([/red][blue]$num[/blue], \[blue]$result[/blue][red])[/red][red])[/red] [red]{[/red]
  [black][b]print[/b][/black] [red]"[/red][purple]result = [blue]$result[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[blue]$num[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[blue]$result[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][maroon]&func2[/maroon][red]([/red][blue]$num[/blue], \[blue]$result[/blue][red])[/red][red])[/red] [red]{[/red]
  [black][b]print[/b][/black] [red]"[/red][purple]result = [blue]$result[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[blue]$num[/blue] = [fuchsia]3[/fuchsia][red];[/red]
[blue]$result[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][maroon]&func2[/maroon][red]([/red][blue]$num[/blue], \[blue]$result[/blue][red])[/red][red])[/red] [red]{[/red]
  [black][b]print[/b][/black] [red]"[/red][purple]result = [blue]$result[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]


[gray][i]# *** procedures/functions ***[/i][/gray]
[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]positive[/maroon] [red]{[/red]
  [black][b]my[/b][/black][red]([/red][blue]$num[/blue][red])[/red] = [blue]@_[/blue][red];[/red]
  [gray][i]# return positive number or zero[/i][/gray]
  [url=http://perldoc.perl.org/functions/return.html][black][b]return[/b][/black][/url] [red]([/red][blue]$num[/blue] > [fuchsia]0[/fuchsia] && [blue]$num[/blue][red])[/red][red];[/red]
[red]}[/red]

[black][b]sub[/b][/black] [maroon]print_if_positive[/maroon] [red]{[/red]
  [black][b]my[/b][/black] [red]([/red][blue]$arg[/blue][red])[/red] = [blue]@_[/blue][red];[/red]
  [gray][i]# print only if result is true[/i][/gray]
  [olive][b]if[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$res[/blue] = [maroon]&positive[/maroon][red]([/red][blue]$arg[/blue][red])[/red][red])[/red] [red]{[/red]
     [black][b]print[/b][/black] [red]"[/red][purple]number = [blue]$res[/blue] is positive.[purple][b]\n[/b][/purple][/purple][red]"[/red] [red];[/red]
  [red]}[/red]
[red]}[/red]

[black][b]sub[/b][/black] [maroon]func[/maroon] [red]{[/red]
  [black][b]my[/b][/black][red]([/red][blue]$num[/blue][red])[/red] = [blue]@_[/blue][red];[/red]
  [gray][i]#print "func: numref = $num\n";[/i][/gray]
  [gray][i]#print "func: numval = ${$num}\n";[/i][/gray]
  [olive][b]if[/b][/olive] [red]([/red][blue]$[/blue][red]{[/red][blue]$num[/blue][red]}[/red] > [fuchsia]0[/fuchsia][red])[/red] [red]{[/red]
    [blue]$[/blue][red]{[/red][blue]$num[/blue][red]}[/red] = [blue]$[/blue][red]{[/red][blue]$num[/blue][red]}[/red] [blue]*[/blue] [fuchsia]100[/fuchsia][red];[/red]
    [black][b]return[/b][/black] [fuchsia]1[/fuchsia][red];[/red]
  [red]}[/red]
  [olive][b]else[/b][/olive] [red]{[/red]
    [black][b]return[/b][/black] [fuchsia]0[/fuchsia][red];[/red]
  [red]}[/red]
[red]}[/red]

[black][b]sub[/b][/black] [maroon]func2[/maroon] [red]{[/red]
  [black][b]my[/b][/black] [red]([/red][blue]$input[/blue], [blue]$output[/blue][red])[/red] = [blue]@_[/blue][red];[/red]
  [olive][b]if[/b][/olive] [red]([/red][blue]$input[/blue] > [fuchsia]0[/fuchsia][red])[/red] [red]{[/red]
    [blue]$[/blue][red]{[/red][blue]$output[/blue][red]}[/red] = [blue]$input[/blue] [blue]*[/blue] [fuchsia]10[/fuchsia][red];[/red]
    [black][b]return[/b][/black] [fuchsia]1[/fuchsia][red];[/red]
  [red]}[/red]
  [olive][b]else[/b][/olive] [red]{[/red]
    [black][b]return[/b][/black] [fuchsia]0[/fuchsia][red];[/red]
  [red]}[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

Output:
Code:
number = 5 is positive.
result = 700
result = 30
 
mikrom,
I'm not sure we're on the same page here. My question is does this: return('value') set a special variable, say $something, so I can say $x = $something in main?

Like this abc('a','b','c') put these parms in @_.
 
I would suggest doing something like this:
Code:
my $temp = sub();
$x = $temp if $temp;
... but, assuming $x has a value in it already you could try something like:
Code:
$x = map {$_ || $x} sub();
or you could replace $x with undef if that's more appropriate.
 
How about:
Code:
$x = sub() || $x;
That would maintain the pre-existing value of $x in the event that sub() returns a false value.
 
Thanks everyone for your response.

ishnid, I like use your solution.

I assume there is no special variable that contains the return values of a sub. This is actually my question.
 
No, there isn't. ishnid's solution is (as normal) the cleanest...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top