Saurabh.v
Saurabh.v
Feb 23, 2017 · 1 min read

What if we pass a mutable object as argument to a function and the function reads this mutable object to do some calculations but does not modify it. Will it be considered pure or impure ?

Anup Cowkur

I am not sure but it may not be pure because while the function is accessing it, some other part of the program can modify this mutable object.
This can make the function to return some different output because object has been mutated.
For example:

class Box {
int length = 5;
}
function(Box b):
length = b.length + 1
return length
}
Now lets say this function is called with object of Box having length 5 but before the function read its value, some other part of the program changed length of box object to 10.
Now function will start to return 11 as output.

Kind of function is interacting with other part of program. How is it pure ?

Saurabh.v

Written by

Saurabh.v

bits_pilani_alumnus, software _engineer at make_my_trip, functional_programming_enthusiast, experience_seeker, poet and learner