perl6中函数参数(1)
sub F($number is copy){ $number++; say $number; } F(5);
#下面是错误的
sub F($number){ $number++; say $number; } F(5)
默认情况下, 参数不可更改,如果要更改, 可以加 is copy 或 is rw
又或者, 你用原来的参数名字, 像下面这样:
sub F($number){ my $swap = $number; $swap++; say $number; say $swap; } F(5)
is copy 跟 is rw的差别
is copy相当于copy一份, 而is rw, 相当于可读写, 参数还是原来的, 这可想像成C中的指针:
sub F($number is copy){ $number++; say $number; } sub G($number is rw){ $number++; say $number; } my $value = 10; F($value); say $value; G($value); say $value;