QamoosTech
Programming FundamentalsFunctions and scopeIntermediate

First-Class Function

PronunciationFURST klas FUNK-shun

Definition

A language has first-class functions when functions are treated like any other value: you can store them in variables, pass them around and return them.

Where you hear it

In Python and JavaScript explanations of callbacks, closures and decorators.

Examples

  • Because functions are first-class, we can keep them in a dictionary and call them by name.
  • JavaScript functions are first-class, so you can pass one as an argument.
  • Because functions are first-class, we can pass the validator as an argument to the form builder.

Common mistake

Mixing up referring to a function with calling it. f is the function itself; f() runs it.

Don't confuse with

A higher-order function, which is one that uses functions as inputs or outputs. First-class is the capability that makes it possible.

Say it at work

  • In a meeting or chatSince functions are first-class here, a lookup table beats the if-chain.
  • In an email or ticketStore the handler in a variable and pass it along.