... a function is a subroutine ... that returns a value (or a list of values).

In general, your distinction between function and subroutine as, respectively, returning or not returning a value seems of little use. Is there really any formal distinction here in any language?

In some programming languages, such as C, you can define either a "pure" subroutine or a function ...

I'm confused by your use of the term "pure" as it relates to subroutines/functions. As I understand it, a pure function is one whose behavior is not affected in any way by the state of the system in which it's running, has no effect upon the system other than by its return value, and creates no persistent state; the function has no "side effects." Any language can have such functions. As such, a pure function returning void would be useless. (Some languages allow the definition of functions with a pure attribute and purity is enforced by the compiler, but I'm not aware that ISO-C is one of them; I may be behind the latest standard here. (FWIW, a little Googling shows that GNU C has an  __attribute__ "pure" feature.)) Your  myfunction() C function is operationally pure; your  myroutine() function is not: it has the side effect of assignment to the  z variable and the effect of this assignment persists after the function terminates execution. (All three of the Perl functions you give as examples are operationally pure, but as you say, Perl neither defines nor enforces any formal notion of function purity.)

Update: See the discussion of the implementation of Pure Functions in the D language.


Give a man a fish:  <%-{-{-{-<


In reply to Re^4: I'm trying to consolidate my functions into subroutines by AnomalousMonk
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.