Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does OCaml have the ability to pass by reference?

In C++ a program can pass a reference, not value to a function.

void incrementInt(int &x) 
{
  ++x;
}

Does OCaml offer this same functionality?

like image 684
Chuck Norrris Avatar asked Oct 12 '16 16:10

Chuck Norrris


1 Answers

No, there is no strict equivalent.

There are refs, which are like pointers to new-allocated memory, and there are records, arrays, objects and values of other compound data types, that are passed "by object reference", which again means they act like pointer to new-allocated memory.

However there's no equivalent to a pointer to a variable or a C++ reference.

like image 75
sepp2k Avatar answered Sep 17 '22 02:09

sepp2k