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

Richard Tobin

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
 
B

Ben Bacarisse

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

Chris Dollin

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

Chris Dollin

(e-mail address removed) wrote:

(Stuff also appearing, it transpires, in comp.programming.)

PS It's not polite to multipost. Don't do it.
 
R

Richard Heathfield

Roland Pibinger said:
C also has 'pass by address' (a.k.a. pointers).

In C, pointer expressions are passed by value, just like any other
argument expressions.
 
C

Chris Dollin

Roland said:
C also has 'pass by address' (a.k.a. pointers).

My evil twin will be ready to point out that C does
/not/ have "pass by address". Using pointers is just
using pointers; there's no special parameter-passing
machinery involved. Contrast eg VAR parameters in Pascal.
 
R

Richard Tobin

C has pass by value, and that's your lot.
[/QUOTE]
C also has 'pass by address' (a.k.a. pointers).

No, but you can pass pointers by value and dereference them in the
called function to achieve the effect of call-by-reference.

-- Richard
 
D

Dik T. Winter

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

The only programming language I know that allows for pass by value-result
is Fortran. But there the call p(a,a) is disallowed. (If two arguments
are aliases of each other, assignment to them in a subroutine is not
allowed.)
 
C

Chris Dollin

Chris said:
My evil twin will be ready to point out that C does
/not/ have "pass by address".

I though we'd agreed not to both post to the same thread? But you're
right.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top