Skip to content

Comment on Is Python call-by-value or call-by-reference? Neither.parent

Comments

C++ supports call-by-reference (and also call-by-value), here's an example of call-by-reference:

    #include <iostream>
    void swap(int &x, int &y) {
      int temp = x;
      x = y;
      y = temp;
    }
    int main() {
      int a = 3;
      int b = 4;
      std::cout << a; // 3
      std::cout << b; // 4
      swap(a, b);
      std::cout << a; // 4
      std::cout << b; // 3
    }
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.