3 ms·
One of the biggest is that anonymous functions typically are less verbose in terms of syntax required. This is really dependent upon the language you're working
by latortuga 17y ago
One of the biggest is that anonymous functions typically are less verbose in terms of syntax required. This is really dependent upon the language you're working with but two pretty decent examples from languages I work with are Ruby and javascript. In javascript, an anonymous function is just a normal function with no name:
function(){ return "i'm anonymous"; }
function namer(){ return "i'm a named function"; }
In Ruby, blocks are a syntactic sugar for anonymous functions:
[1,2,3,4].each {|num| print i } # => 1234
[1,2,3,4].each do |num| print i; end # => 1234
In javascript a named function is very similar to an anonymous one - they can both be bound to variables, both be passed to other functions, both be returned from functions. I believe the biggest difference you'll find is that some languages enable you to write them with very little trouble (e.g. the ruby given above, simply brackets with an arg list) while some require more extensive declaration. Another good contrast of this is javascript, which doesn't even require that you give a function a formal parameter list, with (hypothetically) Java which will need both a return type and the types of each argument.
Also that stackoverflow link is pretty good :)