Could you explain the Pass by value-result problem?

K

kinaxx

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

Robert Bauck Hamar

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.
pass by value-result
passing mechanism
1.The values of the arguments are copied into the formal 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 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);
}
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?

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

Guest

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


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


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


int main


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);
}

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

Robert Bauck Hamar

Erik said:
Hello,
now I'm learning progamming language in university. [...]
pass by value-result
passing mechanism
1.The values of the arguments are copied into the formal parameters.
2.The final values of the parameters are copied back out to the
arguments. [...]
ex)
Assume: pass by value-result
void square(int x,int y)
{
x*=x;
y*=y;
}


main()

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);
}

Why the temporaries?

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

James Kanze

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

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.
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);
}

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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top