call by value, reference, name...

This is a discussion on call by value, reference, name... within the C Programming forums, part of the General Programming Boards category; hey everyone.. i am new to C and need your help with telling me the differences between call by value, ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    7

    Question call by value, reference, name...

    hey everyone.. i am new to C and need your help with telling me the differences between call by value, reference name and copy-restore...

    right now i am trying to figure out what the outputs for this code are gonna be based on those cases

    Code:
    #include <stdio.h>
    
          int a;
    
          void f(int x, int y) {
            x += a;
            a += 2*y;
          }
    
          int main() {
            a = 3;
            f(a, 4);
            printf("%d\n", a);
            return 0;
          }
    i figure out that by reference the output of this code will be 14... but what about by value? name? and copy-restore??

    any help is appreciated .. thanks

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    7
    is call by value answer 11? reference 14?

    i am not sure about name and the other one still... please i am sure people reading this thread can help...

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Your example uses call by value. There is no call by reference in that code.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think you mean "Pass by reference". Which means passing pointers to variables instead of the values.

    Code:
    #include <stdio.h>
    
          int a;
    
          void f(int* x, int y) {
            *x += a;
            a += 2*y;
          }
    
          int main() {
            a = 3;
            f( &a, 4);
            printf("&#37;d\n", a);
            return 0;
          }
    I've never heard of "copy-restore", and I'm not sure what you mean by "reference name"?
    Last edited by cpjust; 05-05-2008 at 09:52 PM.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    7
    Quote Originally Posted by robwhit View Post
    Your example uses call by value. There is no call by reference in that code.
    ok... i know that the symentics of the code are by value.... soo the answer is 11...
    if this code would be rewritten to be by reference then the output would be 14....

    how should this code be rewritten to be by name and by copy-restore??

    i already understand how it works by value and reference.... i am more concerned about the other two cases....


    thanks

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    7
    here i broke it down:

    for by value:

    Code:
    f(int x, int &y){
    // x will be 3 as passed argument
    x += a;
    // now a is added to x so x will be 6
    // but now nothing is done with x anymore
    a += 2*y;
    // a is still 3 so the result is 11
    }

    for by reference:

    Code:
     f(int x, int &y){
    // x will be 3 as passed argument
    x += a;
    // now a is added to x so x will be 6
    // but because & is used x is the same as a
    // meaning if you change x it will change a
    a += 2*y;
    // a is now 6 so the result is 14
    }



    i need help with by name and copy - restore

    thank you

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    All parameters in C are passed by copying. The "pass by reference" only exists because a copy of a pointer is still a pointer, which has a valid address.

    Call by name, call by copy restore?

    Are you talking about how the stack calls can handle parameter name changing, when you call or return from a called function?

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    7
    Quote Originally Posted by Adak View Post
    All parameters in C are passed by copying. The "pass by reference" only exists because a copy of a pointer is still a pointer, which has a valid address.

    Call by name, call by copy restore?

    Are you talking about how the stack calls can handle parameter name changing, when you call or return from a called function?
    hi

    i am using these considerations:

    Copy-restore: A hybrid between call-by-value and call-by-reference.
    Call-by-name: The effect is as though the procedure was a macro.
    Used in Algol. Can be used for inline expansion of functions.


    for example:
    Code:
    int a;
    void unsafe(int x) {
    x= 2; a= 0;
    }
    int main() {
    a= 1; unsafe(a); printf("&#37;d\n", a);
    }
    Under call-by-reference, it prints 0. Under copy-restore, it prints 2.


    i still don't understand why 2 there and what the output of function i posted before should be =(
    Last edited by magic101; 05-05-2008 at 08:34 PM.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by magic101 View Post
    Copy-restore: A hybrid between call-by-value and call-by-reference.
    Call-by-name: The effect is as though the procedure was a macro.
    Used in Algol. Can be used for inline expansion of functions.
    I don't think anyone here has any clue what you're talking about.
    In C there are only 2 possible ways of passing data to a function: By Value, or By Reference (i.e. pointer).
    I never heard of Algol until today. Does anyone still use it?

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by cpjust View Post
    I think you mean "Pass by reference". Which means passing pointers to variables instead of the values.
    Right, I did.

    I've never heard of those other things either.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    Code:
    f(int x, int &y){
    This is not valid C.
    The & on variables are used for references in C++, but can't be used in C.
    This is also why I use the term pass by pointer instead of pass by reference for C.

    Code:
    void foo(int* x);
    Here we can pass by pointer.


    And FYI, there are only 3 ways to pass arguments in C and C++:

    - By value
    - By pointer
    - By reference (C++ only)

    So I don't know where you're getting the rest or how you're mixing or confusing code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    An example of call by value would be:
    Code:
    void foo(int v)
    {
        /* v's value is a copy of x's value */
        /* ... */
    }
    
    /* ... */
    int x = 0;
    foo(x);
    An example of simulated call by reference in C would be:
    Code:
    void bar(int *p)
    {
        /* p refers to x, but pointer notation is used */
        /* ... */
    }
    
    /* ... */
    int x = 0;
    bar(&x);
    An example of simulated call by copy-restore in C would be:
    Code:
    void baz(int *p)
    {
        /* copy */
        int *local = malloc(sizeof(int));
        if (local == NULL)
        {
            return;
        }
        *local = *p;
    
        /* ... */
    
        /* restore */
        *p = *local;
        free(local);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 03:48 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  4. call by reference
    By Jackie in forum C Programming
    Replies: 3
    Last Post: 10-29-2001, 05:46 AM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21