To add to ZekeDragon's excellent post, I'll show you why lambdas (anonymous functions) are useful. While he used Haskell as his example language, I'll use Clojure.
In Clojure, a lambda is created with the `fn` special form, so it's different from Haskell. You can create one like this:
(fn [args] body-of-function)
Now, we're using a functional programming language, so functions can be passed around like any other value. You can pass functions to other functions, and you can store functions in data structures and any number of awesome things. There is a shorter way to create lambdas in Clojure using the #() reader macro. You could use it to create a lambda like this:
#(do-something % %2 %3)
where %, %2, and %3 are the first three arguments to the function.
Let's use the map function as an example. The map function takes a function that takes a single argument and a sequence. It moves through the entire sequence one element at a time. It applies the function to each one of these elements, and places the return value in the place of the element in the sequence. Once it's finished, it returns the newly mapped sequence.
Now, what if you just wanted to square each number in a sequence? If you don't square anything else in your code, it wouldn't make sense to create a top-level square function when only one function is going to use it, would it? Instead, we can just use an anonymous function:
(map #(* % %) (range 100)) ; Here we're using the shorthand for creating lambdas. We're also using the range function. It just generates a sequence of numbers from 0 to n (n is 100 here)
;We could also do it like this:
(map (fn [n] (* n n)) (range 100)) ; This works the same way, but uses the fn special-form directly.
And that's it.
Another reason lambdas are useful are for times where you need to use the same short function multiple times within the scope of another function or something. If the function is only used within that scope and nowhere else in your code, it doesn't make sense to define it as a top-level function. Alternatively, you can just give that function a name, locally:
(let [square #(* % %)] ; We created a local square function using a lambda
[(map square (range 100)) (map square (range 100 301))])
Anonymous functions are very useful things, and lots of languages support them. Even Java will likely have some support for lambdas and closures in the upcoming JDK7.
I hope this post was helpful. <3