Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

How do I turn this definition of an anonymous function, An anonymous function is a function that is assigned to a variable. Anonymous functions are also used when you want to perform a short and straight forward task, into an analogy?

share|improve this question

closed as unclear what you're asking by gnat, Martijn Pieters, Giorgio, Bart van Ingen Schenau, MichaelT Apr 22 '14 at 15:37

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2  
First off, that's a pretty crummy definition of an anonymous function. –  jfriend00 Apr 22 '14 at 5:03
    
@jfriend00 can you give me a good explanation of an anonymous function? –  user3421197 Apr 22 '14 at 5:09
    
    

2 Answers 2

up vote 0 down vote accepted

Anonymous functions are simply functions with no name. An anonymous function assigned to an variable is a function that is created at run time and only accessible via the variable it's assigned to (read: scope of the variable). Many times a function name is not important but are assigned to a local variable of another function object. Here an anonymous function assigned to an local variable:

var showMessage = function (x){alert(x);};

In short anonymous functions assigned to local variables are basically the same as methods in other OOP languages. I think it's better to say nothing than statements about programming based on number of lines. Too many lines of code is not a problem, only a result that's most commonly caused by a bad structure and low efficiency.

share|improve this answer

What do you mean by analogy? Being a C++/C# programmer primarily, I would have to say that that definition sounds very familiar to a C++/C# delegates and lamda expressions, or maybe function pointers. The link below explains the concept in C++.

http://stackoverflow.com/questions/9568150/what-is-a-c-delegate

It sounds like the variable would hold an address to a function along with parameter types specified in a certain order to call on those functions.

Anonymous functions are to Javascript as delegates/lamda expressions/function pointers are to C#/C++?

share|improve this answer
    
Virtually everything in Javascript is a function. Even the variables are evaluated function values. You can't truly create an anonymous function, since the function/result is assigned to something else. –  Adam Zuckerman Apr 22 '14 at 5:35
    
@AdamZuckerman - the term "anonymous function" is used all the time to describe a particular construct in javascript. You can argue whether it is technically anonymous or not (it isn't callable by name outside of it's context so that make it anonymous to me), but that's what it is called in the javascript community. The wikipedia definition I linked above describes it pretty well. –  jfriend00 Apr 22 '14 at 5:43
    
@AdamZuckerman I do not program in java-script. I looked it up and the code seemed most familiar to C# delegates. –  Ruina Apr 22 '14 at 6:28
    
@Ruina - I'm not the person who asked the question - perhaps you meant to address your comment to the OP. –  jfriend00 Apr 22 '14 at 6:33
    
@jfriend00 my bad :X - Comment removed :] –  Ruina Apr 22 '14 at 6:38

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