Skip to content
BENIBIX Learn

Lambda Function

Development🧭 Programming ParadigmsFunctional Programming

Also known as Lambda, Anonymous Function, Arrow Function

A small function in one line, often with no name. You use it once.

Think of it like

Think of it like a sticky note with one quick job on it. You use it once and throw it away.

Example

Jack sorts a list of players by score with a lambda. The whole rule for sorting fits inside the sort call.

Why it matters

Lambdas keep a tiny rule right where it is used. You do not need to jump elsewhere to read it.

Definition

A lambda is a small function written in one line, often with no name. You use it where a function is needed just once, like inside map or sort. Python writes it with the word lambda; JavaScript uses an arrow, like (x) => x * x. It can hold only a short piece of code. For anything longer, a normal named function is clearer.

Example code

python
>>> square = lambda x: x * x
>>> square(4)
16
A one-line function that squares a number.

Common mistake

Packing a long rule into one lambda. If it needs a comment to read, give it a name instead.

Key takeaways

  1. 1
    One line. A short piece of code.
  2. 2
    Often nameless. Used in one place.
  3. 3
    Arrow or lambda. Same idea, two spellings.

Compare