2
votes

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.

3
given the same two strings as parameters you don't give strings, you give pointers. - KamilCuk
@KamilCuk hmm, this is interesting. If you give the same pointers to strcmp you 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 to strcmp. - VLAZ
For example gcc compiler has "const" and "pure" function attribute. In that description, memcmp could be "pure", but may not be "const". "const" functions cannot access the values behind the pointers. - KamilCuk
@KamilCuk Both memcmp and strcmp have 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

3 Answers

3
votes

strcmp() is a pure function, since the result is dependant solely on the parameters and in addition, it does not modify a global state.

Of course, improper use of strcmp() may invoke undefined behaviour, but this is not relevant for it being pure or impure.

Edit:

A pure function only yields the same results, when the global memory referenced is also the same. Functions, which do not require that are called constant functions.

GCC documentation provides strlen() as an example of a pure function. Indeed, this function takes a pointer as a parameter, and accesses it to find its length. This function reads global memory (the memory pointed to by parameters is not considered a parameter), but does not change it, and the value returned derives from the global memory accessed.

2
votes

The behavior of strcmp(), like that of strlen() depends only on the contents of the memory pointed to by the const char * arguments. No side effects are intended to occur, and if the potential changes in the virtual memory management system are considered invisible to the program, it can be considered pure.

Note however that the closely related function strcoll() may have side effects depending on the locale handling implementation, and should not be considered pure.

Similarly isdigit() is pure but isalpha() is not.

1
votes

It's not pure, in the sense that the arguments are mutable. This means that the result of calling strcmp(a, b) will depend on what those pointers are pointing at. The return value should only depend on the value of the arguments.

Another requirement for a pure function is that it has no side effects. This is fulfilled. Well, in practice it is. It depends on how you see it. I can almost promise that all implementations of it has no side effects, but the standard itself does not require them to have no side effects.

In general, you can for all relevant purposes assume that it is does not have side effects. The only exception is if the call invokes undefined behavior, but that's something you should avoid anyway.

Further strcmp() does not modify any variables or call any functions, thus it does not have any side effects.

You cannot say for sure that it does not call any functions. That's implementation details.