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". ;-)

In reply to Re^5: I'm trying to consolidate my functions into subroutines by afoken
in thread I'm trying to consolidate my functions into subroutines by Peter Keystrokes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.