|
List Info
Thread: subroutine - variable scope:
|
|
| subroutine - variable scope: |
  United States |
2008-03-18 18:25:52 |
|
hello everyone:
I want to pass two numbers to a subroutine. within the subroutine add
two numbers and return the sum to calling function.
I have two files my_main.pl and my_sub.pl. the parameter's value
didn't pass to the subroutine. sum's value is always
zero.
I think i may not understood the value scope well in perl. Please
check my code and tell me how can I make this work.
Thanks for your help!
my_main.pl
#!/usr/bin/perl
use strict;
use warnings;
require "my_sub.pl";
my $x = 5;
my $y = 7;
my $result = addition($x, $y);
print $result;
my_sub.pl file
#!/usr/bin/perl
#a sub-routine used in my_main.pl
#Illegal character in prototype for main::addition $a, $b at
my_sub.pl line 11
#
use strict;
use warnings;
my $num1 = 0;
my $num2 = 0;
sub addition($num1,$num2)
{
my $answer = $num1 + $num2;
print "$answer is: $answer.n";
return $answer;
}
1;
__._,_.___
.
__,_._,___
|
| Re: subroutine - variable scope: |
  United States |
2008-03-18 19:09:23 |
|
__,_._,___
|
| Re: subroutine - variable scope: |
  United States |
2008-03-19 00:08:27 |
|
--- In perl-beginner%40yahoogroups.com">perl-beginner yahoogroups.com, "Jeff Shu" <santa98bn ...>
wrote:
>
> hello everyone:
> I want to pass two numbers to a subroutine. within the subroutine
add
> two numbers and return the sum to calling function.
>
> I have two files my_main.pl and my_sub.pl. the parameter's value
> didn't pass to the subroutine. sum's value is always
> zero.
>
> I think i may not understood the value scope well in perl. Please
> check my code and tell me how can I make this work.
> Thanks for your help!
>
> my_main.pl
>
> #!/usr/bin/perl
>
>
> use strict;
> use warnings;
>
> require "my_sub.pl";
>
> my $x = 5;
> my $y = 7;
>
> my $result = addition($x, $y);
> print $result;
>
>
>
> my_sub.pl file
>
> #!/usr/bin/perl
> #a sub-routine used in my_main.pl
>
> #Illegal character in prototype for main::addition $a, $b at
> my_sub.pl line 11
> #
>
> use strict;
> use warnings;
>
> my $num1 = 0;
> my $num2 = 0;
>
> sub addition($num1,$num2)
> {
> my $answer = $num1 + $num2;
> print "$answer is: $answer.n";
> return $answer;
>
> }
>
> 1;
>I'm a newbie at this but, if the sub returns '$answer'
shouldn't the main use '$answer' rather than '$result' ?
__._,_.___
.
__,_._,___
|
| Re: subroutine - variable scope: |
  United States |
2008-03-19 00:09:47 |
|
--- In perl-beginner%40yahoogroups.com">perl-beginner yahoogroups.com, "Jeff Shu" <santa98bn ...>
wrote:
>
> hello everyone:
> I want to pass two numbers to a subroutine. within the subroutine
add
> two numbers and return the sum to calling function.
>
> I have two files my_main.pl and my_sub.pl. the parameter's value
> didn't pass to the subroutine. sum's value is always
> zero.
>
> I think i may not understood the value scope well in perl. Please
> check my code and tell me how can I make this work.
> Thanks for your help!
>
> my_main.pl
>
> #!/usr/bin/perl
>
>
> use strict;
> use warnings;
>
> require "my_sub.pl";
>
> my $x = 5;
> my $y = 7;
>
> my $result = addition($x, $y);
> print $result;
>
>
>
> my_sub.pl file
>
> #!/usr/bin/perl
> #a sub-routine used in my_main.pl
>
> #Illegal character in prototype for main::addition $a, $b at
> my_sub.pl line 11
> #
>
> use strict;
> use warnings;
>
> my $num1 = 0;
> my $num2 = 0;
>
> sub addition($num1,$num2)
> {
> my $answer = $num1 + $num2;
> print "$answer is: $answer.n";
> return $answer;
>
> }
>
> 1;
>I'm a newbie at this but, if the sub returns '$answer'
shouldn't the main use '$answer' rather than '$result' ?
__._,_.___
.
__,_._,___
|
| RE: Re: subroutine - variable scope: |
  Hungary |
2008-03-19 03:23:30 |
|
__,_._,___
|
[1-5]
|
|
|
about | contact Other archives ( Real Estate discussion Medical topics )
|