34

A function that does nothing, takes no arguments and returns nothing is traditionally called a noop, or no-op. An example of a noop is below:

function noop(){}

http://en.wikipedia.org/wiki/NOP

So is there a name for a function which is meant only to return its arguments, and not do anything else? An example of this kind of function:

function(a){return a}
  • 1
  • 17
    To the close-voters: This is not an opinion based question, there's a definitive answer. – Daenyth Mar 6 '15 at 18:40
  • @Daenyth "It's also commonly known as a pass-through" -- probably not as definitive as it appears – gnat Mar 6 '15 at 19:14
  • 2
    @gnat pass-through is used to refer to methods in a subclass which invoke a parent method without changing the parameters or whatever the parent method returns. But the super method may return nothing or something entirely different. Returning the same thing that is passed in is not part of the definition of a pass-through. If it does, that is only because the specific parent method does that. In fact, OO class methods which do nothing but return the same value they were passed are extremely rare. – itsbruce Mar 6 '15 at 22:56
  • 4
    @Snowman That's not an informed statement. In any language which supports Higher Order Functions, which can return a function that may do any arbitrary thing to a given input, it is very useful to be able to return, in some circumstances, a function that does not change the input. It allows control flows to be created with functions rather than keywords. When folding over a list (or other structure) to create a compound function (as in the case of difference lists), the identity function is often the natural seed value. – itsbruce Mar 8 '15 at 23:48
65

It's called the identity function and is sometimes abbreviated as id in category theory and functional programming languages.

  • 4
    Though it should be noted that most programmers don't have the formal background or functional programming knowledge to know that. It's also commonly known as a pass-through. – Telastyn Mar 6 '15 at 18:41
  • 5
    I've done plenty of formal math, but for some reason I didn't remember the answer until I saw this and facepalmed. – Ixrec Mar 6 '15 at 20:36
  • 5
    @Telastyn And in Java 8 it's still called "identity". The name is easing out of its math/functional background and becoming the standard. – Izkata Mar 6 '15 at 20:45
  • 3
    Indeed. And the fact that it has a formal root, combined with the spreading awareness of the term, makes it more useful than some arbitrary, language-specific jargon. – itsbruce Mar 6 '15 at 22:42
  • 11
    @Telastyn"pass through" is almost always used in OO referring specifically to methods which invoke a parent's method without changing the parameters. That's both more specific and limited than the concept of the identity function, not least because the OO method will often return nothing rather than the argument - or something entirely different. So while a pass-through will return the same thing as the super method, it is not at all required to return the original argument. These are different things. – itsbruce Mar 6 '15 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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