If a function has nothing specific to return, it is often customary to return one of the input parameters (the one that is seen as the primary one). Doing this allows you to use "chained" function calls in expressions. For example, you can do
char buffer[1024];
strcat(strcpy(buffer, "Hello"), " World");
specifically because strcpy
returns the input dst
value as its result. Basically, when designing such a function, you are supposed to choose the most appropriate parameter for "chaining" and return it as the result (again, if you have noting else to return, i.e. if otherwise your function would return void
).
Some people like it, some people don't. It is a matter of personal preference. C standard library often supports this technique, memcpy
being another example. A possible use case might be something along the lines of
char *clone_buffer(const char *buffer, size_t size)
{
return memcpy(new char[size], buffer, size);
}
Most of the time people find the fact that memcpy
returns the same pointer as the one passed to it confusing, because there is a popular belief that returning a pointer form a function should normally (or always) indicate that the function might reallocate the memory. While this might indeed indicate the latter, there's no such hard rule and there has never been, so the often expressed opinion that returning a pointer (like memcpy
does) is somehow "wrong" or "bad practice" is totally unfounded.