COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Could you explain pass by value-result problem??

• Email
• Follow

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

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?

 0
Reply kinaxx (3) 6/5/2007 2:59:02 PM

See related articles to this posting

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

--
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

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

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>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

 0

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

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>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

 0

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

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>
>                         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

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>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

 0

"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

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

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

I see you're learning C.
<kinaxx@gmail.com> wrote in message
> 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
>
>
>
>
> 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?
>
>

 0

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

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

12 Replies
364 Views

Similar Articles

12/12/2013 10:37:42 AM
page loaded in 51515 ms. (0)

Similar Artilces:

Hi all, I am reading the book "Writing Testbench". And write the code below: It seems the code is blocked in the task "@ (posedge clk)", and the task never return the "valid", why? I guess task cannot pass the continuous clk value? //--------test.v------------- module test; reg clk; integer i; reg [7:0] input1,input2; reg [7:0] valid1; test_lib test_lib(); always begin #50 clk <= 1'b0; #50 clk <= 1'b1; end initial begin for (i=1;i<=8;i=i+1) begin input1 = i; input2 = i; begin test_lib.compa...

Help Value List
I want the value list that I have created to show the user. "Active" or "Complete" but when selected I want Filemaker to see "1" or "0". Is this possible with FMP7 ? -S squeed2000@yahoo.com wrote: >I want the value list that I have created to show the user. "Active" >or "Complete" but when selected I want Filemaker to see "1" or "0". Is >this possible with FMP7 ? > >-S You're not going to get a field to show one value but actually be another. The value is the value is the value. What y...

Passing values between procedures(without actually passing them)
It's possible to achieve something like this? Procedure1(param1,param2) IS .... begin Procedure2; end Procedure1; Procedure2 IS ..... begin --Know the parameters values of Procedure1 at this point(with dbms_debug, dbms_trace, some V\$, ....) end Procedure2; The point to this is that i have a lot of procedures(like Procedure1) calling a Procedure(Procedure2). So if this could be achieved, i only have to use a simple line like "Procedure2;" to all procedures(and with the advantage of not being dependent of the number of parameteres, so future changes wouldn'...

MatLab database problem~to get integer values instead of floating-point values
Hi All, I met a problem when programming a simple database application at MatLab. As you see the following code, SELECT query would two data (base_dt and sum(rtn) ) from database. one (base_dt column) is date-format. the other ( sum(rtn) column ) is floating-point value. but when running the following code, the second field is returned to be integer instead of floating-point value. the first field is what i expected : date format In my thought, problem is related with the functions in query. I failed to solve this problem by "setdbprefs". FYR, database is Oracle and matlab ve...

Problem with: su
Hi, when I run su - sybase -c "PASS=`cat .sybpw`; echo \$PASS" nothing is echoed, but when I run su - sybase -c "cat .sybpw" everything went fine. I need the PASS variable later in the command, e.g. su - sybase -c "PASS=`cat .sybpw`; isql -Usa -P\$PASS" Any hints? Regards, Heinz Heinz M�ller <onkelheinz@mscologne.de> wrote: > Hi, > > when I run > > su - sybase -c "PASS=`cat .sybpw`; echo \$PASS" > > nothing is echoed Both command and parameter substitution are performed within double-quot...

Pass-by-value variations
Hi all, Are the following three functions equivalent, in terms of observable behavior re: the standard, and in terms of typical compiler optimization? // #1 template <typename T> void f (T x) { // change x } // #2 template <typename T> void f (const T& x) { T x2 = x; // change x2 } // #3 template <typename T> void f (T x) { T& x2 = x; // change x2 } #1 is classic "pass-by-value" and of course preferred if T is small, inbuilt or MOJO/ZUTO-enhanced (?). However my immediate problem is that in VC++ 7.1 some types cannot be passed by value -- specifically a...

References Themselves Are Passed by Value
Is it true that Javascript has no clone() method, to pass an object by copy of value instead of reference? If I have an object and want to make an array out of all of its instance variables, I can loop through it and pass its values to a new array, and the class instances will be passed by copy and not by reference? Example 9.3: References Themselves Are Passed by Value // This is another version of the add_to_totals() function. It doesn't // work, through, because instead of changing the array itself, it tries to // change the reference to the array. function add_to_totals2(t...

Passing to/from function problems
Folks, I have a simple script below... I come from a programming background with PHP,C++,bash unix shell scripting so I have a rough understanding when it comes to javascript. I have written a few javascripts of my own but only until now have I realised that this will be the first time that I have wanted to pass data *to* a function, chew it up, and spit it back... In my test script below, I would have expected the script to return def=1 and ghi=2, but it doesn't... Can someone tell me where I've gone wrong? Thanks, all replies, via the newsgroup would be much appreciated, r...

passing values to cw_fslider
Hello. I'm trying to write a program in which changes in the quantity associated with one widget are reflected in the values of other widgets, and I am at a loss to do this. In fact, I'm not sure it even can be done. In the example below, I have slider widgets for mass, volume, and density. If I change the volume slider, the mass is held constant and a new value for the density is calculated. Etc. The new values for volume, mass, and density are stored in the info structure correctly, it seems. However, the position of the other two sliders are not changed. Is there anyway to do t...

problem with value of the pointer...
hi i would like to try to reduce global variable for a program... i get a value from getenv... after this value will never change... i tried this code: void readRtuConfig(char *local_dir_led) { if(!(local_dir_led = getenv("LOCAL_DIR_LED"))) { printf("Incapable de lire la variable: LOCAL_DIR_LED\n"); return 1; } printf("%s\n", local_dir_led); } void main() { char *local_dir_led=NULL; readRtuConfig(local_dir_led); printf("%s\n", local_dir_led); } the program crash... maybe that happen because local_dir_le...

Functors are passed by value
Dear all, Scott Meyers decscribes in item 38 of his book 'Effective STL;' that functors should be designed for pass by value. This is because the STL uses in its interface many pass by value construction for argument. But besides that, is there actual an advantage to it that way? I can only think that it is probably more general to do pass by value. A second question would be that I often require a specific interface callback function (thus not a template functor). Mostly I use boost function, but I see now that pass by const reference would be more effective than by value, i.e.: v...

passing parameter problems
Hi, I'm designing a website in dreamweaver and I'm using JavaScript to slide text across the page. The text is contained in <div> tags. I have designed a function which can move two of the div tags across the screen. I use the timer to manage the movement of the text across the screen. I would like redesign that function so that it can move any amount of div blocks across the screen. To do this, I need to pass a parameter to the function moveParas() The following set up works fine, calling moveParas which moves two div code blocks across the screen: function window_onload(...

problem with variable values
Hi! I'm writing a program in VHDL which should draw a line on the monitor. I have implemented vga_sync module, RAM memory and other necesarry things. Almost everything is OK. So i don't know what is happening with variables values I have a process which is writing to memory pixel coordinates in resolution 80x60 at the Altera UP-2 (Flex 10K) Board. CODE ------------------------------------------------------------------------------------------------------------------------------------ bresenham :process variable punkt1_x : std_logic_vector(6 downto 0) := CONV_STD_LOGIC_VECTOR(20,...

Is there a known result for this problem?
Hi, all, Yesterday I ran into the following problem There are N line segments on a plane intersecting with each other, what is the complexity of finding the maximum non-intersecting subset? Equivalently, what is the complexity of finding the minimum number of line segments to remove in ordering to make no remaining segment intersecting with each other? It appears to be a global optimization problem, but I am not sure if it is NP hard.... It could be easily reduced to the clique problem (line->vertice, non-intersecting->an edge), but reducing the clique problem to this problem seems t...

Newbie: Explain My Problem
Code: #The Guess My Number Game import random num = "" guess = "" counter = 7 num = random.randrange(1, 100) print "I'm thinking of a whole number from 1 to 100." print "You have ", counter, " chances left to guess the number." print guess = int(raw_input("Your guess is: ")) while counter != 0: if guess == num: print "You guessed the number, ", num, " in ", counter-6, " guesses!" elif guess > num: counter = counter - 1 print print "The number is less t...

passing values 86575
I have this home.js file and I'm trying to collect the values on the other at "emplist.aspx" function poponload() { testwindow= window.open ("emplist.aspx", "mywindow", "location=1,status=1,scrollbars=1,width=600,height=600"); testwindow.moveTo(0,0); } document.write ("<form action=http://www.espn.com method=post>") document.write ("<input type=text>") document.write ("<input type=button value=Search onClick='javascript: poponload()'>") document.write ("</form>") but I d...

Pass by reference or value?
Hello friends, I am facing a lot of confusion over a simple issue ofparameter passing. Just let me know what I have figured out is rightor wrong:"There is no pass by reference in Java. Everything is passed by value.In case of primitive types, a copy of the variable which holds itsvalue is made and passed to the called function. In case of objects, acopy of the reference is made and passed to the called function. It isthis reference variable which holds the actual reference to an object.Think of the reference variable as a container which holds a remotecontrol, the remote being the object ...

Problem with passing a string to a function
Hi, I have a number of buttons on a form which run mailmerges. Next to each button is a text box/control that the user enters a date into when the letter was created/merged. When the user presses the button it calls a function called LetterMerge to carry out a mail merge. Here is the on click property for one of the buttons: Private Sub cmdConsultationEWO_Click() Dim strLetterDateSentName As String strLetterDateSentName = Forms![frmCase]![frmIncident]![frmIncidentPerpetrator]![LetterDateSentConsultEWO].ControlSource Call LetterMerge("Consultation-EWO.doc", strLetterDate...

Passing Query Results to Form
Maybe I am going about this totally wrong.... What I have written is a query pulling SSN, Student-ID, LastName, FirstName, MiddleInitital from a table. Sort is so LastName, FirstName, MiddleInitial, SSN, and StudentID are all ascending, and in this order. I want to the person using this query, in a ListBox, to be able to look up a person (student) by last name to see if they are in the table already, and then pass all the above information to the form, of which this listbox is a part of. How do I do this? I can only pass all information if I use a list box based on the table, but as a r...

passing values Newbie question
I have looked through the first 672 messages and haven't found anything similar to this so I'm sorry if this has been posted and I didn't see it. I'm new to js, so PLEASE be patient with me! Thanks in advance! I posted the following message in a Frontpage newsgroup: I need to carry values from one webpage form to another. I can't use ..asp, I have to use html or dhtml. I've tried going through the VBA help files and i can't figure it out. I don't know what how to refer to the option button that was selected. Here's what I'm trying to do, so if the...