CS Electrical And Electronics
@cselectricalandelectronics

Difference between pass by value, pass by reference, pass by address?

All QuestionsCategory: Cpp LanguageDifference between pass by value, pass by reference, pass by address?
Anonymous asked 3 years ago
1 Answers
Anonymous answered 3 years ago

C++ Supports 3 types of parameter passing

  • Pass By Value
  • Pass By Address
  • Pass By Reference

 
Pass By Value
 

void tryswap(int, int);

tryswap(a, b);

void tryswap(int x, int y) {
  int temp = x;
  x = y;
  y = temp;
}


Pass By Reference
 
 

void myswap(int &, int &);

myswap(a, b);

void myswap(int &r1, int &r2) {
  int temp = r1;
  r1 = r2;
  r2 = temp;
}



Pass By Address
 

void myswap(int *, int *);

myswap(&a, &b);

void myswap(int *p1, int *p2) {
  int temp = *p1;
  *p1 = *p2;
  *p2 = temp;
}