Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What (are there any) languages with only pass-by-reference?

I was wondering. Are there languages that use only pass-by-reference as their eval strategy?

like image 402
Dervin Thunk Avatar asked May 26 '10 15:05

Dervin Thunk


People also ask

What languages use pass by value?

The most common are pass-by-value and pass-by-reference. C++ supports both techniques, while most other languages (Java, JavaScript, Python, etc.) use pass-by-value exclusively.

Is JavaScript a pass-by-reference language?

JavaScript is always pass-by-value. This means everything in JavaScript is a value type and function arguments are always passed by value. That being said, object types are a bit more confusing. The confusion lies in the fact that object types are reference types which are passed by value.

Is Python pass by value or reference?

Python passes arguments neither by reference nor by value, but by assignment.

Is Haskell pass-by-reference?

No, Haskell is not pass-by-reference. Nor is it pass-by-value. Pass-by-reference and pass-by-value are both strict evaluation strategies, but Haskell is non-strict.


3 Answers

I don't know what an "eval strategy" is, but Perl subroutine calls are pass-by-reference only.

sub change {
    $_[0] = 10;
}

$x = 5;
change($x);
print $x;  # prints "10"
change(0);  # raises "Modification of a read-only value attempted" error
like image 133
Sean Avatar answered Oct 01 '22 12:10

Sean


VB (pre .net), VBA & VBS default to ByRef although it can be overriden when calling/defining the sub or function.

like image 38
Alex K. Avatar answered Oct 01 '22 12:10

Alex K.


FORTRAN does; well, preceding such concepts as pass-by-reference, one should probably say that it uses pass-by-address; a FORTRAN function like:

INTEGER FUNCTION MULTIPLY_TWO_INTS(A, B)
INTEGER A, B
MULTIPLY_BY_TWO_INTS = A * B
RETURN

will have a C-style prototype of:

extern int MULTIPLY_TWO_INTS(int *A, int *B);

and you could call it via something like:

int result, a = 1, b = 100;

result = MULTIPLY_TWO_INTS(&a, &b);

Another example are languages that do not know function arguments as such but use stacks. An example would be Forth and its derivatives, where a function can change the variable space (stack) in whichever way it wants, modifying existing elements as well as adding/removing elements. "prototype comments" in Forth usually look something like

(argument list -- return value list)

and that means the function takes/processes a certain, not necessarily constant, number of arguments and returns, again, not necessarily a constant, number of elements. I.e. you can have a function that takes a number N as argument and returns N elements - preallocating an array, if you so like.

like image 30
FrankH. Avatar answered Oct 01 '22 13:10

FrankH.