DEV Community

Discussion on: Functional programming basics part 1: Pure function

Collapse
 
pizmovc profile image
Luka

I don't think that the void return type has anything to to with function purity. A function can be pure even if it returns some value. It just shouldn't modify anything outside of it and does not use any variables that are defined outside of its scope. Its return value should be determined solely by its input parameters.

Collapse
 
tux0r profile image
tux0r

Its return value should be determined solely by its input parameters.

Wouldn't that make any function in strongly typed languages "pure"?

Thread Thread
 
pizmovc profile image
Luka

No.

For example:

int a = 5;

int sumOfNumbers(int x, int y) {
  a = 1;
  return x + y;
}

int sum = sumOfNumbers(12, 33);

This function is not pure as it modifies a.