3 ms·
> As illustrational example consider this small code fragment: Consider this crap small code fragment. Any function that returns void is almost certainly wron
by zabzonk 5y ago
> As illustrational example consider this small code fragment:
Consider this crap small code fragment. Any function that returns void is almost certainly wrong.
- gpderetta 5y agowhat should abort() return?
- turminal 5y agoEnlighten us, please
- zabzonk 5y agoAny function that does not return a value must have a side effect, else why call it. Calling functions that have side effects (and particularly don't indicate if the side effect was succesful), is not a great idea.
- torstenvl 5y agoAre there any software projects of substantial scale that are purely FP? Even things like I/O or dynamic memory allocation have side effects. So while FP has some great ideas implemented at scale (first class functions and MapReduce perhaps the most well-known), they don't seem very useful by themselves. EDIT: I'm not objecting to the idea "functions should return information," I'm objecting to the idea that side effects are "not a great idea."
- zabzonk 5y agoYou generally want to test if an "I/O or dynamic memory allocation" worked. BTW, I am basically a C++ programmer, not some functional maniac. But whenever I see a void return type, I think something is wrong.
- worik 5y agoNo it is not.
- burnished 5y agoWhat does 'wrong' mean in this context? I mean, if you're against side effects and thus believe functions should always return a value that seems reasonable. Except that for a toy example where the purpose is to exercise some hardware and measure results it does not seem to be applicable.
- Koshkin 5y agoYou are right. That's why in good old languages they were called 'procedures.'
- zabzonk 5y agoAnd those languages were badly designed. Which is why we don't have "procedures" or "subroutines" in modern programming languages" - just functions.
- Koshkin 5y agoWell that's unfortunate. I suspect the fault lies with C, the early versions of which did not have the 'void' keyword, and 'int' was assumed as the default return type - which also meant that when such "function" did not include a 'return' statement it "returned" whatever garbage was left in the register. Instead of adding the keyword 'void' (to be abused later as the empty arg list) they could have added 'proc' instead, but that would have complicated the parsing, and so they went with the 'void' pseudo-type as an indication of the fact that the function is not really a function but a procedure.
- MauranKilom 5y agoGood thing printf returns something, otherwise it might wrong or useless!