List Info

Thread: call-by-value




call-by-value
country flaguser name
United States
2007-03-12 12:17:37
 Hi everyone:

I wrote a sample script and want to see how call-by-value
works.

my understanding is that once variable is be localized by my
function,
a copy of the value of argument is being send to
function/subroutine.

Here is my sample script:


use strict;
use warnings;

sub addition
{
#$_<0> = $_<0> + 1;

$a = $a + 1;
print "in subroutine $a value changed to: $an";

}

my $a = 5;

&addition($a);

print "after subroutine the value of $a is: ",
$a;

I tried two options
a) I used _ arrary to access argument and increment it by 1.
the
result is that value of '$a' changed in main block as well.

b) I used $a = $a + 1 in subroutine, the output is

"Use of uninitialized value in addition (+) at
C:CIS068Echap10call_by_value.pl line 12.
in subroutine $a value changed to: 1
after subroutine the value of $a is: 5 " I am sure if
option b)
demonstrated purpose of call-by-value.

Plase give me some suggestion on why a) didn't work,
wheather b) is a right approach to demonstrate the purpose
of
call-by-value. Thanks!

 Jeff
 3-12-07



------------------------ Yahoo! Groups Sponsor
--------------------~--> 
Yahoo! Groups gets a make over. See the new email design.
http://us.click.yahoo.com/hOt0.A/lOaOAA/yQLSAA/ndFolB/TM

------------------------------------------------------------
--------~-> 

Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )