Category: 

What Is an Anonymous Function?

Article Details
  • Written By: Alex Newth
  • Edited By: Angela B.
  • Last Modified Date: 02 September 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
In late 19th-century London, mail was delivered to residential addresses up to twelve times each day.   more...

September 28 ,  1924 :  Two US military planes complete the first flights around the world.  more...

An anonymous function is a function used in many programming languages but, unlike other functions, it can be used without having to be bound and it does not need a name. One way to use an anonymous function is to pass data from one section to another, especially if the data are short. Unlike other functions, which have to be declared and used in two separate steps, this function combines both in a single step. Instead of programming many different placeholders into a program that needs them, the programmer can use one of these functions as a substitute, reducing the amount of code and ensuring there are no spelling mistakes.

Many functions in programming need to be bound to a token or symbol called an identifier. This is bypassed when using an anonymous function, because no identifier is required for the function to run. The function does not need a name; it is simply declared and used at runtime. Most programmers will give the function a name to make it easier to manually identify, unless there are just a few anonymous functions.

Ad

One way to use an anonymous function is to make it pass information from one section to another. This can be done either by loading certain information into the function or by passing information along a channel until it reaches this function. The primary reason for doing this is that this function requires very little code to declare, and it functions as a suitable intermediary when passing information.

When a regular function is used in a programming language, it requires two steps. First, the function must be declared, and then there must be coding to run the declared function. An anonymous function is able to do both in one step, and it typically requires less coding. This is normally used for single-use functions; the anonymous function is not bound to anything, so it may cause an error with repeated functions.

With most programming functions, several placeholders are required to hold information. These placeholders must be precisely named or the program will not recognize them. Coding also is required for each placeholder and, for a large program, this may require many lines of code. Instead, an anonymous function can be used as a placeholder, and typically just one is needed instead of many. The lack of need for a name for this function means the program will instantly recognize it and will allow the function to hold the information.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

exception 'Exception' with message 'error writing captcha: Duplicate entry '2147483647' for key 'PRIMARY'' in /ssd/www/wisegeek/public_html/_core/classes/public/Captcha.php:44
Stack trace:
#0 /ssd/www/wisegeek/public_html/_core/controls/public/ControlDiscussionPostBox.php(324): Captcha->createCaptcha()
#1 /ssd/www/wisegeek/public_html/framework/classes/Control.php(104): ControlDiscussionPostBox->preRender(false)
#2 /ssd/www/wisegeek/public_html/framework/classes/Control.php(149): Control->render()
#3 /ssd/www/wisegeek/public_html/tpl/default-nocustom-lu/pages/public/article/article.htm(526): Control->__toString()
#4 /ssd/www/wisegeek/public_html/framework/classes/Control.php(300): require('/ssd/www/wisege...')
#5 /ssd/www/wisegeek/public_html/framework/classes/Control.php(309): Control->requireTpl('pages/public/ar...', Object(PageArticleCom), true)
#6 /ssd/www/wisegeek/public_html/framework/classes/Control.php(131): Control->renderTpl('pages/public/ar...', Object(PageArticleCom))
#7 /ssd/www/wisegeek/public_html/framework/classes/FormDataControl.php(87): Control->renderTemplate()
#8 /ssd/www/wisegeek/public_html/framework/classes/Control.php(109): FormDataControl->renderTemplate()
#9 /ssd/www/wisegeek/public_html/framework/classes/ScriptPage.php(50): Control->render(false)
#10 /ssd/www/wisegeek/public_html/framework/classes/Control.php(149): ScriptPage->render()
#11 /ssd/www/wisegeek/public_html/framework/classes/Page.php(97): Control->__toString()
#12 /ssd/www/wisegeek/public_html/_core/classes/public/PublicFrontController.php(443): Page->processRequest()
#13 /ssd/www/wisegeek/public_html/_core/classes/public/PublicFrontController.php(7): PublicFrontController->renderPage()
#14 /ssd/www/wisegeek/public_html/index.php(11): PublicFrontController::run()
#15 {main}