BerandaComputers and TechnologyAsk HN: How come there's a distinction between defining vs. calling a...

Ask HN: How come there’s a distinction between defining vs. calling a function?

In pure functional languages, like Haskell, a function with no arguments is treated as its result.

foo : Int
foo =
1 + 1

Whereas in most other imperative languages, there's a distinction between defining a function, and calling the function. Example in javascript

// defining a function
const foo = () => 1 + 1

// calling a function
foo()

If you want to call a function right after defining it.

const foo = (() => 1 + 1)()

How come most languages make this distinction? The only thing I could think of is that it gives fine-grained control to programmer when something is executed. If it wasn't, then you'd have to rely on lazy evaluation.

Are there other reasons? Was it a historical accident?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments