Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: My problems to understand the Perl documentation

by AnomalousMonk (Bishop)
on Aug 22, 2020 at 23:54 UTC ( #11121001=note: print w/replies, xml ) Need Help??

in reply to My problems to understand the Perl documentation

... two glossaries. One general and computer science ...

I think that a general, CS-ish glossary or reference source is best provided by existing on-line resources like Wikipedia, to which PerlMonks can easily link. See expression, subroutine, function, etc. Perl should stick to documenting Perlish things. (Only Perl can gloss Perl.)

(Note that in Wikipedia, the CS definitions of "subroutine" and "function" are synonymous | discussed in the same article (per LanX here).)

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

Replies are listed 'Best First'.
Re^2: My problems to understand the Perl documentation
by LanX (Sage) on Aug 23, 2020 at 00:34 UTC
    > (Note that in Wikipedia, the CS definitions of "subroutine" and "function" are synonymous.)

    Linking to the same page doesn't mean they are "synonymous".

    WP entries on programming are written by programmers. If their main language is redefining a concept they'll take it as normative.

    Compare the notion of "closure" in PHP for "anonymous subroutine" which is ... "unfortunate". In Python are anonymous subroutines aka "lambdas" restricted to one expression only which is ... "even more unfortunate".

    IOW various WP authors will introduce their own truth.

    Anyway this WP goes into some details to explain why the concept vary between different languages.

    Some programming languages, such as Pascal, Fortran, Ada and many dialects of BASIC, distinguish between functions or function subprograms, which provide an explicit return value to the calling program, and subroutines or procedures, which do not. In those languages, function calls are normally embedded in expressions (e.g., a sqrt function may be called as y = z + sqrt(x)). Procedure calls either behave syntactically as statements (e.g., a print procedure may be called as if x > 0 then print(x) or are explicitly invoked by a statement such as CALL or GOSUB (e.g., call print(x)). Other languages, such as C and Lisp, do not distinguish between functions and subroutines.

    In strictly functional programming languages such as Haskell, subprograms can have no side effects, which means that various internal states of the program will not change. Functions will always return the same result if repeatedly called with the same arguments. Such languages typically only support functions, since subroutines that do not return a value have no use unless they can cause a side effect.

    In programming languages such as C, C++, and C#, subroutines may also simply be called functions, not to be confused with mathematical functions or functional programming, which are different concepts.

    So subroutines and functions are synonymous in Perl, but not in general.

    To be more precise Perl's sub is a meta construct implementing all features from procedures and functions like known in older languages, from this perspective the names are linguistically just "pars pro toto" in Perl.

    In Perl you can use a sub like

    • a function
    • a procedure
    • a method
    • a closure
    • a lambda aka anonymous sub
    • an operator (via overloading)

    so the wordings become sometimes fuzzy.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    ) the listed items are not exclusive, there are overlaps.

      > (Note that in Wikipedia, the CS definitions of "subroutine" and "function" are synonymous.) Linking to the same page doesn't mean they are "synonymous".

      No, of course Wikipedia isn't the arbiter of all knowledge and, at times, it's downright wrong but how, exactly, would you characterize the difference between a subroutine and a function? To my mind, there's no difference and methods belong in the same category since they're just functions that supply the object as the first parameter. void functions are still functions and Perl is still written in C.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2022-05-27 00:24 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (94 votes). Check out past polls.