Hacker News new | ask | show | jobs
by samatman 3464 days ago
Functions have properties which are completely defined by their inputs and outputs.

Subroutines (I prefer the term procedure) are just a sequence of commands. They may use arguments and return a value but they can also do anything else.

Functions, real functions, can be reasoned about, composed, mapped over collections, and otherwise trusted to behave themselves. Very few programming languages provide strict functions. One may write them, of course, if one is careful, but that is doing work a compiler could be doing for you.

1 comments

There's also a disconnect with using a PL's concept of a function (procedure, calling convention) instead of the simple intellectual concept (composition) whether or not you're concerned with the mathematical concept (purity); language compilers or runtimes often perform poorly because as a matter of course every function call adds to a stack (failing to perform obvious inline or tail call optimizations), involves a dispatch that might be more expensive than the function itself (just in case inheriting code overloaded it or because every procedure lives in a run-time mutable table), or causes potentially large copies of arguments (to pretend at them being immutable by called code), etc. -- paying costs which in many cases could be avoided.