Pass-by-Value vs. Pass-by-Reference - C++ Function

C++ examples for Function:Function Parameter

Description

Pass-by-Value vs. Pass-by-Reference

Demo Code

#include <iostream>

// triples and returns n by value
int tripleByValue(int n) {
  return n * n * n;
}
// triples and returns n by reference
int tripleByReference(const int& n) {
  return n * n * n;
}

int main(int argc, const char* argv[]) {
  int count = 3;//from  w w  w  . ja  va  2s .  c om

  std::cout << "tripleByValue = " << tripleByValue(count) << std::endl;
  std::cout << "tripleByReference = " << tripleByReference(count)
    << std::endl;

  return 0;
}

Result


Related Tutorials