views:

114

answers:

1

Recently, I've been learning different programming langages, and come across many different names to initalize a function construct.

For instance, ruby and python use the def keyword, and php and javascript use function, while VB uses void, etc.

My question is: What is the reasoning and differences between these different constructs and keywords?

+1  A: 

C and all the many languages that copied from it don't use any keywords to declare a function -- you just to <returned type> <function name>(<arguments). void is just the way to say "no type at all", i.e. what we used to call a "subroutine" or "procedure" (a "function" that doesn't return any value).

function, as used in Javascript, is clearly a sharp and immediately obvious keyword, equally usable for both named and nameless functions. IMHO, the best of the bunch.

I don't know what Guido was thinking when he chose def for named functions and lambda for unnamed ones (the Netherlands, where he was born and raised and where he was living at the time, has many wonderful beers, of course;-). Ruby just adopted def from Python, maybe just because it's short (but it has no sensible meaning or interpretation...!).

Alex Martelli
`def` has no sensible meaning? Isn't it short for "define"?
Mark Rushakoff
But, "define" _what_? Why should "defining" indicate specifically named functions rather than unnamed ones, or classes, or dicts, or...?! And, why "define" rather than "default", "defer", "defect", "defenestrate", "defunct"...? I love Python, and Guido's a friend, but `def` is just _the_ worst keyword in the language!-)
Alex Martelli
As pointed out, `void` is a return type and not a keyword to mark a function as `def` is. `def` seems to me to be borrowed from functional languages where even functions are first class types.
dirkgently
Functions are first-class in Python (`def`) almost as much as in Javascript (`function`) -- and SML (which uses `fun`, as does F#), O'Caml (`let`, like Scheme), Haskell (no keyword needed, e.g. `twice x = x + x` is how you define a function returning twice its argument). Scala does use `def`, but it's more recent than Python and Ruby; Lisp uses `defun`.
Alex Martelli