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

爷,独闯天下 提交于 2019-12-10 16:55:12

问题


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


回答1:


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



回答2:


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




回答3:


How about Brainfuck?




回答4:


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.



来源:https://stackoverflow.com/questions/2914181/what-are-there-any-languages-with-only-pass-by-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!