I want to know the difference between call by reference and call by pointer..

I believe you are referring to:

Call by reference and call by value.

In a nutshell-- a call by value 'copies' the values at the memory address onto the stack when the function is called. A call by reference passes the address of the value to the function (no copying of values is done).

That may be overly simple but it is a nutshell after all. Why does it matter? If you have a large data structure it can be very time consuming to copy the entire memory value(s) to the stack. The code can be much faster to pass the address. Also consider scope in C++. If you pass values into a function they are copied and are manipulated in the scope of that function. When the function returns those values are not updated in the larger from where they came... here is a program to prove that. This is a slightly modified program I wrote in C, I know it isn't C++ but the concept is generally the same.

#include <stdio.h>

void swap(int *a, int *b);
void swap2(int a, int b);

int main(void){
  int a, b;

  a = 10;
  b = 20;

  // swap passing the addresses of a and b to the function
  swap(&a, &b);
  printf("REFERENCE:\nA:%d\nB:%d\n", a, b);

  // reset the values of a and b
  a = 10;
  b = 20;

  // swap passing the values of a and b to the function
  swap2(a, b);
  printf("VALUE:\nA:%d\nB:%d\n", a, b);
  return 0;
}

void swap(int *a, int *b){
  int temp;
  temp = *b;
  *b = *a;
  *a = temp;
}

void swap2(int a, int b){
  int temp;
  temp = b;
  b = a;
  a = temp;
}

If you are not familiar with plain C you can basically replace the printf calls with cout calls, and you could overload the swap function without creating a distinct name for the second type of function. Also there are some slight semantic differences with pointers/references in C++ but for something as basic as this I think it gets the point across.

Edited 5 Years Ago by \007: n/a

First of all, what you are referring to is called "pass-by-reference" and "pass-by-pointer" because it pertains to the passing of the parameters to a function, not to the calling of the function. Those who use those "call-by-" terms are simply sloppy with their terminology.

Their are two main purposes for using pass-by-reference or pass-by-pointer, as opposed to pass-by-value. First, you can avoid the copying of the object in question, this is particularly important if the object is expensive to copy (large in size), or even non-copyable. The cost for that is, of course, that all access to the variable is indirect (requires dereferencing). This use-case often lead to the idiomatic const MyClass& param form for passing a parameter. This syntax means that you pass the caller's variable by reference to it, but you forbid the function that takes this parameter to actually modify the variable (which guarantees to the caller that his variable will remain intact after the call).

The second purpose for pass-by-reference or pass-by-pointer is to actually allow for the modification of the caller's variable. This is especially useful when your function needs to return more information than can be conveniently bundled in the return value (i.e. more than one output variable). You can then pass variables by (non-const) reference, modify them in the function, and then the caller will be left with the "results of the function" stored in the variables he passed to the function. This can be implemented with either a reference or a pointer, as so:

#include <iostream>

void set_to_42(int& value) { //pass-by-reference
  value = 42;
};

void set_to_69(int* value) { //pass-by-pointer
  *value = 69;   //notice the dereferencing with * in front of 'value'.
};

int main() {
  int a = 0;
  std::cout << a << std::endl; //OUTPUT: 0
  set_to_42(a);
  std::cout << a << std::endl; //OUTPUT: 42
  int b = 0;
  std::cout << b << std::endl; //OUTPUT: 0
  set_to_69(&b); //notice the address-of with & before 'b'.
  std::cout << b << std::endl; //OUTPUT: 69
  return 0;
};

You can already see one difference between pass-by-pointer and pass-by-reference. When you pass as a pointer, it requires additional syntax to take the address-of the variable and to dereference the pointer to access the variable. In the case of reference parameters, this is essentially done implicitly which leads to cleaner syntax, better readability, which is very important in good coding practices. Because of this implicit ref/deref, references are often thought-of as "aliases" for variables because they behave exactly as if they were the variable they refer to.

Because reference variables are "aliases" they cannot be "re-seated". Which means that once initialized (constructed) to refer to a particular variable, they cannot be made to refer to another variable (or "re-seated"), because all uses of the reference variable is equivalent to the use of the original variable. This is a very nice feature because it wields out bugs that are typical with pointers (like mistakenly re-seating a pointer). Moreover, this feature also makes references non-assignable, which is also useful for forbidding nasty, unsafe referencing schemes. However, if re-seating is necessary in some application, then pass-by-pointer is the fall-back solution.

For the reasons mentioned above, in good quality C++ code, you will very rarely if ever see pass-by-pointer, unless it is legacy C code or code that needs to interface with C, or provide a C compatible interface, because C does not support reference variables. And in most cases where re-seating is necessary, usually, most experienced C++ programmers prefer the use of a smart-pointer instead of a raw-pointer (the use-cases of raw-pointers are rapidly shrinking nowadays, but there are still special cases where they are needed).

Simply call by reference creates a reference to the object and passes that. Think of reference is another name for the object. You can manipulate the data and do not copy the whole object to pass it to the function.

By passing it by a pointer you just pass its adress. You can use and increment decrement this adress to move around the actual object. You still do not copy the object but you have a greater flexibility to manipulate the adress and data both.

This article has been dead for over six months. Start a new discussion instead.