Pass-By-Value-Result Problem

Discussion in 'C Programming' started by kinaxx@gmail.com, Jun 5, 2007.

  1. Guest

    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.
    , Jun 5, 2007
    #1
    1. Advertising

  2. In article <>,
    <> 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
    your question. Quite likely the answer will be "it's implementation
    defined".

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Jun 5, 2007
    #2
    1. Advertising

  3. 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.
    Ben Bacarisse, Jun 5, 2007
    #3
  4. Chris Dollin Guest

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

    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.

    --
    There' no hortage of vowel on Uenet.

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
    Chris Dollin, Jun 5, 2007
    #4
  5. Chris Dollin Guest

    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
    Chris Dollin, Jun 5, 2007
    #5
  6. 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
    Roland Pibinger, Jun 5, 2007
    #6
  7. 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.
    Richard Heathfield, Jun 5, 2007
    #7
  8. Chris Dollin Guest

    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.
    Chris Dollin, Jun 5, 2007
    #8
  9. In article <>,
    Roland Pibinger <> 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.
    Richard Tobin, Jun 5, 2007
    #9
  10. In article <f43t1e$19jb$> (Richard Tobin) writes:
    > In article <>,
    > <> 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
    > 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.)
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Jun 6, 2007
    #10
  11. Chris Dollin Guest

    Chris Dollin wrote:

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


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

    --
    "No-one here is exactly what he appears." G'kar, /Babylon 5/

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
    Chris Dollin, Jun 6, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    20
    Views:
    7,930
    Roedy Green
    Sep 9, 2005
  2. Replies:
    4
    Views:
    7,229
    James Kanze
    Jun 6, 2007
  3. venkatagmail
    Replies:
    11
    Views:
    681
    James Kanze
    Oct 3, 2007
  4. Vols
    Replies:
    3
    Views:
    375
    Ian Collins
    Apr 28, 2008
  5. Michael Tan
    Replies:
    32
    Views:
    969
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page