价值传递结果问题 [英] Pass-By-Value-Result Problem

查看:53
本文介绍了价值传递结果问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

现在我正在大学学习编程语言。

但我有一些问题。

在教科书中。说有四个传递机制


1)按值传递(换句话说:按值调用)

2)通过引用传递(换句话说:通过参考)

3)传递价值结果< - 我对这个问题有疑问。

4)通过名字传递


传递值 - 结果

传递机制

1.参数的值被复制到参数中。

2.最后参数的值被复制回

参数。

通过价值结果的特征

-AKA copy-in,复制(复制 - 恢复)

- 没有别名问题发生(不同于这个

点的参考传递)

-The复制结果的顺序可能很重要


ex)

假设:按值传递结果

void square(int x ,int y)

{

x * = x;

y * = y;

}


main()

{

int a = 5;

int b = 10;

square(a,b);

printf(" a =%db =%d \ n",a,b);

}


输出:


a = 25 b = 100


可以理解!但是这个怎么样?


void p(int x,int y)

{

int sum,prod;

sum = x + y;

prod = x * x;

x = sum; //总和将通过x返回

y = prod; // prod将通过y返回

main()

{

int a = 5;

p(a,a); // a == ??

}


什么价值?


感谢阅读。

解决方案

ki **** @ gmail。 com 写道:


您好,

现在我正在大学学习编程语言。

但是我有一些问题。

在教科书中。说有四个传递机制


1)按值传递(换句话说:按值调用)

2)通过引用传递(换句话说:通过参考)

3)传递价值结果< - 我对这个问题有疑问。



您将在更通用的新闻组中获得更好的建议,例如

comp.programming。这个小组讨论了C中只有值可以传递给函数的价值。


-

Ben。


在文章< 11 ********************* @ x35g2000prf.googlegroups中。 com>,

< ki **** @ gmail.comwrote:


> 1)按值传递(换句话说:按价值呼叫)
2)通过引用传递(换句话说:通过引用调用)
3)传递值 - 结果< - 我对此主题有疑问。
4)按名称传递



这不是C问题,因为C使用(1)。


>特征通过价值结果
-AKA复制,复制(复制 - 恢复)
- 不会出现别名问题(与此点的参考传递不同)
- 复制结果的顺序可能很重要


> void p(int x,int y)
{
int sum,prod;
sum = x + y;
prod = x * x;
x = sum; //总和将通过x返回
y = prod; // prod将通过y返回
}
main()
{a / 5;
p(a,a); // a == ??
}

什么价值?



正如你所引用的文字所说,复制结果的顺序

可能很重要。如果有任何编程语言使用

按值和结果调用,大概它的规范将回答你的问题。很可能答案是它的实施

定义。


- Richard

-

应考虑在一些字母表中需要多达32个字符

- 1963年的X3.4。


ki **** @ gmail.com 写道:


您好,

现在我正在大学学习编程语言。



哪一个?因为我们在这里做C;我们不是一般的编程

新闻组。你会在comp.lang.misc中变得更好,我想,

或者可能是comp.compilers。


但我有一些问题。
教科书中的
。说有四个传递机制


1)按值传递(换句话说:按值调用)

2)通过引用传递(换句话说:通过参考)

3)传递值 - 结果< - 我对此主题有疑问。

4)传递名称



C有值传递,这就是你的命运。


[数组的明显例外就是这样;显而易见;它是

不是参数传递机制的一部分,它是

数组的一部分 - 使用 - 作为值 - 成为指针 - 指向 - 元素-0机制。]


所以你的语法-C值传递值结果不能在这里回答

,因为它''不是C问题,但是......


main()

{

int a = 5 ;

p(a,a); // a == ??

}


什么价值?



这将取决于/实际语言/声称具有

值传递结果。因为我们不知道它是什么(但是

我们知道它不是C,并且可能承认知道它不是
C ++) ,我们不能说,这样做对我们来说是不合时宜的。


(fx:OT)


如果是C像Goldschmidt一样突变,有价值传递的结果

参数作为一个选项 - 我认为这个事件很可能是通过铁路到月球或者b $ b b Buffy过着漫长而幸福的生活 -

然后我会期望它说如果任何价值结果

一个函数的参数重叠了结果是未定义。

C'就是这样。


-

Uenet上没有元音的元素。


Hewlett-Packard Limited注册号:

注册办事处:Cain Road,Bracknell,Berks RG12 1HN 690597英格兰


Hello,
now I''m learning progamming language in university.
but i have some question.
in textbook. says there are four passing Mechanism

1) pass by value (inother words : call by value)
2) pass by reference (inother words: call by reference)
3) pass by value-result <- i have question this subject .
4) pass by name

pass by value-result
passing mechanism
1.The values of the arguments are copied into the fomal parameters.
2.The final values of the parameters are copied back out to the
arguments.
Characteristics of the Pass by Value-Result
-AKA copy-in , copy-out(copy-restore)
-no alias problem occurs(differ to the pass by reference in this
point)
-The order of copying the results may be important

ex)
Assume: pass by value-result
void square(int x,int y)
{
x*=x;
y*=y;
}

main()
{
int a=5;
int b=10;
square(a,b);
printf("a = %d b = %d\n",a,b);
}

output:

a=25 b=100

can understand! but what about this?

void p(int x,int y)
{
int sum,prod;
sum=x+y;
prod=x*x;
x=sum; //sum will be returned via x
y=prod; // prod will be returned via y
}
main()
{
int a=5;
p(a,a); // a == ??
}

What value of a?

Thanks to read.

解决方案

ki****@gmail.com writes:

Hello,
now I''m learning progamming language in university.
but i have some question.
in textbook. says there are four passing Mechanism

1) pass by value (inother words : call by value)
2) pass by reference (inother words: call by reference)
3) pass by value-result <- i have question this subject .

You will get better advice in a more general newsgroup like
comp.programming. This group discusses C in which only values can be
passed to functions.

--
Ben.


In article <11*********************@x35g2000prf.googlegroups. com>,
<ki****@gmail.comwrote:

>1) pass by value (inother words : call by value)
2) pass by reference (inother words: call by reference)
3) pass by value-result <- i have question this subject .
4) pass by name

This isn''t a C question, because C uses (1).

>Characteristics of the Pass by Value-Result
-AKA copy-in , copy-out(copy-restore)
-no alias problem occurs(differ to the pass by reference in this
point)
-The order of copying the results may be important

>void p(int x,int y)
{
int sum,prod;
sum=x+y;
prod=x*x;
x=sum; //sum will be returned via x
y=prod; // prod will be returned via y
}
main()
{
int a=5;
p(a,a); // a == ??
}

What value of a?

As it says in the text you quoted, "the order of copying the results
may be important". If there''s any programming language that uses
call-by-value-and-result, presumably its specification will answer
your question. Quite likely the answer will be "it''s implementation
defined".

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


ki****@gmail.com wrote:

Hello,
now I''m learning progamming language in university.

Which one? Because we do C here; we''re not a general prog-lang
newsgroup. You''d be better off in comp.lang.misc, I think,
or possibly comp.compilers.

but i have some question.
in textbook. says there are four passing Mechanism

1) pass by value (inother words : call by value)
2) pass by reference (inother words: call by reference)
3) pass by value-result <- i have question this subject .
4) pass by name

C has pass by value, and that''s your lot.

[The apparent exception for arrays is just that; apparent; it''s
not part of the parameter-passing mechanism, it''s part of the
arrays-used-as-values-become-pointers-to-element-0 mechanism.]

So your syntactically-C pass-by-value-result can''t be answered
here, because it''s not a C question, but ...

main()
{
int a=5;
p(a,a); // a == ??
}

What value of a?

That would depend on the /actual language/ claiming to have
pass-by-value-result. Since we don''t know what it is (but
we do know it isn''t C, and might admit to knowing it isn''t
C++), we can''t say, and it would be atopical of us to do so.

(fx:OT)

If C mutated, Goldschmidt-like, to have pass-by-value-result
arguments as an option -- an event I regard about as likely as
railways to the moon or Buffy living a long and happy life --
then I''d expect it to say that if any of the value-result
arguments to a function overlapped The Results Are Undefined.
C''s like that.

--
There'' no hortage of vowel on Uenet.

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


这篇关于价值传递结果问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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