0

In C++ , there are several ways to write a function:

int foo1(int x){return x;} // copy,which is slow
int foo2(const int &x) {return x;} //quote,which is faster
int foo3(int *x){return *x;} // pointer in C

In Mathematica , when I use

x//foo1//foo2

Which way does mma use? Or mma use some other ways?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
AsukaMinato
  • 9,758
  • 1
  • 14
  • 40
  • 3
    High-level languages basically all use pass-by-reference, but Mathematica does that then tries to pretend it's doing pass-by-value. You're really passing around pointers to some kind of Expression object, but when you modify one, Mathematica will copy it for you. – b3m2a1 May 11 '20 at 05:58
  • 2
    In short, Mathematica has the performance of the 2nd/3rd lines but the behaviour of the 1st line. To be precise, it uses copy-on-write. Many similar systems, like MATLAB, also use the same. – Szabolcs May 11 '20 at 07:18
  • 1
    I would also recommend this section of my book, as well as this Q/A. – Leonid Shifrin May 11 '20 at 09:32

0 Answers0