Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

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

kin@gmail.com wrote:
> 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)

This is the usual way of passing arguments in C++.

> 2) pass by reference (inother words: call by reference)

This can be used in C++ with reference parameters.

> 3) pass by value-result <- i have question this subject.

C++ does not support value-result, but they can be simulated.

int main

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

simulation in C++:
void square(int &x, int &y)
{
  x *= x;
  y *= y;

}

int main()
{
  int a = 5;
  int b = 10;

  // copy the arguments to temporaries, one per formal parameter.
  int tmp1 = a;
  int tmp2 = b;
  // send these as references, so that result can be retrieved
  square(tmp1, tmp2);
  // copy result back:
  a = tmp1;
  b = tmp2;
  printf("a = %d b = %d\n",a,b);

Try to simulate pass as I showed. Then apply brain. We're not here to do
your homework.

--
rbh

On 2007-06-05 17:48, Robert Bauck Hamar wrote:

Why the temporaries? Just doing

int main()
{
   int a = 5;
   int b = 10;

   square(a, b);

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

}

will work just as well.

--
Erik Wikstrm

Because that is how pass-by-value-result parameter passing might be
simulated in C++. As the OP asked about pass-by-value-result, and C++
doesn't support it, this is about the only topical answer in this NG.

>Just doing

> int main()
> {
>   int a = 5;
>   int b = 10;

>   square(a, b);

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

> will work just as well.

In this particular example, yes. But not in the second example given by
the OP. My post tries to explain how pass-by-value-result works in code,
finding out how it differs from pass-by-reference is exactly what the
OP's home work assignment reads.

--
rbh

On Jun 5, 4:50 pm, kin@gmail.com wrote:

Note that copy-in, copy-out is not present in C++, which makes
questions about it more or less off topic here.  You might want
to ask in comp.compilers, where the experts for this sort of
thing hang out.

Whatever the language in question specifies it to be.  In C++,
it is 5, because C++ uses pass by value.  (Unless the parameters
are references, in which case it is pass by reference.)  In
Fortran (which is designed so that both pass by reference and
copy-in, copy-out are legal), it would be undefined behavior
(although I don't think the Fortran standard uses exactly that
expression).  A language requiring copy-in, copy-out could
specify the order of copying, however, and then it would be
defined.

--
James Kanze (GABI Software)             email:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc