Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^5: I'm trying to consolidate my functions into subroutines

by afoken (Chancellor)
on May 17, 2017 at 19:39 UTC ( [id://1190495]=note: print w/replies, xml ) Need Help??


in reply to Re^4: I'm trying to consolidate my functions into subroutines
in thread I'm trying to consolidate my functions into subroutines

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.

/tmp>cat evil.c #include <stdio.h> #include <stdlib.h> typedef int (*func_returning_int)(void); typedef void (*func_returning_void)(void); void dummy(void) { puts("Hello World"); } int main(int argc, char ** argv) { func_returning_void f = &dummy; int i = 0; f(); i = ((func_returning_int)f)(); printf("i=%i\n",i); return 0; } /tmp>CFLAGS="-Wall -pedantic" make evil cc -Wall -pedantic evil.c -o evil /tmp>./evil Hello World Hello World i=12 /tmp>

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^6: I'm trying to consolidate my functions into subroutines
by Anonymous Monk on May 18, 2017 at 18:04 UTC

    It is even possible to call procedures as function. Of course, this results in undefined behavior,

    C99 says,

    If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
    (There is, however, an exception for the main() function that has an implicit "return 0" before closing brace.)

    Further, the C standard does not define or use such terminology "procedure" or "subroutine". These words no not even appear in remarks or informal parts. I think this is a very deliberate choice.

    The logic or rationale behind those dangerous jump statements (goto/return/break/continue) is that the compiler is unable to follow the program flow or value propagation in the absolute sense. Gcc may warn that a variable may be used uninitialized, while the programmer can easily see this can not happen. The compiler may not even know if the return value was properly computed or if undefined behavior was somehow involved. Thus C does not prohibit questionable constructs such as goto into a scope of a variable (past its initialization).

    But note, even when "may be uninitialized" warning is a false positive, it has informational value for the programmer. It may signal a refactoring opportunity, such that would allow the compiler better insight into your program and possibly better code as a result.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1190495]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-20 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found