Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Arguments Into Functions (http://www.go4expert.com/articles/arguments-into-functions-t821/)

pradeep 22May2006 18:50

Arguments Into Functions
 
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: c

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: c

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: c

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.

rahul.mca2001 6Mar2008 13:36

Re: Arguments Into Functions
 
there was a lot of confusion now cleared


All times are GMT +5.5. The time now is 17:42.