Nice writeup. In general I am not a fan of multiple returns per function (especially for very simple functions), although I am not sure I am following your argument.
In order for a function to leak memory in this fashion you'd need to be working in a language that lacks a garbage collector. So let's say the above is C or C++. If that's the case, the "request" struct would/should always be passed by reference (a pointer) and not by value. I am sure there are some situations where this type of single return is favourable due to potential memory leaks - but I'm just not sure this example is one of them. :)
I also think perhaps there is a bug in the solution as you always assign "cost=10" before returning it!
Thanks for the feedback Andrew! I've fixed the last example code in order to better demonstrate what I meant.
Yes, the argument I was trying to make is mainly concerned, but not limited to garbage collection. Actually, any resource may need to be 'freed' at the end of the function; boolean locks, system resources etc...
Also fixed the bug you spotted, thanks!
Yeah, nice one. The last example demonstrates the concept much better now.
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.