Thread: function calling

  1. #1
    help me
    Guest

    function calling

    can some 1 explain "call by refernce" and "call by value"?plz!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Here, a copy of the original variable is passed. Any changes made to it inside the function will not alter the variable passed to it.
    Code:
    void Function(int MyVariable)
    {
    
    }
    Here, a reference to the variable is passed. Any changes made to it inside the function will also change the value of the variable you passed to it.
    Code:
    void Function(int& MyReference)
    {
    
    }
    Here, a pointer to the variable is passed. It is basically the same as a reference, but pointers can change what they point at at runtime plus they have a kind of tri-state, a NULL pointer, which doesn't point at anything valid. A reference always points at something.
    Code:
    void Function(int* MyPointer)
    {
    
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    And here's an example:

    Code:
    #include <stdio.h>
    
    void CallByValue(int val)
    {
       val *= 2;
    }
    
    void CallByRef(int *pVal)
    {
       *pVal *= 2;
    }
    
    int main()
    {
       int value = 5;
    
       CallByValue(value);
       printf("Call By Value, value = %d\n\n", value);
    
       CallByRef(&value);
       printf("Call By Reference, value = %d\n\n", value);
    
       return 0;
    }
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>void Function(int& MyReference)
    You forgot to mention this is for C++ only
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Hammer
    >>void Function(int& MyReference)
    You forgot to mention this is for C++ only
    This is the C-board? Dang, missed that. LOL!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM