Arguments Into Functions

Discussion in 'C++' started by pradeep, May 22, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    There are three techniques used to pass variables into a C or C++ functions.
    1. Pass by value C/C++
    2. Pass a pointer by value C/C++
    3. Pass by reference C++ only
    Pass by value

    The function receives a copy of the variable. This local copy has scope, that is, exists only within the function. Any changes to the variable made in the function are not passed back to the calling routine. The advantages of passing by values are simplicity and that is guaranteed that the variable in the calling routine will be unchanged after return from the function. There are two main disadvantages. First, it is inefficient to make a copy of a variable, particularly if it is large such as an array, structure or class. Second, since the variable in the calling routine will not be modified even if that's what is desired, only way to pass information back to the calling routine is via the return value of the function. Only one value may be passed back this way.

    Code:
    void IncreaseMe(int theInt);
      main()
      {
      int i;
      i = 5;
      IncreaseMe(i);
      cout << "i is %d" << i << " \n";
      }
      
      void IncreaseMe(int i)
      {
      i = i + 1;
      }

    Answer: i is 5.
    Since i was passed by value, the local copy in the function gets incremented. The copy in main remained unmodified and still will be five.


    Passing a pointer by value

    A pointer to the variable is passed to the function. The pointer can then be manipulated to change the value of the variable in the calling routine. It is interesting to note that the pointer itself is passed by value. The function cannot change the pointer itself since it gets a local copy of the pointer. However, the function can change the contents of memory, the variable, to which the pointer refers. The advantages of passing by pointer are that any changes to variables will be passed back to the calling routine and that multiple variables can be changed.

    Code:
    void IncreaseMe2(int *theInt);
      main()
      {
      int i;
      int *pt;
      i = 5;
      pt = &i; /* set the pointer to the address of I */
      IncreaseMe2(pt);
      cout << "i is %d" << i << " \n";
      }
      
      void IncreaseMe(int *i)
      {
      *i = *i + 1;
      }
      
    Answer: i is 6.
    Since i was passed by reference, the function received a pointer to the variable. By properly dereferencing, i was incremented to 6. Not that while i was changed, the pointer to i, pt was not.

    Passing by reference (C++ only)

    C++ provides this third way to pass variables to a function. A reference in C++ is an alias to a variable. Any changes made to the reference will also be made to the original variable. When variables are passed into a function by reference, the modifications made by the function will be seen in the calling routine. References in C++ allow passing by reference like pointers do, but without the complicated notation. Since no local copies of the variables are made in the function, this technique is efficient. Additionally, passing multiple references into the function can modify multiple variables.

    Code:
    void IncreaseMe(int &theInt);
      main()
      {
      int i;
      i = 5;
      IncreaseMe(i);
      cout << "i is %d" << i << " \n";
      }
      
      void IncreaseMe(int &i)
      {
      i = i + 1;
      }
      
    Answer: i is 6.
    Since I was passed by reference, the variable in the calling routine is modified. Also, note that the main program is identical to the main program in the passing by value example. The compiler does the hard work. The programmer does not need to be concerned with the complicated notation and the explicit referencing and differencing seen when using pointers.
     
  2. rahul.mca2001

    rahul.mca2001 New Member

    Joined:
    Feb 13, 2008
    Messages:
    103
    Likes Received:
    0
    Trophy Points:
    0
    there was a lot of confusion now cleared
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice