Thread: Parameters call by...

  1. #1
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189

    Parameters call by...

    Hey guys,

    Please help me about understanding of call by parameters.

    For this C syntax:

    Code:
    void main(){
    
    int value =2, list[5]={1,3,5,7,9};
    swap(value, list[0]);
    swap(list[0], list[1]);
    swap(value, list[value]);
    
    }
    
    void swap(int a, int b){
    
    int temp;
    temp=a;
    a=b;
    b=temp;
    
    }
    What are the values of variables value and list after each of the three calls to swap with the following parameter-passing methods:

    a. Passed by value
    b. Passed by reference
    c. Passed by name
    d. Passed by value-result

    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    161
    In the code you provided, the values are all left entirely unchanged because they were passed by value.

    edit: swap should take pointers:
    Code:
    void swap(int *a, int *b)
    {
      int temp = *a;
      *a = *b;
      *b = temp;
    }
    
    /* and should be called with addresses: */
    int main()
    {
      int i = 5;
      int x = 10;
      swap(&i, &x);
      return 0;
    }
    Last edited by thefroggy; 01-06-2004 at 08:36 AM.

  3. #3
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    void main()
    it's
    Code:
    int main (void)
     { 
       return 0; 
     }
    the code is useless because you swap local variables, so swap using pointers:
    Code:
    void swap(int *a, int *b){
    
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
    
    }
    swap ( &value, &list[0]);

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main(){
    No:
    Code:
    int main ( void ) {
    >void swap(int a, int b){
    There's no prototype, so this must be defined before main.

    >swap(value, list[0]);
    value and list[0] are passed to swap by value, no changes are made to either.

    >swap(list[0], list[1]);
    list[0] and list[1] are passed by value, no changes are made outside of swap.

    >swap(value, list[value]);
    Noticing a trend? Passing by value doesn't effect the originals.

    In C there is only one way to pass arguments to a function, by value. If you want to modify the original variable inside of the function body, you can pass the address of that variable (by value!) and dereference the resulting pointer to access the original memory location.
    My best code is written with the delete key.

  5. #5
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    The code is just for sample. It is not really in C programming. I want to know is if I use passed by value, what is the values of variables, value and list. If I use passed by reference, which is similar to pointer, what is the values???

    I have a confusion with passed by name and passed by references.

    And also I don't get the point of the differences between passed by value and passed by value-result.


  6. #6
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    when you pass by value , copies of the original variables are made and they are treated like normal local variables. when you pass by reference , you pass a pointer to the variable and by dereferencing that pointer you can modify the variable itself.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    161
    > If I use passed by reference, which is similar to pointer
    Be careful with your terminology here. The only way to pass by reference in C is to pass a pointer.

    > passed by value-result
    I've never heard this before.

  8. #8
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    Originally posted by thefroggy
    The only way to pass by reference in C is to pass a pointer.


    Please read my post completely before criticize. Anyway thanks

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It is not really in C programming.
    You picked a restricted language to ask your question in then.

    Pass-by-value: A copy of the variable is made and any work done inside of the function modifies the copy. The variable passed as an argument remains unchanged.

    Pass-by-reference: The variable itself is passed (rather, an alias with the same memory location) and no copy is made. Any changes inside of the function modify the variable passed directly and the changes remain after the function returns. C does not support this, but C++ does.

    Pass-by-name: Similar to pass-by-reference, the symbolic name of the variable is passed, resulting effectively in a textual substitution, much like #define. A problem is that if any variable names in the function clash with the caller's argument names, there will be errors. C does not support this.

    Pass-by-value-result: Similar to pass-by-reference except copies are made. When passing to the function a copy is made of the variable and used in the function, when the function returns, the copy is copied back into the original variable passed. Note that this is not the same as pass-by-reference because the copy is independent of the original. C does not support this.
    My best code is written with the delete key.

  10. #10
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    What I have tried and found out is that:

    for a). Passed by value the answer is
    Code:
    value=2, list [5] ={1,3,5,7,9}
    for b). Passed by reference :
    Code:
     value=2, list [5] = {3,2,5,7,9}
    for c and d I still don't know. Please check my answer for a and b is correct or not?

  11. #11
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    great post , Prelude.
    so, C supports only passing by value. It simulates passing by reference by using pointers.
    here's a thread on this http://cboard.cprogramming.com/showt...s+by+reference

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Please check my answer for a and b is correct or not?
    Assuming that C allowed all of your passing options:
    Code:
    int value=2, list[5]={1,3,5,7,9};
    swap(value, list[0]); /*1*/
    swap(list[0], list[1]); /*2*/
    swap(value, list[value]);/*3*/
    Pass-by-value:
    1) value=2, list={1,3,5,7,9};
    2) value=2, list={1,3,5,7,9};
    3) value=2, list={1,3,5,7,9};

    Pass-by-reference:
    1) value=1, list={2,3,5,7,9};
    2) value=1, list={3,2,5,7,9};
    3) value=2, list={3,1,5,7,9};

    Pass-by-name:
    1) value=1, list={2,3,5,7,9};
    2) value=1, list={3,2,5,7,9};
    3) value=2, list={3,1,5,7,9};

    Pass-by-value-result:
    1) value=1, list={2,3,5,7,9};
    2) value=1, list={3,2,5,7,9};
    3) value=2, list={3,1,5,7,9};

    Notice how all but pass-by-value have the same end result, the only difference is in how they get to that end result and the potential problems they might have in use. For further information, try this link.
    My best code is written with the delete key.

  13. #13
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    So we can conclude that

    by name, by reference and by value-result are difference only in theory and in practical it give the same result.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >by name, by reference and by value-result are difference only in theory and in practical it give the same result.
    More accurately, they all give the same result, but do it in a different way. In practice, their use really depends on how the language syntax supports them.
    My best code is written with the delete key.

  15. #15
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    After I have read from the link you gave me and I think like that...


    for pass-by-name

    for no. 3)

    should be
    Code:
    value=2, list={3,2,1,7,9}
    What I understand is that for pass-by-name, it gave everything. including list[value]. I mean not only list[], but also it counter value. So in swap when the value is equal to 2, the list is no longer list[1], should be list[2], isn't it?


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Parameters Corrupt
    By cecomp64 in forum C Programming
    Replies: 6
    Last Post: 07-11-2008, 11:33 AM
  2. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  3. Call constructor within constructor. How to?
    By xErath in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 05:30 PM
  4. Assembly example
    By Lynux-Penguin in forum C Programming
    Replies: 6
    Last Post: 04-24-2002, 07:45 PM
  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