What is the use of return value of strrev()
? Even after reversing the string, the value will be modified in the given string.
Ex: char *strrev(char *src);
Here after the string is reversed, the output will be present in the src. In that case what is the use of returning the string?
Either the return value or the string which we are passing also acts as output, that is enough. In that case what is the use of return value of strrev()
?
To allow chaining.
Imagine you wanted to reverse a string, then reverse it back again (contrived example, but it makes the point). With chaining, you can do this:
strrev(strrev(charBuffer));
If sttrev
returned void
, you'd have to do:
strrev(charBuffer);
strrev(charBuffer);
to get the same result.
As @WernerHenze says in his comment, it also allows you to directly work with the output in function calls, like this:
printf("%s\n", strrev(charBuffer));
The possibility of chaining basically gives more flexibility to the programmer as to how the code is structured.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With