Velocity Reviews > Pass-By-Value-Result Problem

# Pass-By-Value-Result Problem

kinaxx@gmail.com
Guest
Posts: n/a

 06-05-2007
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?

Richard Tobin
Guest
Posts: n/a

 06-05-2007
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> wrote:

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

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

Ben Bacarisse
Guest
Posts: n/a

 06-05-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

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

--
Ben.

Chris Dollin
Guest
Posts: n/a

 06-05-2007
(E-Mail Removed) wrote:

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

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.

--
There' no hortage of vowel on Uenet.

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Chris Dollin
Guest
Posts: n/a

 06-05-2007
(E-Mail Removed) wrote:

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

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

--
A rock is not a fact. A rock is a rock.

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Roland Pibinger
Guest
Posts: n/a

 06-05-2007
On Tue, 05 Jun 2007 15:54:44 +0100, Chris Dollin wrote:
>C has pass by value, and that's your lot.

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

--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch

Richard Heathfield
Guest
Posts: n/a

 06-05-2007
Roland Pibinger said:

> On Tue, 05 Jun 2007 15:54:44 +0100, Chris Dollin wrote:
>>C has pass by value, and that's your lot.

>
> C also has 'pass by address' (a.k.a. pointers).

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

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

Chris Dollin
Guest
Posts: n/a

 06-05-2007
Roland Pibinger wrote:

> On Tue, 05 Jun 2007 15:54:44 +0100, Chris Dollin wrote:
>>C has pass by value, and that's your lot.

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

--
Far-Fetched Hedgehog
The shortcuts are all full of people using them.

Richard Tobin
Guest
Posts: n/a

 06-05-2007
In article <(E-Mail Removed)>,
Roland Pibinger <(E-Mail Removed)> wrote:

>>C has pass by value, and that's your lot.

>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

Dik T. Winter
Guest
Posts: n/a

 06-06-2007
In article <f43t1e\$19jb\$(E-Mail Removed)> (E-Mail Removed) (Richard Tobin) writes:
> In article <(E-Mail Removed). com>,
> <(E-Mail Removed)> wrote:
> >3) pass by value-result <- i have question this subject .

....
> >void p(int x,int y)

....
> > x=sum; //sum will be returned via x
> > y=prod; // prod will be returned via y

....
> > p(a,a); // 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
> 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.)
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/