On Tuesday, 13 September 2016 at 06:59:10 UTC, Jonathan M Davis wrote:
On Tuesday, September 13, 2016 03:33:04 Ivy Encarnacion via Digitalmars-d- learn wrote:

 A pure function cannot call any function that is not pure [...]

I've read that a lot but it's not true. A pure function can call impure function. The restriction is, that the impure function called within the pure function does not depend or mutate on state existing outside of that function. If the called function changes local variable, it has no bearing on outside the scope.
Here a contrived example in C.

size_t strlen(const char *str); is pure. If I define it this way for example:

size_t my_strlen(const char *str)
{
  char temp[50];

strlcpy(temp, str, sizeof temp); /* strlcpy is not pure as it mutates something outside of its "scope" */

  return strlen(str);
}

That function is pure. There is no visible change outside of it.

my_strlen and strlen have exactly the same properties.

Reply via email to