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.
|
|
0
|
|
|
|
Reply
|
kinaxx (3)
|
6/5/2007 2:59:02 PM |
|
On Tue, 05 Jun 2007 07:59:02 -0700
kinaxx@gmail.com wrote:
> pass by value-result
Note this:
> -The order of copying the results may be important
Think clearly about it and your problem will be solved.
--
C:>WIN | Directable Mirror Arrays
The computer obeys and wins. | A better way to focus the sun
You lose and Bill collects. | licences available see
| http://www.sohara.org/
|
|
0
|
|
|
|
Reply
|
Steve
|
6/5/2007 3:21:55 PM
|
|
kinaxx@gmail.com writes:
> main()
> {
> int a=5;
> p(a,a); // a == ??
> }
>
>
> What value of a?
That's the problem! We cannot know what the value of a will be,
because it's not obvious. It depends on the order in which the
results are copied back to a. Notably, it doesn't depend on the order
of the x=sum;y=prod; assignments in the procedure, since result
copying is done only when the procedure _returns_. If the first
result is copied back first, then a will get the product. If the
first result is copied last, then a will get the sum.
That's exactly the value-result problem.
Note that most programming languages don't define the order in which
the arguments are evaluated, or results returned, including C, Scheme,
etc... and leave it up to the implementation to choose the order (it
may not even be always the same since this freedom may allow the
compiler to optimize even more some function calls).
--
__Pascal Bourguignon__ http://www.informatimago.com/
NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
|
|
0
|
|
|
|
Reply
|
Pascal
|
6/5/2007 5:01:32 PM
|
|
kinaxx@gmail.com wrote:
>
> 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.
Wrong. The final values are discarded.
--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com
|
|
0
|
|
|
|
Reply
|
CBFalconer
|
6/5/2007 11:08:43 PM
|
|
CBFalconer <cbfalconer@yahoo.com> writes:
> kinaxx@gmail.com wrote:
>>
>> 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.
>
> Wrong. The final values are discarded.
What, then, is the difference between this and pass by value?
--
Ben.
|
|
0
|
|
|
|
Reply
|
Ben
|
6/6/2007 4:05:21 AM
|
|
Ben Bacarisse wrote:
> CBFalconer <cbfalconer@yahoo.com> writes:
>> kinaxx@gmail.com wrote:
>>>
>>> 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.
>>
>> Wrong. The final values are discarded.
>
> What, then, is the difference between this and pass by value?
Nothing. That is "pass by value". How do you return a result when
the original value was the result of an expression, for example?
--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com
|
|
0
|
|
|
|
Reply
|
CBFalconer
|
6/6/2007 7:13:55 AM
|
|
CBFalconer wrote:
> Ben Bacarisse wrote:
>> CBFalconer <cbfalconer@yahoo.com> writes:
>>> kinaxx@gmail.com wrote:
>>>>
>>>> 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.
>>>
>>> Wrong. The final values are discarded.
>>
>> What, then, is the difference between this and pass by value?
>
> Nothing. That is "pass by value". How do you return a result when
> the original value was the result of an expression, for example?
I would expect the language to disallow the call in that case.
As an alternative, it could write the expression value into
a temp and use that. There are several options and, I suspect,
a [1] language for each of them.
[1] For arbitrarily large values of 1.
--
The shortcuts are all full of people using them.
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
|
|
0
|
|
|
|
Reply
|
Chris
|
6/6/2007 8:58:13 AM
|
|
CBFalconer <cbfalconer@yahoo.com> writes:
> Ben Bacarisse wrote:
>> CBFalconer <cbfalconer@yahoo.com> writes:
>>> kinaxx@gmail.com wrote:
>>>>
>>>> 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.
>>>
>>> Wrong. The final values are discarded.
>>
>> What, then, is the difference between this and pass by value?
>
> Nothing. That is "pass by value".
There is a parameter passing mechanism, conventionally called pass by
value-result, that is different from passing by value, and it behaves
as the OP described it. Are you saying that what the OP is being
taught is wrong -- that there is no such mechanism?
> How do you return a result when
> the original value was the result of an expression, for example?
The same question applies to call by reference. The usual solution is
that the compiler only allows expressions that denote lvalues to be
used in such situations.
> --
> <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
> <http://www.securityfocus.com/columnists/423>
> <http://www.aaxnet.com/editor/edit043.html>
> <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
> cbfalconer at maineline dot net
>
>
> --
> Posted via a free Usenet account from http://www.teranews.com
>
Your sig (as my news feed sees it) is either 9 lines long, or the
links you keep posting are not in your sig. Would you consider
trimming to changing it/them?
--
Ben.
|
|
0
|
|
|
|
Reply
|
Ben
|
6/6/2007 2:07:41 PM
|
|
Ben Bacarisse wrote:
> CBFalconer <cbfalconer@yahoo.com> writes:
>
.... snip ...
>
> There is a parameter passing mechanism, conventionally called pass by
> value-result, that is different from passing by value, and it behaves
> as the OP described it. Are you saying that what the OP is being
> taught is wrong -- that there is no such mechanism?
>
>> How do you return a result when the original value was the result
>> of an expression, for example?
>
> The same question applies to call by reference. The usual solution
> is that the compiler only allows expressions that denote lvalues to
> be used in such situations.
Then use pass-by-reference. The so-called "value-result" gains
nothing at all. It does expend extra effort, in that the caller
has to remember and address and do an extra final transfer.
If you really want to enhance complexity, consider pass-by-name.
--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com
|
|
0
|
|
|
|
Reply
|
CBFalconer
|
6/6/2007 3:45:08 PM
|
|
"CBFalconer" <cbfalconer@yahoo.com> wrote in message
news:4666D684.D77DF5B4@yahoo.com...
> Ben Bacarisse wrote:
>> CBFalconer <cbfalconer@yahoo.com> writes:
>>
> ... snip ...
>>
>> There is a parameter passing mechanism, conventionally called pass by
>> value-result, that is different from passing by value, and it behaves
>> as the OP described it. Are you saying that what the OP is being
>> taught is wrong -- that there is no such mechanism?
>>
>>> How do you return a result when the original value was the result
>>> of an expression, for example?
>>
>> The same question applies to call by reference. The usual solution
>> is that the compiler only allows expressions that denote lvalues to
>> be used in such situations.
>
> Then use pass-by-reference. The so-called "value-result" gains
> nothing at all. It does expend extra effort, in that the caller
> has to remember and address and do an extra final transfer.
It does allow certain issues concerning aliasing of parameters to be
resolved (though anyone doing this deserves the world of hurt they might
get). Consider:
procedure P(X : in T;
Y : in out T) is
C : constant T := {whatever};
begin
Y := Y + C;
Y := Y + X;
end P;
...
P(A, A);
Passing by reference could give
A = (A'+C)+(A'+C), where A' is the original value of A
whereas passing by value-result gives
A = (A'+C)+A'
which would be more in keeping with the original behaviour of the
subprogram.
Whether using pass-by-reference is simpler or not does not eliminate the
existence of this other mechanism. A reasonable language would not burden
the user with the details of the passing mechanism. The choice of mechanism
would be, in a large part, driven by the rules of the language being used.
--
Stuart
|
|
0
|
|
|
|
Reply
|
Stuart
|
6/6/2007 5:00:15 PM
|
|
CBFalconer wrote:
> Ben Bacarisse wrote:
>> CBFalconer <cbfalconer@yahoo.com> writes:
>>
> ... snip ...
>>
>> There is a parameter passing mechanism, conventionally called pass by
>> value-result, that is different from passing by value, and it behaves
>> as the OP described it. Are you saying that what the OP is being
>> taught is wrong -- that there is no such mechanism?
>>
>>> How do you return a result when the original value was the result
>>> of an expression, for example?
>>
>> The same question applies to call by reference. The usual solution
>> is that the compiler only allows expressions that denote lvalues to
>> be used in such situations.
>
> Then use pass-by-reference. The so-called "value-result" gains
> nothing at all.
False:
(a) if the argument is accessed multiple times in the procedure
body, it avoids dereferencing each time;
(b) if the argument size is small -- eg comparable with a pointer --
it's as cheap to pass the value in as it is to pass the
reference
> If you really want to enhance complexity, consider pass-by-name.
I don't know any recent languages that have pass-by-name; that
piece of functionality is more likely to be provided by cheap
closures nowadays. (Insert pointed gestures towards any language
that doesn't have them.) (And indeed "cheap closures" is one way
pbn was implemented.)
--
"We did not have time to find out everything /A Clash of Cymbals/
we wanted to know." - James Blish
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
|
|
0
|
|
|
|
Reply
|
Chris
|
6/7/2007 8:29:48 AM
|
|
Its all a question of memory. Think of 2 terms here: stack and heap.
When you pass a parameter by value, you are working with a local copy of the
value represented by the parameter, i.e, you are on the stack.
When a parameter is passed by reference, you are working on the actual value
in your computer's RAM.
I see you're learning C.
<kinaxx@gmail.com> wrote in message
news:1181055542.834797.82520@a26g2000pre.googlegroups.com...
> 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.
>
|
|
0
|
|
|
|
Reply
|
Ace
|
6/13/2007 12:39:13 AM
|
|
Ace wrote:
> Its all a question of memory.
It's a question of language semantics.
> Think of 2 terms here: stack and heap.
Or don't.
> When you pass a parameter by value, you are working with a local copy of the
> value represented by the parameter,
Yes.
> i.e, you are on the stack.
Not necessarily: it depends on the language. (The copy might be heap-allocated,
or in a register, or in static store that the compiler can prove won't be
shared.)
> When a parameter is passed by reference, you are working on the actual value
> in your computer's RAM.
Not necessarily: it depends on the language. (Reference's need not be
machine pointers, although it is the usual way. And, possibly nitpicking,
the value might be in /ROM/ -- call-by-reference doesn't /require/ that
the referenced object be updatable.)
--
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
|
|
0
|
|
|
|
Reply
|
Chris
|
6/13/2007 9:09:49 AM
|
|
|
12 Replies
205 Views
(page loaded in 0.153 seconds)
Similiar Articles: estimation parameter using fminsearch - comp.soft-sys.matlab ...... for your problem. Would you mean 'fsolve' in maple?..I have no idea, could you please explain a ... hence the result is not > optimal. You can ... the starting value ... problem with neumann boundary conditions for heat equation - comp ...How could you explain that? at the edge: dT/dx = hconv ... m^2] However this gives errors in the result. Do you ... shooting method, boundary value problem - comp.soft ... problem with mixed c and fortran code - comp.lang.fortran ...... think it of negative value to just say that "you could get ... integer(C_INT), value :: result_len ... from Win32 to Linux - passing in * as an arg to the ... problem with ... conventional wisdom how to upsample very large arrays, accurately ...Could you explain in more detail what you mean by "4X ... numbers and checking the sign of the result ... be sure that we have found them all, could we? If a (low-pass ... How to write a code for iteration process - comp.soft-sys.matlab ...... am still a bit of confuse, could you explain ... the function that you pass it to behave. Basically, for this problem you're ... Method Matlab Code: initial value problem ... pthread_cond_wait problem - comp.unix.programmer... silly: The main problem is that you are ... pthread functions I could look into? When you explain what ... does not mean that what you're waiting for has come to pass ... Fill a single contour - comp.soft-sys.matlabCan you explain this more: "PATCH ... filling; if so, then you will have a bit of a problem. ... I changed the value for green to "less than 50" so you could see some ... Pointers to global and stack variables - comp.compilers... from a procedure except by passing some variables by reference or value-result ... implement them as the compiler's problem. You ... comp.programming Most books explain that ... catching std::exception by value - comp.lang.c++.moderated ...If you wanted, you could override the what ... Also, there is the problem of an out-of-memory exception when you are about to ... push a stack on a stack without passing by value ... Fail to import due to date format - comp.databases.ms-access ...The problem is Access won't accept my data due ... You could import the date value into a field of type Text. ... random.choice(month_dict.keys ... pass through ... the firing squad problem - comp.programming... repeated up the line, each passing back received N-1. When the result ... that he can count and all you did was find a value ... give you the idea for how you solve the problem ... How to get integer value for register contents ? - comp.lang ...Could some Verilog guru please help ? I am passing a 32 bit ... The usual problem with integer is that it is 32 bits when you ... you will get a meaningful result ... Difference between passing a number and a variable to a subroutine ...... passing mechanisms, so it should result in code ... out (or pass by value). The exception to the no copy-in/copy-out involves discontigous array sections. std::map< MyString, MyString > comparison operator? - comp.lang ...It sounds like you have a problem with your ... and MyString implements value semantics correctly, you ... your objects, that s why you need them, That could explain ... How to combine two awk commands - comp.lang.awk... count; i++) print lines[i]}" Please could you ... I had never realized the problem of messing the order until you point out. ... sous-expression invalide Would you explain ... Could you explain pass by value-result problem?? - comp ...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 ... C++ Programming :: Could you explain the Pass by value-result problem?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 ... 7/14/2012 6:47:55 PM
|