what is pass by ref function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arunmib
    New Member
    • May 2007
    • 104

    what is pass by ref function?

    hi all,
    I just got this freaky kind of doubt....I have the following piece of code,

    Code:
    int main()
    {
         int Val= 10, *ptr;
         ptr = &Val;
    
        TestFn(&Val);
       
        TestPtr(ptr);
    
        return 0;
    }
    
    void TestFn(int *i)
    {
        ....does some operations
    }
    
    void TestPtr(int *j)
    {
       ....does some other operations
    }
    Technically speaking for the first function (TestFn) I am passing the address of the variable Val. So I can call this function as pass by reference function.

    But when it comes to the second function (TestPtr), what i am doing is the following, the address of the Val variable is assigned to Ptr. So, for second function we are just sending the address directly as input parameter. You people get my point right, the function's input parameter is a pointer and the input is also a pointer (with the address of another variable). So this just an equivalent to pass by value (not with values, but with addresses in pointer variables)

    Can I call this as pass by value then ?

    Am I clear or do I need to be more descriptive....
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    C uses pass by value only; even when you pass a pointer as a parameter, the
    value of that pointer is passed to the function. C++ also has a pass by reference
    mechanism but that's not what you're using in your example code.

    kind regards,

    Jos

    Comment

    • arunmib
      New Member
      • May 2007
      • 104

      #3
      Thanks for the reply Jos.

      So you mean here that if I use the same code in C++, then it can be used as pass by reference also. If yes, can you just brief me about the mechanism how it is handled or some location where I can look for answers....

      Comment

      • pradeep kaltari
        Recognized Expert New Member
        • May 2007
        • 102

        #4
        Originally posted by arunmib
        Thanks for the reply Jos.

        So you mean here that if I use the same code in C++, then it can be used as pass by reference also. If yes, can you just brief me about the mechanism how it is handled or some location where I can look for answers....
        Hi,
        In C, pointers also occupy some place in the memory to store the address.
        In C++ there is something called reference type. This is just like giving an alias/ new name to the variable.
        Code:
        int main()
        {
        ...
        int i=5;
        ...
        int &j=i;
        ...
        }
        Here j does is not allocated with memory. Instead it refers to the same location of i. Consider the following pass-by-reference function call in C++:

        Code:
        int main()
        {
        ..
        int i=5;
        ...
        call function(i);
        ...
        }
        void function(int &j)
        {
        ...
        }
        I hope this was helpful.

        Regards,
        Pradeep

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          Pradeep response is extremely close. The only thing that wasn’t mentioned is that a reference can take up memory. However, the compiler's optimiser may realise it is not necessary and not bother.


          Adrian

          Comment

          • pradeep kaltari
            Recognized Expert New Member
            • May 2007
            • 102

            #6
            Originally posted by AdrianH
            Pradeep response is extremely close. The only thing that wasn’t mentioned is that a reference can take up memory. However, the compiler's optimiser may realise it is not necessary and not bother.


            Adrian
            Hi,
            I read it somewhere that its unspecified whether references require memory or not. Could anybody throw some light on this?

            Thanks,
            Pradeep

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by pradeep kaltari
              Hi,
              I read it somewhere that its unspecified whether references require memory or not. Could anybody throw some light on this?

              Thanks,
              Pradeep
              I don't know what the standard says, but if the compiler can remove the need for allocating memory, it will try to. But there are times that this may not be possible.

              It is probably unspecified so that the compiler manufacturers can do as much or as little as possible.


              Adrian

              Comment

              • pradeep kaltari
                Recognized Expert New Member
                • May 2007
                • 102

                #8
                Originally posted by AdrianH
                I don't know what the standard says, but if the compiler can remove the need for allocating memory, it will try to. But there are times that this may not be possible.

                It is probably unspecified so that the compiler manufacturers can do as much or as little as possible.


                Adrian
                Ok. Thank You Adrain.

                Regards,
                Pradeep

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Originally posted by pradeep kaltari
                  Ok. Thank You Adrain.

                  Regards,
                  Pradeep
                  No prob. Glad to help.


                  Adrian

                  Comment

                  • arunmib
                    New Member
                    • May 2007
                    • 104

                    #10
                    Hey thanks to adrian and pradeep for making thing clear.....

                    Comment

                    • AdrianH
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1251

                      #11
                      Originally posted by arunmib
                      Hey thanks to adrian and pradeep for making thing clear.....
                      Glad to help.


                      Adrian

                      Comment

                      Working...