Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a keyword or a statement, and a function call?

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?

like image 769
Geoff Avatar asked May 19 '11 06:05

Geoff


3 Answers

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.

like image 70
blubb Avatar answered Sep 24 '22 20:09

blubb


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.

like image 35
Cryo Avatar answered Sep 23 '22 20:09

Cryo


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.

like image 23
Trevor Burnham Avatar answered Sep 26 '22 20:09

Trevor Burnham