I was thinking about this recently since Python 3 is changing print
from a statement to a function.
However, Ruby and CoffeeScript take the opposite approach, since you often leave out parentheses from functions, thereby blurring the distinction between keywords/statements and functions. (A function call without parentheses looks a lot like a keyword.)
Generally, what's the difference between a keyword and a function? It seems to me that some keywords are really just functions. For example, return 3
could equally be thought of as return(3)
where the return function is implemented natively in the language. Or in JavaScript, typeof
is a keyword, but it seems very much like a function, and can be called with parentheses.
Thoughts?
A function is executed within a stack frame, whereas a keyword statement isn't necessarily. A good example is the return
statement: If it were a function and would execute in its own stack, there would be no way it could control the execution flow in the way it does.
Keywords and functions are ambiguous. Whether or not parentheses are necessary is completely dependent upon the design of the language syntax.
Consider an integer declaration, for instance:
int my_integer = 4;
vs
my_integer = int(4)
Both of these examples are logically equivalent, but vary by the language syntax.
Programming languages use keywords to reserve their finite number of basic functions. When you write a function, you are extending a language.
Keywords are lower-level building blocks than functions, and can do things that functions can't.
You cite return
in your question, which is a good example: In all the languages you mention, there's no way to use a function to provide the same behavior as return x
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With