I am learning about side-effects and pure functions. I know that pure functions have no side-effects and their return value is the same for the same arguments. I would like to know whether the C function strcmp() is a pure function. I believe that it is pure, due to the fact that given the same two strings as parameters, the result will always be the same. Furthermore, strcmp() does not modify any variables or call any functions, thus it does not have any side effects.
However I am not sure whether my reasoning is correct.
given the same two strings as parametersyou don't give strings, you give pointers. - KamilCukstrcmpyou are not going to necessarily get the same result as there might be different data at the end of those. And if you give pointers to the same strings, that's not necessarily the same input value-wise tostrcmp. - VLAZmemcmpcould be "pure", but may not be "const". "const" functions cannot access the values behind the pointers. - KamilCukmemcmpandstrcmphave the same "pureness" problems, in that it's passed pointers to data. Two calls may have the same pointers, but may give different result if the data the pointers are pointing to have changed between the calls. That makes them, in a way, impure. - Some programmer dude