Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm a little bit confused about 'function' and 'lambda'. I've seen some examples showing that the scheme keyword lambda works very similarly to the JavaScript keyword function, but I really don't know how they are related.

I'm told that 'function' and 'method' can be used interchangeably when speaking about objects in .net. I'm wondering if 'lambda' and 'function' similarly mean the same thing. I wonder if 'lambda' has some esoteric meaning, seeing that the Greek letter lambda (λ) appears in so many avatars on this site. To make things even more confusing, in .net, the functional parts of C# refer to function expressions passed to another function as 'lambda expressions', so the word really seems to be all over the place.

I'm also vaguely familiar with the term 'lambda calculus'.

What is the difference between a function and a lambda?

share|improve this question
Nitpick - they are called "lambda expressions", not "lambda functions", at least as far as C#/.NET documentation goes. – Oded Jan 18 '12 at 16:39
@TWith2Sugars - Read the message. Your answer is low quality as it pretty much is just a link, so got converted to a comment. – Oded Jan 18 '12 at 16:51
I wonder if 'lambda' has some esoteric meaning, seeing that the Greek letter lambda (λ) appears in so many avatars on this site. One would hope it would be in reference to lambda calculus, but I have a strange feeling Half Life is to blame for lambda avatars. – Yannis Jan 18 '12 at 16:51
Fair enough, here is the link to the stackoverflow answer: – TWith2Sugars Jan 18 '12 at 16:52
@ZaphodBeeblebrox: I suspect you're correct about the Half-Life influence. :/ – FrustratedWithFormsDesigner Jan 18 '12 at 16:59

The word "lambda" or "lambda expressions" most often refers to anonymous functions. So in that sense a lambda is a kind of function, but not every function is a lambda (i.e. named functions aren't usually referred to as lambdas). Depending on the language, anonymous functions are often implemented differently than named functions (particularly in languages where anonymous functions are closures and named functions are not), so referring to them with different terms can make sense.

The difference between scheme's lambda keyword and Javascript's function keyword is that the latter can be used to create both anonymous functions and named functions while the former only creates anonymous functions (and you'd use define to create named functions).

The lambda calculus is a minimal programming language/mathematical model of computation, which uses functions as its only "data structure". In the lamdba calculus the lambda-symbol is used to create (anonymous) functions. This is where the usage of the term "lambda" in other languages comes from.

share|improve this answer
That is extremely rough. You use define (or let or one of its relatives, or an internal define) to create names -- that's all. There's nothing special in define with respect to functions. – Eli Barzilay Jan 19 '12 at 16:32
@EliBarzilay Well, define does have a special form for defining functions (i.e. you can write (define (f x) (foo)) instead of (define f (lambda (x) (foo)))), but my point was that you can't create a named function using lambda alone, i.e. you can't write something like (lambda f (x) (foo)) to define a function named f that takes one argument like you can with Javascript's function keyword. – sepp2k Jan 19 '12 at 16:44
define has that as a syntactic sugar, so it's not as important as its role as a name binding tool for all values. As for lambda not creating a name by itself: that's an important feature, since it separates name giving from function forms... IMO JS is doing the right thing in allowing the separation while also accepting an optional name for those masses who would be horrified at the idea of a function with no name. (And luckily the size of those masses are in a general decline...) – Eli Barzilay Jan 19 '12 at 18:51

A lambda is simply an anonymous function - a function with no name.

share|improve this answer
Note Quite: lambda's can contain state (as in closure does) that they snag from the context where they are declared. – Loki Astari Jan 18 '12 at 20:22
So could a named function, if the language lets you declare nested functions. – cHao Jan 18 '12 at 23:49
Kudos for making it into the "low quality posts" review tab with an upvoted answer. – Yannis Jan 19 '12 at 13:54
@ZaphodBeeblebrox - Not intentionally, I can assure you. – Oded Jan 19 '12 at 13:59
Not really, a lambda expression in Scheme is just like a function expression with no name -- but there's nothing stopping you from later giving them a name. For example var f = [function(x){return x;}][0]. You could argue that the function value itself has no name, but that would be true for all functions... – Eli Barzilay Jan 19 '12 at 16:30

Answered Here:

Basically Lambda is an anonymous function.

share|improve this answer
So instead of...? – Bryan Oakley Jan 19 '12 at 0:38
Sorry, forgot to remove that part before posting :| – TWith2Sugars Jan 19 '12 at 8:51

In C# Anonymous function is a general term that includes both lambda expressions and anonymous methods (anonymous methods are delegate instances with no actual method declaration).

Lambda expressions can be broken down to expression lambda and statement lambda

Expression lambda:

(int x, string y) => x == y.Length 

Statement lambda is similar to expression lambda except the statement(s) are enclosed in braces:

(int x, string y) => {
         if (x == y.Length) {

When we talk about lambda expressions in JavaScript that basically just means using a function as an argument in a call to another function.

var calculate = function(x, y, operation){
    return operation(x, y);

// we're passing anonymous function as a third argument
calculate(10, 15, function(x, y) {
    return x + y;
}); // 25
share|improve this answer
+1 A lot of people have mentioned that lambdas are anonymous functions, but there is more to it than that. The body (right hand side) of a lambda is often an expression rather than a statement block. The body of a named function being an expression is typically allowed (or required) in functional languages, but not imperative languages. – Zantier 3 hours ago

TL;DR As others pointed out: the lambda notation is just a way to define functions without being forced to give them a name.

Long version

I would like to elaborate a bit on this topic because I find it very interesting. Disclaimer: I have taken my course on lambda calculus a long time ago. If someone with better knowledge finds any inaccuracies in my answer, feel free to help me improve it.

Let's start with expressions, e.g. 1 + 2 and x + 2. Literals such as 1 and 2 are called constants because they are bound to specific fixed values.

An identifier such as x is called variable and in order to evaluate it you need to bind it to some value first. So, basically you cannot evaluate x + 1 as long as you do not know what x is.

The lambda notation provides a schema for binding specific input values to variables. A lambda expression can be formed by adding λx . in front of an existing expression, e.g. λx . x + 1. Variable x is said to be free in x + 1 and bound in λx . x + 1

How does this help in evaluating expressions? If you feed a value to the lambda expression, like so

(λx . x + 1) 2

then you can evaluate the whole expression by replacing (binding) all occurrences of the variable x with the value 2:

(λx . x + 1) 2
      2 + 1

So, the lambda notation provides a general mechanism for binding things to variables that appear in an expression / program block. Depending on the context, this creates sightly different concepts in programming languages:

  • In a purely functional language like Haskell, lambda expressions represent functions in the mathematical sense: an input value is injected into the body of the lambda and an output value is produced.
  • In many languages (e.g. JavaScript, Python, Scheme) evaluating the body of a lambda expression can have side-effects. In this case one can use the term procedure to mark the difference wrt pure functions.

Apart from the differences, the lambda notation is about defining formal parameters and binding them to actual parameters.

The next step, is to give a function / procedure a name. In several languages, functions are values like any other, so you can give a function a name as follows:

(define f (lambda (x) (+ x 1)))      ;; Scheme

f = \x -> x + 1                      -- Haskell

val f: (Int => Int) = x => x + 1     // Scala

var f = function(x) { return x + 1 } // JavaScript

f = lambda x: x + 1                  # Python

As Eli Barzilay pointed out, these definition just bind the name f to a value, which happens to be a function. So in this respect, functions, numbers, strings, characters are all values that can be bound to names in the same way:

(define n 42)   ;; Scheme

n = 42          -- Haskell

val n: Int = 42 // Scala

var n = 42      // JavaScript

n = 42          # Python

In these languages you can also bind a function to a name using the more familiar (but equivalent) notation:

(define (f x) (+ x 1))         ;; Scheme

f x = x + 1                    -- Haskell

def f(x: Int): Int = x + 1     // Scala

function f(x) { return x + 1 } // JavaScript

def f(x): return x + 1         # Python

Some languages, e.g. C, only support the latter notation for defining (named) functions.


Some final observations regarding closures. Consider the expression x + y. This contains two free variables. If you bind x using the lambda notation you get:

\x -> x + y

This is not (yet) a function because it still contains a free variable y. You could make a function out of it by binding y as well:

\x -> \y -> x + y


\x y -> x + y

which is just the same as the + function.

But you can bind, say, y in another way (*):

incrementBy y = \x -> x + y

The result of applying function incrementBy to a number is a closure, i.e. a function / procedure whose body contains a free variable (e.g. y) that has been bound to a value from the environment in which the closure was defined.

So incrementBy 5 is the function (closure) that increments numbers by 5.

NOTE (*)

I am cheating a bit here:

incrementBy y = \x -> x + y

is equivalent to

incrementBy = \y -> \x -> x + y

so the binding mechanism is the same. Intuitively, I think of a closure as representing a chunk of a more complex lambda expression. When this representation is created, some of the bindings of the mother expression have already been set and the closure uses them later when it gets evaluated / invoked.

share|improve this answer

Clearly the last example of an anonymous function in javascript is not anonymous as its name is "add". I think of lambda expressions as the half-way point between very simple procedural programming and object orientation. Simply you can pass values or pointers -- lambda lets you pass procedure (code blocks) -- objects are the full encapsulation of values and methods. C allows you to pass function pointers. And of course java, javascript and c# handle completely anonymous (having no name) functions.

The add function above is lambda expressed but is certainly not anonymous.

share|improve this answer
You shouldn't be posting blind responses to other answers, it makes it kinda difficult to follow, and is bad form for this site. Please see the FAQ for a better description. – Jeff Langemeier May 7 '13 at 15:47

protected by gnat 7 mins ago

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.