Take the 2-minute tour ×

is PHP and C++ the only 2 places that we need to careful about passing a simple data type variable as a function argument and the value can be changed?

such as

$count = 2;
echo $count;

and the 3rd line, echo $count display something other than 2. I only know of PHP and C++ where it can happen. Is there any other place where it can happen?

Update: that is, what looks like "pass by value" is in fact "pass by reference". If it is passing an object in Java, Perl, PHP, Python, and Ruby, it is automatically pass-by-reference and the instance variables' values of the object can be changed. What about passing in non-object?

share|improve this question

4 Answers 4

up vote 1 down vote accepted

Many other languages support such "pass by reference" -- Perl, Fortran, Pascal, Visual Basic, ...

Which ones you care about, well, it's hard for us to say!-)

Edit: In your update, you put Java and Python in the same category, but that's wrong: you could not reproduce your example with (Java):

  int count = 2;
  /* count here is CERTAIN to be still 2 */

or (Python):

count = 2
# count here is CERTAIN to be still 2

Python and Java's style is sometimes known as "by object reference" (as opposed to "by variable reference"): what's passed (or assigned) is a reference to the object (so IF the object has mutators the callee can invoke those mutators and thus alter the object), NOT to a "variable" (so that after the call the variable, if any, used to pass the argument, still refers to the same object, never to another object). This distinction is most clear to see with immutable objects, such as (in both Java and Python) numbers and strings.

share|improve this answer
in Java and Python, can't you pass a "Car" object to a function, and inside that function, alter the "mileage" property of the car object so that when the function returns, mileage is changed? –  動靜能量 Jun 2 '09 at 9:34
Yes, if the object is mutable, you can alter it in the callee -- it's still the same object (same identity), but by definition a mutable object is one whose state can be altered (without identity change). That's quite a distinct concept from (say) assigning a distinct object to a certain variable. –  Alex Martelli Jun 2 '09 at 15:46

Following the discussion above, recently I learnt that y programming language design use of the term "pass-by-reference" is rather very strict, and for example in java is in fact just pass-by-value, strictly.

Either, you pass a primitive by value (clear to everybody I supposed) or you pass a reference to an object by value. Ok that I understand now yet consider the following perl code.

sub addFoo {
  my ($arrayRef) = @_;
  push @{$arrayRef},"foo"; 

my @array = (1,2,3);
my $arrayRef; # FUTURE reference (equivalent to pointer C) to @array.

addFoo($arrayref = \@array); #1.
addFoo(\@array); #2.
addFoo($arrayRef); #3.

print join(" ",@array),"\n"; # result "1 2 3 foo foo foo\n";

My question if which of three alternative calls to addFoo, is considered pass-by-reference as opposed to pass-A-reference? Seems that the answer is clear for the number three, but I cannot say for number #1. #2. as are doing exactly the same. At least the number 2 is considered explicitly as pass-by-reference in some perl man pages and web-sites.

share|improve this answer

Back in the old days, FORTRAN passed constants by reference. You could pass the constant 2 to a subroutine and the subroutine could assign 3 to it. Then after returning, this code

x = 2 + 2

would set x to 6. I don't know if modern FORTRAN compilers allow that to happen.

share|improve this answer

You can't alter a variable in C++ unless you explicitly define it as a reference. If you have regular (non reference) arguments, they are copies and can not be changed. If you read and understand the API, then there's no need to be careful about passing a simple data type variable as a function arguments.

void func(type & arg);

can be changed and

void func(type arg);

can't. And usually in C++ it won't be a reference argument unless the API want's the value to be changed in the first place (output parameter).

And as Alex already mentioned there are many languages that support the pass by reference idiom.

You would only need to be careful if a language makes it not clear if a variable is passed by value or by reference.

share|improve this answer
In this C++ example the call to the function "func(x)" looks the same whether a value or a reference is being passed. As lothar says, you NEED to read and understand the API -- is it void func(type & arg) or void func(type arg)? If the function takes a reference and you need to pass by value, I think there's a way you can invoke the copy constructor -- is it "func(type(x))"? –  Mark Lutton Jun 12 '09 at 12:24

Your Answer


