哪种语言允许您更改参数的值? [英] which language allows you to change an argument's value?

查看:50
本文介绍了哪种语言允许您更改参数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道哪种语言允许你更改参数的值?

喜欢:


foo( & a){

a = 3

}


n = 1

print n


foo(n)#传入n,而不是& n

打印n


现在n将是3.我认为C ++和PHP可以让你这样做,使用

他们的引用(别名)机制。而C,Python和Ruby可能不会让你这么做。 Java和Perl怎么样?


有什么方法可以防止函数改变参数'

的值?

不是我传入的内容,该功能可以修改它如果我没有传递我的论点地址,那么这不是一种可取的行为吗?对于一个

的事情,如果我们使用一个模块,并调用该模块中的一些函数,并且

模块的作者对他的代码进行了一些更改,那么我们无法知道我们传入的内容可能会发生变化。
当然,如果它是在
Java,Python和Ruby中,我们传入对象的引用(而不是C +

+'的别名引用的含义) ,所以对象可以被改变,但是当b = n时,可以预期和/或
相比,当n = 1时,即使它是
Ruby,当一切都是对象,当n = 1时传递n不会

使n成为3.有没有办法防止它发生在允许它的

语言中?


I wonder which language allows you to change an argument''s value?
like:

foo(&a) {
a = 3
}

n = 1
print n

foo(n) # passing in n, not &n
print n

and now n will be 3. I think C++ and PHP can let you do that, using
their reference (alias) mechanism. And C, Python, and Ruby probably
won''t let you do that. What about Java and Perl?

is there any way to prevent a function from changing the argument''s
value?

isn''t "what i pass in, the function can modify it" not a desireable
behavior if i am NOT passing in the address of my argument? For one
thing, if we use a module, and call some functions in that module, and
the module''s author made some changes to his code, then we have no way
of knowing what we pass in could get changed. Of course, if it is in
Java, Python, and Ruby, and we pass in a reference to object (not C+
+''s meaning of alias reference), so the object can get changed, but
that can be expected, vs passing in n, when n = 1. Even when it is
Ruby, when everything is an object, passing n in when n = 1 won''t ever
make n become 3. Is there a way to prevent it from happening in the
languages that allows it?

推荐答案

30 sep,12:47,Summercool< Summercooln ... @ gmail.comwrote:
On 30 sep, 12:47, Summercool <Summercooln...@gmail.comwrote:

我想知道哪种语言允许你更改参数的值?

喜欢:


foo (& a){

a = 3


}


...

有什么方法可以防止函数改变参数'

的价值?

...

有没有办法以防止它发生允许它的

语言?
I wonder which language allows you to change an argument''s value?
like:

foo(&a) {
a = 3

}

...
is there any way to prevent a function from changing the argument''s
value?
...
Is there a way to prevent it from happening in the
languages that allows it?





当然在C ++中,不修改参数值的函数应该是

(我宁愿说必须)等待CONST参考或值:


// const ref

foo(const T& a ){

a = 3; //错误


}

//价值

foo(T a){

a = 3; //好的,但只修改形式参数:参数

(又名实际参数)没有改变


}


现在,如果你想阻止一个功能(来自你正在使用的库)

来修改它......呃,你不应该:一个好的图书馆永远不会

如果不需要修改,则等待非const参数。所以在

这个案例中有没有办法防止它发生是b / b
无关紧要:你可以复制这个对象,但如果

功能的目的是修改它,那就毫无意义了。


9月30日凌晨3:47,Summercool< Summercooln ... @ gmail.comwrote:
On Sep 30, 3:47 am, Summercool <Summercooln...@gmail.comwrote:

I想知道哪种语言允许你更改参数的价值?

喜欢:


foo(& a){

a = 3


}


n = 1

打印n


foo(n)#传入n,而不是& n

打印n

现在n将为3.我认为C ++和PHP可以让你这样做,使用

他们的引用(别名)机制。而C,Python和Ruby可能不会让你这么做。 Java和Perl怎么样?


有什么方法可以防止函数改变参数'

的值?

不是我传入的内容,该功能可以修改它如果我没有传递我的论点地址,那么这不是一种可取的行为吗?对于一个

的事情,如果我们使用一个模块,并调用该模块中的一些函数,并且

模块的作者对他的代码进行了一些更改,那么我们无法知道我们传入的内容可能会发生变化。
当然,如果它是在
Java,Python和Ruby中,我们传入对象的引用(而不是C +

+'的别名引用的含义) ,所以对象可以被改变,但是当b = n时,可以预期和/或
相比,当n = 1时,即使它是
Ruby,当一切都是对象,当n = 1时传递n不会

使n成为3.有没有办法防止它发生在允许它的

语言中?
I wonder which language allows you to change an argument''s value?
like:

foo(&a) {
a = 3

}

n = 1
print n

foo(n) # passing in n, not &n
print n

and now n will be 3. I think C++ and PHP can let you do that, using
their reference (alias) mechanism. And C, Python, and Ruby probably
won''t let you do that. What about Java and Perl?

is there any way to prevent a function from changing the argument''s
value?

isn''t "what i pass in, the function can modify it" not a desireable
behavior if i am NOT passing in the address of my argument? For one
thing, if we use a module, and call some functions in that module, and
the module''s author made some changes to his code, then we have no way
of knowing what we pass in could get changed. Of course, if it is in
Java, Python, and Ruby, and we pass in a reference to object (not C+
+''s meaning of alias reference), so the object can get changed, but
that can be expected, vs passing in n, when n = 1. Even when it is
Ruby, when everything is an object, passing n in when n = 1 won''t ever
make n become 3. Is there a way to prevent it from happening in the
languages that allows it?



有人会说,在真正优秀的OO设计中,状态应该是受对象保护的(不是引用的常量)

对象)

Some would say that in truely good OO design, the state should be
protected by the object (not the constness of the reference to the
object)


.. oO(Summercool)
..oO(Summercool)

>我认为在Pascal和C中,我们永远不会修改
参数,除非我们明确允许它,通过传入参数的
指针(地址)。
>I think in Pascal and C, we can never have an
argument modified unless we explicitly allow it, by passing in the
pointer (address) of the argument.



Pascal还允许通过引用传递,这是在声明函数参数时使用关键字

''var''完成的。 Object Pascal还允许

const参数。


Micha

Pascal also allows passing by reference, which is done with the keyword
''var'' when declaring the function parameters. Object Pascal also allows
const parameters.

Micha


这篇关于哪种语言允许您更改参数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆