Re^5: I'm trying to consolidate my functions into subroutinesby afoken (Abbot)
|on May 17, 2017 at 19:39 UTC||Need Help??|
Re^5: I'm trying to consolidate my functions into subroutines and Re^5: I'm trying to consolidate my functions into subroutines are already good answers.
Old home computer BASICs (those with line numbers, e.g. C64, ZX 81, ZX Spectrum) have functions and subroutines. Functions (like SIN, COS, PEEK, RAND) are build-in, and there is usually no way to create user-defined functions. Procedures are simply some lines of code called by GOSUB and ending with RETURN, not able to return a value except by modifying global variables.
Pascal and Fortran share the distinction between functions and procedures, forcing the programmer to check the return value, or at least use it in some way. Common workarounds are IFs without any code in the THEN block, or using the unwanted return value as input for a procedure with nearly no side-effects. In Sinclair BASICs (see below), you often see RAND USR 16514 to call machine code at address 16514 and discarding the return value. Actually, the return value is used to initialise the random number generator. But often, the machine code simply does not return, and so even PRINT USR 16514 can be found.
C and C++ have, from a syntactical point of view, only functions. But strictly speaking, any function returning void does just that: It returns nothing and thus is a procedure. Unlike Pascal, Fortran, and the old home computer BASICs, C and C++ allow calling any function like a procedure (i.e. fooBar(); instead of someVar=fooBar();), discarding any return value. This is the source of many bugs, because often functions return an error code or a success indicator; and lazy programmers tend to ignore the possibility of an error, especially in seemingly trivial functions. It is even possible to call procedures as function. Of course, this results in undefined behavior, but gcc accepts that without any warnings, even with -Wall and -pedantic. The logic is simple: You are the programmer, you are explicitly casting, so you must know what you are doing.
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)