Strange. I'd consider that as call-by-reference, but I'm probably wrong. But (as far as I can see) it doesn't have all the messy syntax you'd normally use when forcing call-by-reference in a call-by-value default (by passing, as the value, the value of the reference to the original value, IYSWIM). And, in my mind, that means that it's a call-by-reference. I still have to get my mind around what that Wiki text says.
[Ah, ninjaed by MadocComadrin, while messing about with the following... Those are better descriptions.]
Here, want some Perl? I like Perl. It's my favourite 'hack something up' language. Probably why I've not tried to get into Python, too much, yet. It's probably ugly and hard to read, but I've put some comments (anything after "#" on a line, or lines starting with "#").
#!/bin/perl
use strict;
use warnings;
# Procedures we'll use
sub Swap1 { # A call-by-reference example (using explicit references)
my ($ref1,$ref2) = @_; # Assign the first two parameters to $ref1 and $ref2, if any. No error-checking being done.
my $temp = $$ref1; # Variable $temp contains whatever '$' value that $ref1 may reference
$$ref1 = $$ref2; # The '$' value that $ref1 references is set to whatever the '$' value is that $ref2 references
$$ref2 = $temp; # The '$' value that $ref2 references is set to whatever value $temp /actually is/.
return 0; # (Unnecessary, but a placeholder for success...
# We could have tested whether we have two arguments at the start,
# and that both are actually references, and on failure return something else...)
} # end of sub Swap1
sub Swap2 { # A call-by-value example (takes values and returns values)
my ($val1,$val2) = @_; # Just take whatever values we want (values, references, doesn't care);
my $temp = $val1; # Copy the $val1 value into $temp;
$val1 = $val2; # Copy the $val2 value into $val1;
$val2 = $temp; # Copy the $temp value in $val2;
return ($val1,$val2); # return the two values back to the caller
} # end of sub Swap2
sub Swap2b { # Another
my ($val1,$val2) = @_; # Get the idea?
return ($val2,$val1); # Even easier.
} # end of sub Swap2b
sub Swap2c {
return reverse @_; # Functionally the same as Swap2 and Swap2b, if /exactly/ two parameters passed
} # end of sub Swap2c
sub DontSwap { return @_; } # end of DontSwap. Silly thing to do, but...
sub Swap3 {
# This time, let's just operate /directly/ on the "@_" import-variable...
my $temp = $_[0]; # The zero-indexed (first item) is copied to $temp
$_[0] = $_[1]; # the one-indexed (second item) is copied onto the zero-indexed one
$_[1] = $temp; # Guess...
return 0; # Yeah, we should have tested to make sure the above doesn't error,
# so we might as well not have bothered returning anything /either/
} # end of sub Swap3
# And now, in use...
my ($first,$second) = ("Item A", "Item B"); # Assigns "Item A" to the variable $first, and "Item B to the variable $second
print "First is '$first' and second is '$second' (Initially!)\n";
#???? "First is 'Item A' and second is 'Item B'"
Swap1(\$first,\$second); # The *reference* to $first and the *reference* to $second are passed
print "First is '$first' and second is '$second' (After Swap1!)\n";
#???? "First is 'Item B' and second is 'Item A'" (They swapped!)
Swap2($first,$second); # The value of $first and the value to $second are passed
print "First is '$first' and second is '$second' (After Swap2 -wrongly!)\n";
#???? "First is 'Item B' and second is 'Item A'" (They /stayed/ swapped, they changed internally only)
($first,$second) = Swap2($first,$second); # The value of $first and the value to $second are passed,
# and the resulting array from "Swap2" is applied back to $first and $second
print "First is '$first' and second is '$second' (After Swap2 - correctly!)\n";
#???? "First is 'Item A' and second is 'Item B'" (This resulted in being back-swapped)
($first,$second) = Swap2b($first,$second); # The value of $first and the value to $second are passed,
# and the resulting array from "Swap2b" is applied back to $first and $second
print "First is '$first' and second is '$second' (After Swap2b!)\n";
#???? "First is 'Item B' and second is 'Item A'" (This resulted in being re-swapped)
($first,$second) = Swap2c($first,$second); # The value of $first and the value to $second are passed,
# and the resulting array from "Swap2c" is applied back to $first and $second
print "First is '$first' and second is '$second' (After Swap2c!)\n";
#???? "First is 'Item A' and second is 'Item B'" (This resulted in being back-swapped, again!)
($second,$first) = DontSwap($first,$second); # The value of $first and the value to $second are passed,
# and the resulting array from "DontSwap" is applied back to $second (first)
# and /then/ $first (second)...
print "First is '$first' and second is '$second' (After DontSwap!)\n";
#???? "First is 'Item B' and second is 'Item A'" (This resulted in being re-re-swapped!)
Swap3($first,$second); # This version does a tricky call-by-reference sort of thing, without needing "\$references" defining!
print "First is '$first' and second is '$second' (After Swap3!)\n";
#???? "First is 'Item A' and second is 'Item B'" (Ooh look, back to normal)
First is 'Item A' and second is 'Item B' (Initially!)
First is 'Item B' and second is 'Item A' (After Swap1!)
First is 'Item B' and second is 'Item A' (After Swap2 -wrongly!)
First is 'Item A' and second is 'Item B' (After Swap2 - correctly!)
First is 'Item B' and second is 'Item A' (After Swap2b!)
First is 'Item A' and second is 'Item B' (After Swap2c!)
First is 'Item B' and second is 'Item A' (After DontSwap!)
First is 'Item A' and second is 'Item B' (After Swap3!)
Whoops, nearly forgot... Consider the following as well...
my ($third,$fourth) = ("Item C", "Item D"); # yadayadayada
my $thirdRef=\$third; my $fourthRef=\$fourth;
Swap2($thirdRef,$fourthRef); # This won't do anything interesting
print "Third is $third, Fourth is $fourth\n";
# Expect Third as Item C, Fourth as Item D, still
($thirdRef,$fourthRef)=Swap2($thirdRef,$fourthRef); # We're sending and swapping the /references/!!!
print "Third is $third, Fourth is $fourth\n";
# Expect Third as Item C, Fourth as Item D, still, because $third and $fourth weren't changed, but...
print "Third's apparent Reference is pointing at $$thirdRef, and Fourth's is pointing at $$fourthRef\n";
# Expect Third's Referenced thing as Item D, Fourth's as Item C... swapped!
Swap3($thirdRef,$fourthRef);
print "Third's apparent Reference is pointing at $$thirdRef, and Fourth's is pointing at $$fourthRef\n";
# Expect Third's Referenced thing as Item C, Fourth's as Item D... swapped back!
Third is Item C, Fourth is Item D
Third is Item C, Fourth is Item D
Third's apparent Reference is pointing at Item D, and Fourth's is pointing at Item C
Third's apparent Reference is pointing at Item C, and Fourth's is pointing at Item D
Yes, it's messy, and I stuck purely to Function Orientated code... Anyway, don't mind me.