parameter passing?

This is a discussion on parameter passing? within the C Programming forums, part of the General Programming Boards category; Hi all, There are four parameter passing mechanisms: call by value call by value-result call by reference call by name ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    8

    parameter passing?

    Hi all,

    There are four parameter passing mechanisms:

    call by value
    call by value-result
    call by reference
    call by name

    i'm quite confused about them. could someone give me an explanation? examples would be more appreciated.

    Thanks in advance.

  2. #2
    Registered User The Junglist's Avatar
    Join Date
    Nov 2002
    Posts
    42
    If someone had taught you that then they should be doing some revision. There are two ways two pass data to functions, nl. by value and by reference.

    Whether the function returns a value from the function is irrelevant.

    Pass by value :
    This is when you pass a variable to a function like this :
    Code:
    #include <stdio.h>
    
    void function( int param )
    {
           param = 10;
    }
    
    int main()
    {
         int x = 5;  
         function ( x );
         printf( "%d", x);
         return 0;
    }
    A copy of 'x' is made and used in the function, so that you cannot change the original value of 'x'.

    Pass by reference:
    This is when you pass the address of a variable to a function like this :
    Code:
    #include <stdio.h>
    
    void function( int *param )
    {
           *param = 10;
    }
    
    int main()
    {
         int x = 5;  
         function ( &x );       //The '&' signifies an address
         printf( "%d", x);
         return 0;
    }
    This time you pass the address of 'x' to the function so that you can change the original value of 'x'. By using the '*', on param in the function, we access the data that param points to, in this case 'x'.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    afaik, it's all passing by reference. when you get down to the assembly language underlying the program, you have something like this:

    Code:
    //c program
    int func() {
      return 3;
    }
    assuming it hasn't been optimized out by your compiler...
    Code:
    ;assembly program
    enter 0,0
      mov eax,3
    leave
    ret
    eax is a 4 byte register. any value returned must be at most 4 bytes. (if you're compiling for a non-intel architechture, this may be different... but not too much different). even void functions return something because the register eax always exists. it's just that the c language won't let you work with void functions that way.

    when you return a char, an int, a long, etc..., you're returning a value through eax. any value bigger than that is returned by reference (ie: by pointer).

    i hope i cleared some things up... but i've never heard of passing by name or value-result before.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    8

    Thumbs up

    Originally posted by Salem
    http://wombat.doc.ic.ac.uk/foldoc/fo...i?call-by-name
    You should be able to figure out the rest
    The website you provided is very helpful. But I'm still quite confused about call by name. I think an example might illustrate this question better.

    Here is a procedure definition:

    def q(a,b,c)
    t = a
    a = b
    b = a+c
    c = t

    and the following program:

    i = 1; j=2;
    q(i,j,i);
    print i, j;

    what is printed when parameters are passed using
    i. call by value
    ii. call by value-result.
    iii. call by reference
    iv. call by name?

    i. The answer is 1,2. Because i and j dont ever change.

    ii. The answer is also 1,2. But it is just a coincidence.

    iii. The answer is 1, 3.

    are they correct so far?

    iv. no idea yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parameter passing and pointers
    By pokiepo in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 01:13 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM

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