CINXE.COM

Function object - Wikipedia

<!DOCTYPE html> <html class="client-nojs vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-sticky-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-feature-night-mode-enabled skin-theme-clientpref-day vector-toc-available" lang="en" dir="ltr"> <head> <meta charset="UTF-8"> <title>Function object - Wikipedia</title> <script>(function(){var className="client-js vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-sticky-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-feature-night-mode-enabled skin-theme-clientpref-day vector-toc-available";var cookie=document.cookie.match(/(?:^|; )enwikimwclientpreferences=([^;]+)/);if(cookie){cookie[1].split('%2C').forEach(function(pref){className=className.replace(new RegExp('(^| )'+pref.replace(/-clientpref-\w+$|[^\w-]+/g,'')+'-clientpref-\\w+( |$)'),'$1'+pref+'$2');});}document.documentElement.className=className;}());RLCONF={"wgBreakFrames":false,"wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy", "wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgRequestId":"e5d709a7-c6ac-4f20-9513-6c50a5ffd114","wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"Function_object","wgTitle":"Function object","wgCurRevisionId":1223836041,"wgRevisionId":1223836041,"wgArticleId":509999,"wgIsArticle":true,"wgIsRedirect":false,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Articles with short description","Short description is different from Wikidata","Articles needing additional references from February 2009","All articles needing additional references","Webarchive template wayback links","Articles with example C code","Articles with example C++ code","Articles with example D code","Articles with example Eiffel code","Articles with example Java code","Articles with example JavaScript code","Articles with example Julia code", "Articles with example Lisp (programming language) code","Articles with example Objective-C code","Articles with example Perl code","Articles with example PHP code","Articles with example Python (programming language) code","Articles with example Ruby code","Object (computer science)","Subroutines"],"wgPageViewLanguage":"en","wgPageContentLanguage":"en","wgPageContentModel":"wikitext","wgRelevantPageName":"Function_object","wgRelevantArticleId":509999,"wgIsProbablyEditable":true,"wgRelevantPageIsProbablyEditable":true,"wgRestrictionEdit":[],"wgRestrictionMove":[],"wgNoticeProject":"wikipedia","wgCiteReferencePreviewsActive":false,"wgFlaggedRevsParams":{"tags":{"status":{"levels":1}}},"wgMediaViewerOnClick":true,"wgMediaViewerEnabledByDefault":true,"wgPopupsFlags":0,"wgVisualEditor":{"pageLanguageCode":"en","pageLanguageDir":"ltr","pageVariantFallbacks":"en"},"wgMFDisplayWikibaseDescriptions":{"search":true,"watchlist":true,"tagline":false,"nearby":true}, "wgWMESchemaEditAttemptStepOversample":false,"wgWMEPageLength":40000,"wgRelatedArticlesCompat":[],"wgCentralAuthMobileDomain":false,"wgEditSubmitButtonLabelPublish":true,"wgULSPosition":"interlanguage","wgULSisCompactLinksEnabled":false,"wgVector2022LanguageInHeader":true,"wgULSisLanguageSelectorEmpty":false,"wgWikibaseItemId":"Q2072111","wgCheckUserClientHintsHeadersJsApi":["brands","architecture","bitness","fullVersionList","mobile","model","platform","platformVersion"],"GEHomepageSuggestedEditsEnableTopics":true,"wgGETopicsMatchModeEnabled":false,"wgGEStructuredTaskRejectionReasonTextInputEnabled":false,"wgGELevelingUpEnabledForUser":false};RLSTATE={"ext.globalCssJs.user.styles":"ready","site.styles":"ready","user.styles":"ready","ext.globalCssJs.user":"ready","user":"ready","user.options":"loading","ext.cite.styles":"ready","ext.pygments":"ready","skins.vector.search.codex.styles":"ready","skins.vector.styles":"ready","skins.vector.icons":"ready","ext.wikimediamessages.styles": "ready","ext.visualEditor.desktopArticleTarget.noscript":"ready","ext.uls.interlanguage":"ready","wikibase.client.init":"ready","ext.wikimediaBadges":"ready"};RLPAGEMODULES=["ext.cite.ux-enhancements","ext.pygments.view","site","mediawiki.page.ready","mediawiki.toc","skins.vector.js","ext.centralNotice.geoIP","ext.centralNotice.startUp","ext.gadget.ReferenceTooltips","ext.gadget.switcher","ext.urlShortener.toolbar","ext.centralauth.centralautologin","mmv.bootstrap","ext.popups","ext.visualEditor.desktopArticleTarget.init","ext.visualEditor.targetLoader","ext.echo.centralauth","ext.eventLogging","ext.wikimediaEvents","ext.navigationTiming","ext.uls.interface","ext.cx.eventlogging.campaigns","ext.cx.uls.quick.actions","wikibase.client.vector-2022","ext.checkUser.clientHints","ext.quicksurveys.init","ext.growthExperiments.SuggestedEditSession","wikibase.sidebar.tracking"];</script> <script>(RLQ=window.RLQ||[]).push(function(){mw.loader.impl(function(){return["user.options@12s5i",function($,jQuery,require,module){mw.user.tokens.set({"patrolToken":"+\\","watchToken":"+\\","csrfToken":"+\\"}); }];});});</script> <link rel="stylesheet" href="/w/load.php?lang=en&amp;modules=ext.cite.styles%7Cext.pygments%2CwikimediaBadges%7Cext.uls.interlanguage%7Cext.visualEditor.desktopArticleTarget.noscript%7Cext.wikimediamessages.styles%7Cskins.vector.icons%2Cstyles%7Cskins.vector.search.codex.styles%7Cwikibase.client.init&amp;only=styles&amp;skin=vector-2022"> <script async="" src="/w/load.php?lang=en&amp;modules=startup&amp;only=scripts&amp;raw=1&amp;skin=vector-2022"></script> <meta name="ResourceLoaderDynamicStyles" content=""> <link rel="stylesheet" href="/w/load.php?lang=en&amp;modules=site.styles&amp;only=styles&amp;skin=vector-2022"> <meta name="generator" content="MediaWiki 1.44.0-wmf.4"> <meta name="referrer" content="origin"> <meta name="referrer" content="origin-when-cross-origin"> <meta name="robots" content="max-image-preview:standard"> <meta name="format-detection" content="telephone=no"> <meta name="viewport" content="width=1120"> <meta property="og:title" content="Function object - Wikipedia"> <meta property="og:type" content="website"> <link rel="preconnect" href="//upload.wikimedia.org"> <link rel="alternate" media="only screen and (max-width: 640px)" href="//en.m.wikipedia.org/wiki/Function_object"> <link rel="alternate" type="application/x-wiki" title="Edit this page" href="/w/index.php?title=Function_object&amp;action=edit"> <link rel="apple-touch-icon" href="/static/apple-touch/wikipedia.png"> <link rel="icon" href="/static/favicon/wikipedia.ico"> <link rel="search" type="application/opensearchdescription+xml" href="/w/rest.php/v1/search" title="Wikipedia (en)"> <link rel="EditURI" type="application/rsd+xml" href="//en.wikipedia.org/w/api.php?action=rsd"> <link rel="canonical" href="https://en.wikipedia.org/wiki/Function_object"> <link rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en"> <link rel="alternate" type="application/atom+xml" title="Wikipedia Atom feed" href="/w/index.php?title=Special:RecentChanges&amp;feed=atom"> <link rel="dns-prefetch" href="//meta.wikimedia.org" /> <link rel="dns-prefetch" href="//login.wikimedia.org"> </head> <body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject mw-editable page-Function_object rootpage-Function_object skin-vector-2022 action-view"><a class="mw-jump-link" href="#bodyContent">Jump to content</a> <div class="vector-header-container"> <header class="vector-header mw-header"> <div class="vector-header-start"> <nav class="vector-main-menu-landmark" aria-label="Site"> <div id="vector-main-menu-dropdown" class="vector-dropdown vector-main-menu-dropdown vector-button-flush-left vector-button-flush-right" > <input type="checkbox" id="vector-main-menu-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-main-menu-dropdown" class="vector-dropdown-checkbox " aria-label="Main menu" > <label id="vector-main-menu-dropdown-label" for="vector-main-menu-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-menu mw-ui-icon-wikimedia-menu"></span> <span class="vector-dropdown-label-text">Main menu</span> </label> <div class="vector-dropdown-content"> <div id="vector-main-menu-unpinned-container" class="vector-unpinned-container"> <div id="vector-main-menu" class="vector-main-menu vector-pinnable-element"> <div class="vector-pinnable-header vector-main-menu-pinnable-header vector-pinnable-header-unpinned" data-feature-name="main-menu-pinned" data-pinnable-element-id="vector-main-menu" data-pinned-container-id="vector-main-menu-pinned-container" data-unpinned-container-id="vector-main-menu-unpinned-container" > <div class="vector-pinnable-header-label">Main menu</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-main-menu.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-main-menu.unpin">hide</button> </div> <div id="p-navigation" class="vector-menu mw-portlet mw-portlet-navigation" > <div class="vector-menu-heading"> Navigation </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="n-mainpage-description" class="mw-list-item"><a href="/wiki/Main_Page" title="Visit the main page [z]" accesskey="z"><span>Main page</span></a></li><li id="n-contents" class="mw-list-item"><a href="/wiki/Wikipedia:Contents" title="Guides to browsing Wikipedia"><span>Contents</span></a></li><li id="n-currentevents" class="mw-list-item"><a href="/wiki/Portal:Current_events" title="Articles related to current events"><span>Current events</span></a></li><li id="n-randompage" class="mw-list-item"><a href="/wiki/Special:Random" title="Visit a randomly selected article [x]" accesskey="x"><span>Random article</span></a></li><li id="n-aboutsite" class="mw-list-item"><a href="/wiki/Wikipedia:About" title="Learn about Wikipedia and how it works"><span>About Wikipedia</span></a></li><li id="n-contactpage" class="mw-list-item"><a href="//en.wikipedia.org/wiki/Wikipedia:Contact_us" title="How to contact Wikipedia"><span>Contact us</span></a></li> </ul> </div> </div> <div id="p-interaction" class="vector-menu mw-portlet mw-portlet-interaction" > <div class="vector-menu-heading"> Contribute </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="n-help" class="mw-list-item"><a href="/wiki/Help:Contents" title="Guidance on how to use and edit Wikipedia"><span>Help</span></a></li><li id="n-introduction" class="mw-list-item"><a href="/wiki/Help:Introduction" title="Learn how to edit Wikipedia"><span>Learn to edit</span></a></li><li id="n-portal" class="mw-list-item"><a href="/wiki/Wikipedia:Community_portal" title="The hub for editors"><span>Community portal</span></a></li><li id="n-recentchanges" class="mw-list-item"><a href="/wiki/Special:RecentChanges" title="A list of recent changes to Wikipedia [r]" accesskey="r"><span>Recent changes</span></a></li><li id="n-upload" class="mw-list-item"><a href="/wiki/Wikipedia:File_upload_wizard" title="Add images or other media for use on Wikipedia"><span>Upload file</span></a></li> </ul> </div> </div> </div> </div> </div> </div> </nav> <a href="/wiki/Main_Page" class="mw-logo"> <img class="mw-logo-icon" src="/static/images/icons/wikipedia.png" alt="" aria-hidden="true" height="50" width="50"> <span class="mw-logo-container skin-invert"> <img class="mw-logo-wordmark" alt="Wikipedia" src="/static/images/mobile/copyright/wikipedia-wordmark-en.svg" style="width: 7.5em; height: 1.125em;"> <img class="mw-logo-tagline" alt="The Free Encyclopedia" src="/static/images/mobile/copyright/wikipedia-tagline-en.svg" width="117" height="13" style="width: 7.3125em; height: 0.8125em;"> </span> </a> </div> <div class="vector-header-end"> <div id="p-search" role="search" class="vector-search-box-vue vector-search-box-collapses vector-search-box-show-thumbnail vector-search-box-auto-expand-width vector-search-box"> <a href="/wiki/Special:Search" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only search-toggle" title="Search Wikipedia [f]" accesskey="f"><span class="vector-icon mw-ui-icon-search mw-ui-icon-wikimedia-search"></span> <span>Search</span> </a> <div class="vector-typeahead-search-container"> <div class="cdx-typeahead-search cdx-typeahead-search--show-thumbnail cdx-typeahead-search--auto-expand-width"> <form action="/w/index.php" id="searchform" class="cdx-search-input cdx-search-input--has-end-button"> <div id="simpleSearch" class="cdx-search-input__input-wrapper" data-search-loc="header-moved"> <div class="cdx-text-input cdx-text-input--has-start-icon"> <input class="cdx-text-input__input" type="search" name="search" placeholder="Search Wikipedia" aria-label="Search Wikipedia" autocapitalize="sentences" title="Search Wikipedia [f]" accesskey="f" id="searchInput" > <span class="cdx-text-input__icon cdx-text-input__start-icon"></span> </div> <input type="hidden" name="title" value="Special:Search"> </div> <button class="cdx-button cdx-search-input__end-button">Search</button> </form> </div> </div> </div> <nav class="vector-user-links vector-user-links-wide" aria-label="Personal tools"> <div class="vector-user-links-main"> <div id="p-vector-user-menu-preferences" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <div id="p-vector-user-menu-userpage" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <nav class="vector-appearance-landmark" aria-label="Appearance"> <div id="vector-appearance-dropdown" class="vector-dropdown " title="Change the appearance of the page&#039;s font size, width, and color" > <input type="checkbox" id="vector-appearance-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-appearance-dropdown" class="vector-dropdown-checkbox " aria-label="Appearance" > <label id="vector-appearance-dropdown-label" for="vector-appearance-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-appearance mw-ui-icon-wikimedia-appearance"></span> <span class="vector-dropdown-label-text">Appearance</span> </label> <div class="vector-dropdown-content"> <div id="vector-appearance-unpinned-container" class="vector-unpinned-container"> </div> </div> </div> </nav> <div id="p-vector-user-menu-notifications" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <div id="p-vector-user-menu-overflow" class="vector-menu mw-portlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-sitesupport-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="https://donate.wikimedia.org/wiki/Special:FundraiserRedirector?utm_source=donate&amp;utm_medium=sidebar&amp;utm_campaign=C13_en.wikipedia.org&amp;uselang=en" class=""><span>Donate</span></a> </li> <li id="pt-createaccount-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="/w/index.php?title=Special:CreateAccount&amp;returnto=Function+object" title="You are encouraged to create an account and log in; however, it is not mandatory" class=""><span>Create account</span></a> </li> <li id="pt-login-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="/w/index.php?title=Special:UserLogin&amp;returnto=Function+object" title="You&#039;re encouraged to log in; however, it&#039;s not mandatory. [o]" accesskey="o" class=""><span>Log in</span></a> </li> </ul> </div> </div> </div> <div id="vector-user-links-dropdown" class="vector-dropdown vector-user-menu vector-button-flush-right vector-user-menu-logged-out" title="Log in and more options" > <input type="checkbox" id="vector-user-links-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-user-links-dropdown" class="vector-dropdown-checkbox " aria-label="Personal tools" > <label id="vector-user-links-dropdown-label" for="vector-user-links-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-ellipsis mw-ui-icon-wikimedia-ellipsis"></span> <span class="vector-dropdown-label-text">Personal tools</span> </label> <div class="vector-dropdown-content"> <div id="p-personal" class="vector-menu mw-portlet mw-portlet-personal user-links-collapsible-item" title="User menu" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-sitesupport" class="user-links-collapsible-item mw-list-item"><a href="https://donate.wikimedia.org/wiki/Special:FundraiserRedirector?utm_source=donate&amp;utm_medium=sidebar&amp;utm_campaign=C13_en.wikipedia.org&amp;uselang=en"><span>Donate</span></a></li><li id="pt-createaccount" class="user-links-collapsible-item mw-list-item"><a href="/w/index.php?title=Special:CreateAccount&amp;returnto=Function+object" title="You are encouraged to create an account and log in; however, it is not mandatory"><span class="vector-icon mw-ui-icon-userAdd mw-ui-icon-wikimedia-userAdd"></span> <span>Create account</span></a></li><li id="pt-login" class="user-links-collapsible-item mw-list-item"><a href="/w/index.php?title=Special:UserLogin&amp;returnto=Function+object" title="You&#039;re encouraged to log in; however, it&#039;s not mandatory. [o]" accesskey="o"><span class="vector-icon mw-ui-icon-logIn mw-ui-icon-wikimedia-logIn"></span> <span>Log in</span></a></li> </ul> </div> </div> <div id="p-user-menu-anon-editor" class="vector-menu mw-portlet mw-portlet-user-menu-anon-editor" > <div class="vector-menu-heading"> Pages for logged out editors <a href="/wiki/Help:Introduction" aria-label="Learn more about editing"><span>learn more</span></a> </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-anoncontribs" class="mw-list-item"><a href="/wiki/Special:MyContributions" title="A list of edits made from this IP address [y]" accesskey="y"><span>Contributions</span></a></li><li id="pt-anontalk" class="mw-list-item"><a href="/wiki/Special:MyTalk" title="Discussion about edits from this IP address [n]" accesskey="n"><span>Talk</span></a></li> </ul> </div> </div> </div> </div> </nav> </div> </header> </div> <div class="mw-page-container"> <div class="mw-page-container-inner"> <div class="vector-sitenotice-container"> <div id="siteNotice"><!-- CentralNotice --></div> </div> <div class="vector-column-start"> <div class="vector-main-menu-container"> <div id="mw-navigation"> <nav id="mw-panel" class="vector-main-menu-landmark" aria-label="Site"> <div id="vector-main-menu-pinned-container" class="vector-pinned-container"> </div> </nav> </div> </div> <div class="vector-sticky-pinned-container"> <nav id="mw-panel-toc" aria-label="Contents" data-event-name="ui.sidebar-toc" class="mw-table-of-contents-container vector-toc-landmark"> <div id="vector-toc-pinned-container" class="vector-pinned-container"> <div id="vector-toc" class="vector-toc vector-pinnable-element"> <div class="vector-pinnable-header vector-toc-pinnable-header vector-pinnable-header-pinned" data-feature-name="toc-pinned" data-pinnable-element-id="vector-toc" > <h2 class="vector-pinnable-header-label">Contents</h2> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-toc.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-toc.unpin">hide</button> </div> <ul class="vector-toc-contents" id="mw-panel-toc-list"> <li id="toc-mw-content-text" class="vector-toc-list-item vector-toc-level-1"> <a href="#" class="vector-toc-link"> <div class="vector-toc-text">(Top)</div> </a> </li> <li id="toc-Description" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#Description"> <div class="vector-toc-text"> <span class="vector-toc-numb">1</span> <span>Description</span> </div> </a> <ul id="toc-Description-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_C_and_C++" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_C_and_C++"> <div class="vector-toc-text"> <span class="vector-toc-numb">2</span> <span>In C and C++</span> </div> </a> <button aria-controls="toc-In_C_and_C++-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle In C and C++ subsection</span> </button> <ul id="toc-In_C_and_C++-sublist" class="vector-toc-list"> <li id="toc-Maintaining_state" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Maintaining_state"> <div class="vector-toc-text"> <span class="vector-toc-numb">2.1</span> <span>Maintaining state</span> </div> </a> <ul id="toc-Maintaining_state-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-In_C#" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_C#"> <div class="vector-toc-text"> <span class="vector-toc-numb">3</span> <span>In C#</span> </div> </a> <ul id="toc-In_C#-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_D" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_D"> <div class="vector-toc-text"> <span class="vector-toc-numb">4</span> <span>In D</span> </div> </a> <ul id="toc-In_D-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_Eiffel" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_Eiffel"> <div class="vector-toc-text"> <span class="vector-toc-numb">5</span> <span>In Eiffel</span> </div> </a> <ul id="toc-In_Eiffel-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_Java" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_Java"> <div class="vector-toc-text"> <span class="vector-toc-numb">6</span> <span>In Java</span> </div> </a> <ul id="toc-In_Java-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_JavaScript" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_JavaScript"> <div class="vector-toc-text"> <span class="vector-toc-numb">7</span> <span>In JavaScript</span> </div> </a> <ul id="toc-In_JavaScript-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_Julia" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_Julia"> <div class="vector-toc-text"> <span class="vector-toc-numb">8</span> <span>In Julia</span> </div> </a> <ul id="toc-In_Julia-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_Lisp_and_Scheme" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_Lisp_and_Scheme"> <div class="vector-toc-text"> <span class="vector-toc-numb">9</span> <span>In Lisp and Scheme</span> </div> </a> <ul id="toc-In_Lisp_and_Scheme-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_Objective-C" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_Objective-C"> <div class="vector-toc-text"> <span class="vector-toc-numb">10</span> <span>In Objective-C</span> </div> </a> <ul id="toc-In_Objective-C-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_Perl" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_Perl"> <div class="vector-toc-text"> <span class="vector-toc-numb">11</span> <span>In Perl</span> </div> </a> <ul id="toc-In_Perl-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_PHP" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_PHP"> <div class="vector-toc-text"> <span class="vector-toc-numb">12</span> <span>In PHP</span> </div> </a> <ul id="toc-In_PHP-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_PowerShell" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_PowerShell"> <div class="vector-toc-text"> <span class="vector-toc-numb">13</span> <span>In PowerShell</span> </div> </a> <ul id="toc-In_PowerShell-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_Python" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_Python"> <div class="vector-toc-text"> <span class="vector-toc-numb">14</span> <span>In Python</span> </div> </a> <ul id="toc-In_Python-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-In_Ruby" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#In_Ruby"> <div class="vector-toc-text"> <span class="vector-toc-numb">15</span> <span>In Ruby</span> </div> </a> <ul id="toc-In_Ruby-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Other_meanings" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#Other_meanings"> <div class="vector-toc-text"> <span class="vector-toc-numb">16</span> <span>Other meanings</span> </div> </a> <ul id="toc-Other_meanings-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-See_also" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#See_also"> <div class="vector-toc-text"> <span class="vector-toc-numb">17</span> <span>See also</span> </div> </a> <ul id="toc-See_also-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Notes" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#Notes"> <div class="vector-toc-text"> <span class="vector-toc-numb">18</span> <span>Notes</span> </div> </a> <ul id="toc-Notes-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-References" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#References"> <div class="vector-toc-text"> <span class="vector-toc-numb">19</span> <span>References</span> </div> </a> <ul id="toc-References-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Further_reading" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#Further_reading"> <div class="vector-toc-text"> <span class="vector-toc-numb">20</span> <span>Further reading</span> </div> </a> <ul id="toc-Further_reading-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-External_links" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#External_links"> <div class="vector-toc-text"> <span class="vector-toc-numb">21</span> <span>External links</span> </div> </a> <ul id="toc-External_links-sublist" class="vector-toc-list"> </ul> </li> </ul> </div> </div> </nav> </div> </div> <div class="mw-content-container"> <main id="content" class="mw-body"> <header class="mw-body-header vector-page-titlebar"> <nav aria-label="Contents" class="vector-toc-landmark"> <div id="vector-page-titlebar-toc" class="vector-dropdown vector-page-titlebar-toc vector-button-flush-left" > <input type="checkbox" id="vector-page-titlebar-toc-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-page-titlebar-toc" class="vector-dropdown-checkbox " aria-label="Toggle the table of contents" > <label id="vector-page-titlebar-toc-label" for="vector-page-titlebar-toc-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-listBullet mw-ui-icon-wikimedia-listBullet"></span> <span class="vector-dropdown-label-text">Toggle the table of contents</span> </label> <div class="vector-dropdown-content"> <div id="vector-page-titlebar-toc-unpinned-container" class="vector-unpinned-container"> </div> </div> </div> </nav> <h1 id="firstHeading" class="firstHeading mw-first-heading"><span class="mw-page-title-main">Function object</span></h1> <div id="p-lang-btn" class="vector-dropdown mw-portlet mw-portlet-lang" > <input type="checkbox" id="p-lang-btn-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-p-lang-btn" class="vector-dropdown-checkbox mw-interlanguage-selector" aria-label="Go to an article in another language. Available in 5 languages" > <label id="p-lang-btn-label" for="p-lang-btn-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--action-progressive mw-portlet-lang-heading-5" aria-hidden="true" ><span class="vector-icon mw-ui-icon-language-progressive mw-ui-icon-wikimedia-language-progressive"></span> <span class="vector-dropdown-label-text">5 languages</span> </label> <div class="vector-dropdown-content"> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li class="interlanguage-link interwiki-it mw-list-item"><a href="https://it.wikipedia.org/wiki/Funtore_(programmazione)" title="Funtore (programmazione) – Italian" lang="it" hreflang="it" data-title="Funtore (programmazione)" data-language-autonym="Italiano" data-language-local-name="Italian" class="interlanguage-link-target"><span>Italiano</span></a></li><li class="interlanguage-link interwiki-ja mw-list-item"><a href="https://ja.wikipedia.org/wiki/%E9%96%A2%E6%95%B0%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88" title="関数オブジェクト – Japanese" lang="ja" hreflang="ja" data-title="関数オブジェクト" data-language-autonym="日本語" data-language-local-name="Japanese" class="interlanguage-link-target"><span>日本語</span></a></li><li class="interlanguage-link interwiki-ru mw-list-item"><a href="https://ru.wikipedia.org/wiki/%D0%A4%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D0%BE%D0%BD%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%B9_%D0%BE%D0%B1%D1%8A%D0%B5%D0%BA%D1%82" title="Функциональный объект – Russian" lang="ru" hreflang="ru" data-title="Функциональный объект" data-language-autonym="Русский" data-language-local-name="Russian" class="interlanguage-link-target"><span>Русский</span></a></li><li class="interlanguage-link interwiki-uk mw-list-item"><a href="https://uk.wikipedia.org/wiki/%D0%A4%D1%83%D0%BD%D0%BA%D1%82%D0%BE%D1%80_(%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D1%83%D0%B2%D0%B0%D0%BD%D0%BD%D1%8F)" title="Функтор (програмування) – Ukrainian" lang="uk" hreflang="uk" data-title="Функтор (програмування)" data-language-autonym="Українська" data-language-local-name="Ukrainian" class="interlanguage-link-target"><span>Українська</span></a></li><li class="interlanguage-link interwiki-zh mw-list-item"><a href="https://zh.wikipedia.org/wiki/%E5%87%BD%E6%95%B0%E5%AF%B9%E8%B1%A1" title="函数对象 – Chinese" lang="zh" hreflang="zh" data-title="函数对象" data-language-autonym="中文" data-language-local-name="Chinese" class="interlanguage-link-target"><span>中文</span></a></li> </ul> <div class="after-portlet after-portlet-lang"><span class="wb-langlinks-edit wb-langlinks-link"><a href="https://www.wikidata.org/wiki/Special:EntityPage/Q2072111#sitelinks-wikipedia" title="Edit interlanguage links" class="wbc-editpage">Edit links</a></span></div> </div> </div> </div> </header> <div class="vector-page-toolbar"> <div class="vector-page-toolbar-container"> <div id="left-navigation"> <nav aria-label="Namespaces"> <div id="p-associated-pages" class="vector-menu vector-menu-tabs mw-portlet mw-portlet-associated-pages" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-nstab-main" class="selected vector-tab-noicon mw-list-item"><a href="/wiki/Function_object" title="View the content page [c]" accesskey="c"><span>Article</span></a></li><li id="ca-talk" class="vector-tab-noicon mw-list-item"><a href="/wiki/Talk:Function_object" rel="discussion" title="Discuss improvements to the content page [t]" accesskey="t"><span>Talk</span></a></li> </ul> </div> </div> <div id="vector-variants-dropdown" class="vector-dropdown emptyPortlet" > <input type="checkbox" id="vector-variants-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-variants-dropdown" class="vector-dropdown-checkbox " aria-label="Change language variant" > <label id="vector-variants-dropdown-label" for="vector-variants-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet" aria-hidden="true" ><span class="vector-dropdown-label-text">English</span> </label> <div class="vector-dropdown-content"> <div id="p-variants" class="vector-menu mw-portlet mw-portlet-variants emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> </div> </div> </nav> </div> <div id="right-navigation" class="vector-collapsible"> <nav aria-label="Views"> <div id="p-views" class="vector-menu vector-menu-tabs mw-portlet mw-portlet-views" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-view" class="selected vector-tab-noicon mw-list-item"><a href="/wiki/Function_object"><span>Read</span></a></li><li id="ca-edit" class="vector-tab-noicon mw-list-item"><a href="/w/index.php?title=Function_object&amp;action=edit" title="Edit this page [e]" accesskey="e"><span>Edit</span></a></li><li id="ca-history" class="vector-tab-noicon mw-list-item"><a href="/w/index.php?title=Function_object&amp;action=history" title="Past revisions of this page [h]" accesskey="h"><span>View history</span></a></li> </ul> </div> </div> </nav> <nav class="vector-page-tools-landmark" aria-label="Page tools"> <div id="vector-page-tools-dropdown" class="vector-dropdown vector-page-tools-dropdown" > <input type="checkbox" id="vector-page-tools-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-page-tools-dropdown" class="vector-dropdown-checkbox " aria-label="Tools" > <label id="vector-page-tools-dropdown-label" for="vector-page-tools-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet" aria-hidden="true" ><span class="vector-dropdown-label-text">Tools</span> </label> <div class="vector-dropdown-content"> <div id="vector-page-tools-unpinned-container" class="vector-unpinned-container"> <div id="vector-page-tools" class="vector-page-tools vector-pinnable-element"> <div class="vector-pinnable-header vector-page-tools-pinnable-header vector-pinnable-header-unpinned" data-feature-name="page-tools-pinned" data-pinnable-element-id="vector-page-tools" data-pinned-container-id="vector-page-tools-pinned-container" data-unpinned-container-id="vector-page-tools-unpinned-container" > <div class="vector-pinnable-header-label">Tools</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-page-tools.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-page-tools.unpin">hide</button> </div> <div id="p-cactions" class="vector-menu mw-portlet mw-portlet-cactions emptyPortlet vector-has-collapsible-items" title="More options" > <div class="vector-menu-heading"> Actions </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-more-view" class="selected vector-more-collapsible-item mw-list-item"><a href="/wiki/Function_object"><span>Read</span></a></li><li id="ca-more-edit" class="vector-more-collapsible-item mw-list-item"><a href="/w/index.php?title=Function_object&amp;action=edit" title="Edit this page [e]" accesskey="e"><span>Edit</span></a></li><li id="ca-more-history" class="vector-more-collapsible-item mw-list-item"><a href="/w/index.php?title=Function_object&amp;action=history"><span>View history</span></a></li> </ul> </div> </div> <div id="p-tb" class="vector-menu mw-portlet mw-portlet-tb" > <div class="vector-menu-heading"> General </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="t-whatlinkshere" class="mw-list-item"><a href="/wiki/Special:WhatLinksHere/Function_object" title="List of all English Wikipedia pages containing links to this page [j]" accesskey="j"><span>What links here</span></a></li><li id="t-recentchangeslinked" class="mw-list-item"><a href="/wiki/Special:RecentChangesLinked/Function_object" rel="nofollow" title="Recent changes in pages linked from this page [k]" accesskey="k"><span>Related changes</span></a></li><li id="t-upload" class="mw-list-item"><a href="/wiki/Wikipedia:File_Upload_Wizard" title="Upload files [u]" accesskey="u"><span>Upload file</span></a></li><li id="t-specialpages" class="mw-list-item"><a href="/wiki/Special:SpecialPages" title="A list of all special pages [q]" accesskey="q"><span>Special pages</span></a></li><li id="t-permalink" class="mw-list-item"><a href="/w/index.php?title=Function_object&amp;oldid=1223836041" title="Permanent link to this revision of this page"><span>Permanent link</span></a></li><li id="t-info" class="mw-list-item"><a href="/w/index.php?title=Function_object&amp;action=info" title="More information about this page"><span>Page information</span></a></li><li id="t-cite" class="mw-list-item"><a href="/w/index.php?title=Special:CiteThisPage&amp;page=Function_object&amp;id=1223836041&amp;wpFormIdentifier=titleform" title="Information on how to cite this page"><span>Cite this page</span></a></li><li id="t-urlshortener" class="mw-list-item"><a href="/w/index.php?title=Special:UrlShortener&amp;url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FFunction_object"><span>Get shortened URL</span></a></li><li id="t-urlshortener-qrcode" class="mw-list-item"><a href="/w/index.php?title=Special:QrCode&amp;url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FFunction_object"><span>Download QR code</span></a></li> </ul> </div> </div> <div id="p-coll-print_export" class="vector-menu mw-portlet mw-portlet-coll-print_export" > <div class="vector-menu-heading"> Print/export </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="coll-download-as-rl" class="mw-list-item"><a href="/w/index.php?title=Special:DownloadAsPdf&amp;page=Function_object&amp;action=show-download-screen" title="Download this page as a PDF file"><span>Download as PDF</span></a></li><li id="t-print" class="mw-list-item"><a href="/w/index.php?title=Function_object&amp;printable=yes" title="Printable version of this page [p]" accesskey="p"><span>Printable version</span></a></li> </ul> </div> </div> <div id="p-wikibase-otherprojects" class="vector-menu mw-portlet mw-portlet-wikibase-otherprojects" > <div class="vector-menu-heading"> In other projects </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="t-wikibase" class="wb-otherproject-link wb-otherproject-wikibase-dataitem mw-list-item"><a href="https://www.wikidata.org/wiki/Special:EntityPage/Q2072111" title="Structured data on this page hosted by Wikidata [g]" accesskey="g"><span>Wikidata item</span></a></li> </ul> </div> </div> </div> </div> </div> </div> </nav> </div> </div> </div> <div class="vector-column-end"> <div class="vector-sticky-pinned-container"> <nav class="vector-page-tools-landmark" aria-label="Page tools"> <div id="vector-page-tools-pinned-container" class="vector-pinned-container"> </div> </nav> <nav class="vector-appearance-landmark" aria-label="Appearance"> <div id="vector-appearance-pinned-container" class="vector-pinned-container"> <div id="vector-appearance" class="vector-appearance vector-pinnable-element"> <div class="vector-pinnable-header vector-appearance-pinnable-header vector-pinnable-header-pinned" data-feature-name="appearance-pinned" data-pinnable-element-id="vector-appearance" data-pinned-container-id="vector-appearance-pinned-container" data-unpinned-container-id="vector-appearance-unpinned-container" > <div class="vector-pinnable-header-label">Appearance</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-appearance.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-appearance.unpin">hide</button> </div> </div> </div> </nav> </div> </div> <div id="bodyContent" class="vector-body" aria-labelledby="firstHeading" data-mw-ve-target-container> <div class="vector-body-before-content"> <div class="mw-indicators"> </div> <div id="siteSub" class="noprint">From Wikipedia, the free encyclopedia</div> </div> <div id="contentSub"><div id="mw-content-subtitle"></div></div> <div id="mw-content-text" class="mw-body-content"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><div class="shortdescription nomobile noexcerpt noprint searchaux" style="display:none">Programming construct</div> <style data-mw-deduplicate="TemplateStyles:r1236090951">.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}</style><div role="note" class="hatnote navigation-not-searchable">This article is about the computer programming concept of function objects. For functors in mathematics, see <a href="/wiki/Functor" title="Functor">Functor</a>. For the related concept in functional programming, see <a href="/wiki/Functor_(functional_programming)" title="Functor (functional programming)">Functor (functional programming)</a>.</div> <style data-mw-deduplicate="TemplateStyles:r1251242444">.mw-parser-output .ambox{border:1px solid #a2a9b1;border-left:10px solid #36c;background-color:#fbfbfb;box-sizing:border-box}.mw-parser-output .ambox+link+.ambox,.mw-parser-output .ambox+link+style+.ambox,.mw-parser-output .ambox+link+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+style+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+link+.ambox{margin-top:-1px}html body.mediawiki .mw-parser-output .ambox.mbox-small-left{margin:4px 1em 4px 0;overflow:hidden;width:238px;border-collapse:collapse;font-size:88%;line-height:1.25em}.mw-parser-output .ambox-speedy{border-left:10px solid #b32424;background-color:#fee7e6}.mw-parser-output .ambox-delete{border-left:10px solid #b32424}.mw-parser-output .ambox-content{border-left:10px solid #f28500}.mw-parser-output .ambox-style{border-left:10px solid #fc3}.mw-parser-output .ambox-move{border-left:10px solid #9932cc}.mw-parser-output .ambox-protection{border-left:10px solid #a2a9b1}.mw-parser-output .ambox .mbox-text{border:none;padding:0.25em 0.5em;width:100%}.mw-parser-output .ambox .mbox-image{border:none;padding:2px 0 2px 0.5em;text-align:center}.mw-parser-output .ambox .mbox-imageright{border:none;padding:2px 0.5em 2px 0;text-align:center}.mw-parser-output .ambox .mbox-empty-cell{border:none;padding:0;width:1px}.mw-parser-output .ambox .mbox-image-div{width:52px}@media(min-width:720px){.mw-parser-output .ambox{margin:0 10%}}@media print{body.ns-0 .mw-parser-output .ambox{display:none!important}}</style><table class="box-More_citations_needed plainlinks metadata ambox ambox-content ambox-Refimprove" role="presentation"><tbody><tr><td class="mbox-image"><div class="mbox-image-div"><span typeof="mw:File"><a href="/wiki/File:Question_book-new.svg" class="mw-file-description"><img alt="" src="//upload.wikimedia.org/wikipedia/en/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png" decoding="async" width="50" height="39" class="mw-file-element" srcset="//upload.wikimedia.org/wikipedia/en/thumb/9/99/Question_book-new.svg/75px-Question_book-new.svg.png 1.5x, //upload.wikimedia.org/wikipedia/en/thumb/9/99/Question_book-new.svg/100px-Question_book-new.svg.png 2x" data-file-width="512" data-file-height="399" /></a></span></div></td><td class="mbox-text"><div class="mbox-text-span">This article <b>needs additional citations for <a href="/wiki/Wikipedia:Verifiability" title="Wikipedia:Verifiability">verification</a></b>.<span class="hide-when-compact"> Please help <a href="/wiki/Special:EditPage/Function_object" title="Special:EditPage/Function object">improve this article</a> by <a href="/wiki/Help:Referencing_for_beginners" title="Help:Referencing for beginners">adding citations to reliable sources</a>. Unsourced material may be challenged and removed.<br /><small><span class="plainlinks"><i>Find sources:</i>&#160;<a rel="nofollow" class="external text" href="https://www.google.com/search?as_eq=wikipedia&amp;q=%22Function+object%22">"Function object"</a>&#160;–&#160;<a rel="nofollow" class="external text" href="https://www.google.com/search?tbm=nws&amp;q=%22Function+object%22+-wikipedia&amp;tbs=ar:1">news</a>&#160;<b>·</b> <a rel="nofollow" class="external text" href="https://www.google.com/search?&amp;q=%22Function+object%22&amp;tbs=bkt:s&amp;tbm=bks">newspapers</a>&#160;<b>·</b> <a rel="nofollow" class="external text" href="https://www.google.com/search?tbs=bks:1&amp;q=%22Function+object%22+-wikipedia">books</a>&#160;<b>·</b> <a rel="nofollow" class="external text" href="https://scholar.google.com/scholar?q=%22Function+object%22">scholar</a>&#160;<b>·</b> <a rel="nofollow" class="external text" href="https://www.jstor.org/action/doBasicSearch?Query=%22Function+object%22&amp;acc=on&amp;wc=on">JSTOR</a></span></small></span> <span class="date-container"><i>(<span class="date">February 2009</span>)</i></span><span class="hide-when-compact"><i> (<small><a href="/wiki/Help:Maintenance_template_removal" title="Help:Maintenance template removal">Learn how and when to remove this message</a></small>)</i></span></div></td></tr></tbody></table> <p>In <a href="/wiki/Computer_programming" title="Computer programming">computer programming</a>, a <b>function object</b><sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">&#91;</span>a<span class="cite-bracket">&#93;</span></a></sup> is a construct allowing an <a href="/wiki/Object_(computer_science)" title="Object (computer science)">object</a> to be invoked or called as if it were an ordinary <a href="/wiki/Subroutine" class="mw-redirect" title="Subroutine">function</a>, usually with the same syntax (a function parameter that can also be a function). In some languages, particularly C++, function objects are often called <b>functors</b> (not related to <a href="/wiki/Functor_(functional_programming)" title="Functor (functional programming)">the functional programming concept</a>). </p> <meta property="mw:PageProp/toc" /> <div class="mw-heading mw-heading2"><h2 id="Description">Description</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=1" title="Edit section: Description"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>A typical use of a function object is in writing <a href="/wiki/Callback_(computer_science)" class="mw-redirect" title="Callback (computer science)">callback</a> functions. A callback in <a href="/wiki/Procedural_programming" title="Procedural programming">procedural languages</a>, such as <a href="/wiki/C_(programming_language)" title="C (programming language)">C</a>, may be performed by using <a href="/wiki/Function_pointer" title="Function pointer">function pointers</a>.<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">&#91;</span>2<span class="cite-bracket">&#93;</span></a></sup> However it can be difficult or awkward to pass a state into or out of the callback function. This restriction also inhibits more dynamic behavior of the function. A function object solves those problems since the function is really a <a href="/wiki/Facade_pattern" title="Facade pattern">façade</a> for a full object, carrying its own state. </p><p>Many modern (and some older) languages, e.g. <a href="/wiki/C%2B%2B" title="C++">C++</a>, <a href="/wiki/Eiffel_(programming_language)" title="Eiffel (programming language)">Eiffel</a>, <a href="/wiki/Groovy_(programming_language)" class="mw-redirect" title="Groovy (programming language)">Groovy</a>, <a href="/wiki/Lisp_(programming_language)" title="Lisp (programming language)">Lisp</a>, <a href="/wiki/Smalltalk" title="Smalltalk">Smalltalk</a>, <a href="/wiki/Perl" title="Perl">Perl</a>, <a href="/wiki/PHP" title="PHP">PHP</a>, <a href="/wiki/Python_(programming_language)" title="Python (programming language)">Python</a>, <a href="/wiki/Ruby_(programming_language)" title="Ruby (programming language)">Ruby</a>, <a href="/wiki/Scala_(programming_language)" title="Scala (programming language)">Scala</a>, and many others, support <a href="/wiki/First-class_function" title="First-class function">first-class function</a> objects and may even make significant use of them.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">&#91;</span>3<span class="cite-bracket">&#93;</span></a></sup> <a href="/wiki/Functional_programming" title="Functional programming">Functional programming</a> languages additionally support <a href="/wiki/Closure_(computer_science)" class="mw-redirect" title="Closure (computer science)">closures</a>, i.e. first-class functions that can 'close over' variables in their surrounding environment at creation time. During compilation, a transformation known as <a href="/wiki/Lambda_lifting" title="Lambda lifting">lambda lifting</a> converts the closures into function objects. </p> <div class="mw-heading mw-heading2"><h2 id="In_C_and_C++"><span id="In_C_and_C.2B.2B"></span>In C and C++</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=2" title="Edit section: In C and C++"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Consider the example of a sorting routine that uses a callback function to define an ordering relation between a pair of items. The following C/C++ program uses function pointers: </p> <div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;stdlib.h&gt;</span> <span class="cm">/* qsort() callback function, returns &lt; 0 if a &lt; b, &gt; 0 if a &gt; b, 0 if a == b */</span> <span class="kt">int</span><span class="w"> </span><span class="nf">compareInts</span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="kt">void</span><span class="o">*</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">void</span><span class="o">*</span><span class="w"> </span><span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">a</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">b</span><span class="w"> </span><span class="p">);</span> <span class="p">}</span> <span class="p">...</span> <span class="c1">// prototype of qsort is</span> <span class="c1">// void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *));</span> <span class="p">...</span> <span class="kt">int</span><span class="w"> </span><span class="n">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">items</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">};</span> <span class="w"> </span><span class="n">qsort</span><span class="p">(</span><span class="n">items</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">items</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">items</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">items</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span><span class="w"> </span><span class="n">compareInts</span><span class="p">);</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> <p>In C++, a function object may be used instead of an ordinary function by defining a class that <a href="/wiki/Operator_overloading" title="Operator overloading">overloads</a> the <a href="/wiki/Function_call_operator" class="mw-redirect" title="Function call operator">function call operator</a> by defining an <code>operator()</code> member function. In C++, this may appear as follows: </p> <div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// comparator predicate: returns true if a &lt; b, false otherwise</span> <span class="k">struct</span><span class="w"> </span><span class="nc">IntComparator</span> <span class="p">{</span> <span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="nf">operator</span><span class="p">()(</span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">&amp;</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">&amp;</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="k">const</span> <span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">b</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="p">};</span> <span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">items</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">};</span> <span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">sort</span><span class="p">(</span><span class="n">items</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span><span class="w"> </span><span class="n">items</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span><span class="w"> </span><span class="n">IntComparator</span><span class="p">());</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> <p>Notice that the syntax for providing the callback to the <code>std::sort()</code> function is identical, but an object is passed instead of a function pointer. When invoked, the callback function is executed just as any other member function, and therefore has full access to the other members (data or functions) of the object. Of course, this is just a trivial example. To understand what power a functor provides more than a regular function, consider the common use case of sorting objects by a particular field. In the following example, a functor is used to sort a simple employee database by each employee's ID number. </p> <div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="k">struct</span><span class="w"> </span><span class="nc">CompareBy</span> <span class="p">{</span> <span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="w"> </span><span class="n">SORT_FIELD</span><span class="p">;</span> <span class="w"> </span><span class="n">CompareBy</span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span><span class="w"> </span><span class="n">sort_field</span><span class="o">=</span><span class="s">&quot;name&quot;</span><span class="p">)</span> <span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">SORT_FIELD</span><span class="p">(</span><span class="n">sort_field</span><span class="p">)</span> <span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="cm">/* validate sort_field */</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span> <span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="k">operator</span><span class="p">()(</span><span class="k">const</span><span class="w"> </span><span class="n">Employee</span><span class="o">&amp;</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">Employee</span><span class="o">&amp;</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="k">const</span> <span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">SORT_FIELD</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s">&quot;name&quot;</span><span class="p">)</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">name</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">name</span><span class="p">;</span> <span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">SORT_FIELD</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s">&quot;age&quot;</span><span class="p">)</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">age</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">age</span><span class="p">;</span> <span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">SORT_FIELD</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s">&quot;idnum&quot;</span><span class="p">)</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">idnum</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">idnum</span><span class="p">;</span> <span class="w"> </span><span class="k">else</span> <span class="w"> </span><span class="cm">/* throw exception or something */</span> <span class="w"> </span><span class="p">}</span> <span class="p">};</span> <span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Employee</span><span class="o">&gt;</span><span class="w"> </span><span class="n">emps</span><span class="p">;</span> <span class="w"> </span> <span class="w"> </span><span class="cm">/* code to populate database */</span> <span class="w"> </span> <span class="w"> </span><span class="c1">// Sort the database by employee ID number</span> <span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">sort</span><span class="p">(</span><span class="n">emps</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span><span class="w"> </span><span class="n">emps</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span><span class="w"> </span><span class="n">CompareBy</span><span class="p">(</span><span class="s">&quot;idnum&quot;</span><span class="p">));</span> <span class="w"> </span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> <p>In <a href="/wiki/C%2B%2B11" title="C++11">C++11</a>, the lambda expression provides a more succinct way to do the same thing. </p> <div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Employee</span><span class="o">&gt;</span><span class="w"> </span><span class="n">emps</span><span class="p">;</span> <span class="w"> </span><span class="cm">/* code to populate database */</span> <span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="w"> </span><span class="n">sort_field</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;idnum&quot;</span><span class="p">;</span> <span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">sort</span><span class="p">(</span><span class="n">emps</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span><span class="w"> </span><span class="n">emps</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">sort_field</span><span class="p">](</span><span class="k">const</span><span class="w"> </span><span class="n">Employee</span><span class="o">&amp;</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">Employee</span><span class="o">&amp;</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* code to select and compare field */</span><span class="w"> </span><span class="p">});</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> <p><br /> It is possible to use function objects in situations other than as callback functions. In this case, the shortened term <i>functor</i> is normally <i>not</i> used about the function object. Continuing the example, </p> <div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="n">IntComparator</span><span class="w"> </span><span class="n">cpm</span><span class="p">;</span> <span class="kt">bool</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cpm</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">);</span> </pre></div> <p>In addition to class type functors, other kinds of function objects are also possible in C++. They can take advantage of C++'s member-pointer or <a href="/wiki/Generic_programming" title="Generic programming">template</a> facilities. The expressiveness of templates allows some <a href="/wiki/Functional_programming" title="Functional programming">functional programming</a> techniques to be used, such as defining function objects in terms of other function objects (like <a href="/wiki/Function_composition_(computer_science)" title="Function composition (computer science)">function composition</a>). Much of the C++ <a href="/wiki/Standard_Template_Library" title="Standard Template Library">Standard Template Library</a> (STL) makes heavy use of template-based function objects. </p><p>Another way to create a function object in C++ is to define a non-explicit conversion function to a function pointer type, a function <a href="/wiki/Reference_(C%2B%2B)" title="Reference (C++)">reference</a> type, or a reference to function pointer type. Assuming the conversion does not discard <a href="/wiki/Type_qualifier" title="Type qualifier">cv-qualifiers</a>, this allows an object of that type to be used as a function with the same <a href="/wiki/Function_signature" class="mw-redirect" title="Function signature">signature</a> as the type it is converted to. Modifying an earlier example to use this we obtain the following class, whose instances can be called like function pointers:<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">&#91;</span>4<span class="cite-bracket">&#93;</span></a></sup> </p> <div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// comparator predicate: returns true if a &lt; b, false otherwise</span> <span class="k">struct</span><span class="w"> </span><span class="nc">IntComparator</span> <span class="p">{</span> <span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="nf">compare</span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">&amp;</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">&amp;</span><span class="n">b</span><span class="p">)</span> <span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">b</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">using</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">decltype</span><span class="p">(</span><span class="n">compare</span><span class="p">);</span> <span class="w"> </span><span class="k">operator</span><span class="w"> </span><span class="n">T</span><span class="o">*</span><span class="p">()</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">compare</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="p">};</span> <span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">items</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">};</span> <span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">sort</span><span class="p">(</span><span class="n">items</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span><span class="w"> </span><span class="n">items</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span><span class="w"> </span><span class="n">IntComparator</span><span class="p">());</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Maintaining_state">Maintaining state</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=3" title="Edit section: Maintaining state"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Another advantage of function objects is their ability to maintain a state that affects <code>operator()</code> between calls. For example, the following code defines a <a href="/wiki/Generator_(computer_science)" class="mw-redirect" title="Generator (computer science)">generator</a> counting from 10 upwards and is invoked 11 times. </p> <div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;algorithm&gt;</span> <span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;iostream&gt;</span> <span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;iterator&gt;</span> <span class="k">class</span><span class="w"> </span><span class="nc">CountFrom</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">public</span><span class="o">:</span> <span class="w"> </span><span class="n">CountFrom</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">count_</span><span class="p">(</span><span class="n">count</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span> <span class="w"> </span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="k">operator</span><span class="p">()()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">count_</span><span class="o">++</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">private</span><span class="o">:</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count_</span><span class="p">;</span> <span class="p">};</span> <span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">state</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span> <span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">generate_n</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">ostream_iterator</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">),</span><span class="w"> </span><span class="mi">11</span><span class="p">,</span> <span class="w"> </span><span class="n">CountFrom</span><span class="p">(</span><span class="n">state</span><span class="p">));</span> <span class="p">}</span> </pre></div> <p>In C++14 or later, the example above could be rewritten as: </p> <div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;algorithm&gt;</span> <span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;iostream&gt;</span> <span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;iterator&gt;</span> <span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">generate_n</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">ostream_iterator</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">),</span><span class="w"> </span><span class="mi">11</span><span class="p">,</span> <span class="w"> </span><span class="p">[</span><span class="n">count</span><span class="o">=</span><span class="mi">10</span><span class="p">]()</span><span class="w"> </span><span class="k">mutable</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">count</span><span class="o">++</span><span class="p">;</span><span class="w"> </span><span class="p">});</span> <span class="p">}</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="In_C#"><span id="In_C.23"></span>In C#</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=4" title="Edit section: In C#"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In <a href="/wiki/C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a>, function objects are declared via <a href="/wiki/Delegate_(CLI)" title="Delegate (CLI)">delegates</a>. A delegate can be declared using a named method or a <a href="/wiki/Lambda_(programming)" class="mw-redirect" title="Lambda (programming)">lambda expression</a>. Here is an example using a named method. </p> <div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span></span><span class="k">using</span><span class="w"> </span><span class="nn">System</span><span class="p">;</span> <span class="k">using</span><span class="w"> </span><span class="nn">System.Collections.Generic</span><span class="p">;</span> <span class="k">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nc">ComparisonClass1</span> <span class="p">{</span> <span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">CompareFunction</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">y</span><span class="p">)</span> <span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">y</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="nf">Main</span><span class="p">()</span> <span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kt">var</span><span class="w"> </span><span class="n">items</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">List</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="w"> </span><span class="p">};</span> <span class="w"> </span><span class="n">Comparison</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">del</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">CompareFunction</span><span class="p">;</span> <span class="w"> </span><span class="n">items</span><span class="p">.</span><span class="n">Sort</span><span class="p">(</span><span class="n">del</span><span class="p">);</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> </pre></div> <p>Here is an example using a lambda expression. </p> <div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span></span><span class="k">using</span><span class="w"> </span><span class="nn">System</span><span class="p">;</span> <span class="k">using</span><span class="w"> </span><span class="nn">System.Collections.Generic</span><span class="p">;</span> <span class="k">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nc">ComparisonClass2</span> <span class="p">{</span> <span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="nf">Main</span><span class="p">()</span> <span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kt">var</span><span class="w"> </span><span class="n">items</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">List</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="w"> </span><span class="p">};</span> <span class="w"> </span><span class="n">items</span><span class="p">.</span><span class="n">Sort</span><span class="p">((</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">y</span><span class="p">);</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="In_D">In D</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=5" title="Edit section: In D"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p><a href="/wiki/D_(programming_language)" title="D (programming language)">D</a> provides several ways to declare function objects: Lisp/Python-style via <a href="/wiki/Closure_(computer_science)" class="mw-redirect" title="Closure (computer science)">closures</a> or C#-style via <a href="/wiki/Delegate_(CLI)" title="Delegate (CLI)">delegates</a>, respectively: </p> <div class="mw-highlight mw-highlight-lang-d mw-content-ltr" dir="ltr"><pre><span></span><span class="kt">bool</span><span class="w"> </span><span class="n">find</span><span class="p">(</span><span class="n">T</span><span class="p">)(</span><span class="n">T</span><span class="p">[]</span><span class="w"> </span><span class="n">haystack</span><span class="p">,</span><span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="k">delegate</span><span class="p">(</span><span class="n">T</span><span class="p">)</span><span class="w"> </span><span class="n">needle_test</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">foreach</span><span class="w"> </span><span class="p">(</span><span class="n">straw</span><span class="p">;</span><span class="w"> </span><span class="n">haystack</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">needle_test</span><span class="p">(</span><span class="n">straw</span><span class="p">))</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span><span class="p">;</span> <span class="p">}</span> <span class="kt">void</span><span class="w"> </span><span class="n">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kt">int</span><span class="p">[]</span><span class="w"> </span><span class="n">haystack</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="mi">345</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span><span class="p">,</span><span class="w"> </span><span class="mi">457</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">56</span><span class="p">,</span><span class="w"> </span><span class="mi">123</span><span class="p">,</span><span class="w"> </span><span class="mi">456</span><span class="p">];</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">needle</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">123</span><span class="p">;</span> <span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="n">needleTest</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="n">needle</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">find</span><span class="p">(</span><span class="n">haystack</span><span class="p">,</span><span class="w"> </span><span class="p">&amp;</span><span class="n">needleTest</span><span class="p">));</span> <span class="p">}</span> </pre></div> <p>The difference between a <a href="/wiki/Delegate_(CLI)" title="Delegate (CLI)">delegate</a> and a <a href="/wiki/Closure_(computer_science)" class="mw-redirect" title="Closure (computer science)">closure</a> in D is automatically and conservatively determined by the compiler. D also supports function literals, that allow a lambda-style definition: </p> <div class="mw-highlight mw-highlight-lang-d mw-content-ltr" dir="ltr"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="n">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kt">int</span><span class="p">[]</span><span class="w"> </span><span class="n">haystack</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="mi">345</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span><span class="p">,</span><span class="w"> </span><span class="mi">457</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">56</span><span class="p">,</span><span class="w"> </span><span class="mi">123</span><span class="p">,</span><span class="w"> </span><span class="mi">456</span><span class="p">];</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">needle</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">123</span><span class="p">;</span> <span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">find</span><span class="p">(</span><span class="n">haystack</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="n">needle</span><span class="p">;</span><span class="w"> </span><span class="p">}));</span> <span class="p">}</span> </pre></div> <p>To allow the compiler to inline the code (see above), function objects can also be specified C++-style via <a href="/wiki/Operator_overloading" title="Operator overloading">operator overloading</a>: </p> <div class="mw-highlight mw-highlight-lang-d mw-content-ltr" dir="ltr"><pre><span></span><span class="kt">bool</span><span class="w"> </span><span class="n">find</span><span class="p">(</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">F</span><span class="p">)(</span><span class="n">T</span><span class="p">[]</span><span class="w"> </span><span class="n">haystack</span><span class="p">,</span><span class="w"> </span><span class="n">F</span><span class="w"> </span><span class="n">needle_test</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">foreach</span><span class="w"> </span><span class="p">(</span><span class="n">straw</span><span class="p">;</span><span class="w"> </span><span class="n">haystack</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">needle_test</span><span class="p">(</span><span class="n">straw</span><span class="p">))</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span><span class="p">;</span> <span class="p">}</span> <span class="kt">void</span><span class="w"> </span><span class="n">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kt">int</span><span class="p">[]</span><span class="w"> </span><span class="n">haystack</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="mi">345</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span><span class="p">,</span><span class="w"> </span><span class="mi">457</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">56</span><span class="p">,</span><span class="w"> </span><span class="mi">123</span><span class="p">,</span><span class="w"> </span><span class="mi">456</span><span class="p">];</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">needle</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">123</span><span class="p">;</span> <span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="n">NeedleTest</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">needle</span><span class="p">;</span> <span class="w"> </span><span class="k">this</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">needle</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="n">opCall</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="n">needle</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">find</span><span class="p">(</span><span class="n">haystack</span><span class="p">,</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">NeedleTest</span><span class="p">(</span><span class="n">needle</span><span class="p">)));</span> <span class="p">}</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="In_Eiffel">In Eiffel</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=6" title="Edit section: In Eiffel"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In the <a href="/wiki/Eiffel_(programming_language)" title="Eiffel (programming language)">Eiffel</a> software development method and language, operations and objects are seen always as separate concepts. However, the <a href="/wiki/Eiffel_(programming_language)#Agents" title="Eiffel (programming language)">agent</a> mechanism facilitates the modeling of operations as runtime objects. Agents satisfy the range of application attributed to function objects, such as being passed as arguments in procedural calls or specified as callback routines. The design of the agent mechanism in Eiffel attempts to reflect the object-oriented nature of the method and language. An agent is an object that generally is a direct instance of one of the two library classes, which model the two types of routines in Eiffel: <code>PROCEDURE</code> and <code>FUNCTION</code>. These two classes descend from the more abstract <code>ROUTINE</code>. </p><p>Within software text, the language keyword <code>agent</code> allows agents to be constructed in a compact form. In the following example, the goal is to add the action of stepping the gauge forward to the list of actions to be executed in the event that a button is clicked. </p> <div class="mw-highlight mw-highlight-lang-eiffel mw-content-ltr" dir="ltr"><pre><span></span><span class="n">my_button</span><span class="p">.</span><span class="n">select_actions</span><span class="p">.</span><span class="n">extend</span><span class="w"> </span><span class="p">(</span><span class="kr">agent</span><span class="w"> </span><span class="n">my_gauge</span><span class="p">.</span><span class="n">step_forward</span><span class="p">)</span> </pre></div> <p>The routine <code>extend</code> referenced in the example above is a feature of a class in a graphical user interface (GUI) library to provide <a href="/wiki/Event-driven_programming" title="Event-driven programming">event-driven programming</a> capabilities. </p><p>In other library classes, agents are seen to be used for different purposes. In a library supporting data structures, for example, a class modeling linear structures effects <a href="/wiki/Universal_quantification" title="Universal quantification">universal quantification</a> with a function <code>for_all</code> of type <code>BOOLEAN</code> that accepts an agent, an instance of <code>FUNCTION</code>, as an argument. So, in the following example, <code>my_action</code> is executed only if all members of <code>my_list</code> contain the character '!': </p> <div class="mw-highlight mw-highlight-lang-eiffel mw-content-ltr" dir="ltr"><pre><span></span><span class="w"> </span><span class="n">my_list</span><span class="p">:</span><span class="w"> </span><span class="nc">LINKED_LIST</span><span class="w"> </span><span class="o">[</span><span class="nc">STRING</span><span class="o">]</span> <span class="w"> </span><span class="p">...</span> <span class="w"> </span><span class="kr">if</span><span class="w"> </span><span class="n">my_list</span><span class="p">.</span><span class="n">for_all</span><span class="w"> </span><span class="p">(</span><span class="kr">agent</span><span class="w"> </span><span class="p">{</span><span class="nc">STRING</span><span class="p">}.</span><span class="n">has</span><span class="w"> </span><span class="p">(</span><span class="sc">&#39;!&#39;</span><span class="p">))</span><span class="w"> </span><span class="kr">then</span> <span class="w"> </span><span class="n">my_action</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="p">...</span> </pre></div> <p>When agents are created, the arguments to the routines they model and even the target object to which they are applied can be either <i>closed</i> or left <i>open</i>. Closed arguments and targets are given values at agent creation time. The assignment of values for open arguments and targets is deferred until some point after the agent is created. The routine <code>for_all</code> expects as an argument an agent representing a function with one open argument or target that conforms to actual generic parameter for the structure (<code>STRING</code> in this example.) </p><p>When the target of an agent is left open, the class name of the expected target, enclosed in braces, is substituted for an object reference as shown in the text <code>agent {STRING}.has ('!')</code> in the example above. When an argument is left open, the question mark character ('?') is coded as a placeholder for the open argument. </p><p>The ability to close or leave open targets and arguments is intended to improve the flexibility of the agent mechanism. Consider a class that contains the following procedure to print a string on standard output after a new line: </p> <div class="mw-highlight mw-highlight-lang-eiffel mw-content-ltr" dir="ltr"><pre><span></span><span class="w"> </span><span class="n">print_on_new_line</span><span class="w"> </span><span class="p">(</span><span class="n">s</span><span class="p">:</span><span class="w"> </span><span class="nc">STRING</span><span class="p">)</span> <span class="w"> </span><span class="c1">-- Print `s&#39; preceded by a new line</span> <span class="w"> </span><span class="kr">do</span> <span class="w"> </span><span class="n">print</span><span class="w"> </span><span class="p">(</span><span class="s">&quot;%N&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">s</span><span class="p">)</span> <span class="w"> </span><span class="kr">end</span> </pre></div> <p>The following snippet, assumed to be in the same class, uses <code>print_on_new_line</code> to demonstrate the mixing of open arguments and open targets in agents used as arguments to the same routine. </p> <div class="mw-highlight mw-highlight-lang-eiffel mw-content-ltr" dir="ltr"><pre><span></span><span class="w"> </span><span class="n">my_list</span><span class="p">:</span><span class="w"> </span><span class="nc">LINKED_LIST</span><span class="w"> </span><span class="o">[</span><span class="nc">STRING</span><span class="o">]</span> <span class="w"> </span><span class="p">...</span> <span class="w"> </span><span class="n">my_list</span><span class="p">.</span><span class="n">do_all</span><span class="w"> </span><span class="p">(</span><span class="kr">agent</span><span class="w"> </span><span class="n">print_on_new_line</span><span class="w"> </span><span class="p">(</span><span class="o">?</span><span class="p">))</span> <span class="w"> </span><span class="n">my_list</span><span class="p">.</span><span class="n">do_all</span><span class="w"> </span><span class="p">(</span><span class="kr">agent</span><span class="w"> </span><span class="p">{</span><span class="nc">STRING</span><span class="p">}.</span><span class="n">to_lower</span><span class="p">)</span> <span class="w"> </span><span class="n">my_list</span><span class="p">.</span><span class="n">do_all</span><span class="w"> </span><span class="p">(</span><span class="kr">agent</span><span class="w"> </span><span class="n">print_on_new_line</span><span class="w"> </span><span class="p">(</span><span class="o">?</span><span class="p">))</span> <span class="w"> </span><span class="p">...</span> </pre></div> <p>This example uses the procedure <code>do_all</code> for linear structures, which executes the routine modeled by an agent for each item in the structure. </p><p>The sequence of three instructions prints the strings in <code>my_list</code>, converts the strings to lowercase, and then prints them again. </p><p>Procedure <code>do_all</code> iterates across the structure executing the routine substituting the current item for either the open argument (in the case of the agents based on <code>print_on_new_line</code>), or the open target (in the case of the agent based on <code>to_lower</code>). </p><p>Open and closed arguments and targets also allow the use of routines that call for more arguments than are required by closing all but the necessary number of arguments: </p> <div class="mw-highlight mw-highlight-lang-eiffel mw-content-ltr" dir="ltr"><pre><span></span><span class="n">my_list</span><span class="p">.</span><span class="n">do_all</span><span class="w"> </span><span class="p">(</span><span class="kr">agent</span><span class="w"> </span><span class="n">my_multi_arg_procedure</span><span class="w"> </span><span class="p">(</span><span class="n">closed_arg_1</span><span class="p">,</span><span class="w"> </span><span class="o">?</span><span class="p">,</span><span class="w"> </span><span class="n">closed_arg_2</span><span class="p">,</span><span class="w"> </span><span class="n">closed_arg_3</span><span class="p">)</span> </pre></div> <p>The Eiffel agent mechanism is detailed in the <a rel="nofollow" class="external text" href="http://www.ecma-international.org/publications/standards/Ecma-367.htm">Eiffel ISO/ECMA standard document</a>. </p> <div class="mw-heading mw-heading2"><h2 id="In_Java">In Java</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=7" title="Edit section: In Java"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p><a href="/wiki/Java_(programming_language)" title="Java (programming language)">Java</a> has no <a href="/wiki/First-class_function" title="First-class function">first-class functions</a>, so function objects are usually expressed by an interface with a single method (most commonly the <code>Callable</code> interface), typically with the implementation being an anonymous <a href="/wiki/Inner_class" title="Inner class">inner class</a>, or, starting in Java 8, a <a href="/wiki/Anonymous_function" title="Anonymous function">lambda</a>. </p><p>For an example from Java's standard library, <code>java.util.Collections.sort()</code> takes a <code>List</code> and a functor whose role is to compare objects in the List. Without first-class functions, the function is part of the Comparator interface. This could be used as follows. </p> <div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span></span><span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span><span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Arrays</span><span class="p">.</span><span class="na">asList</span><span class="p">(</span><span class="s">&quot;10&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;1&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;20&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;11&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;21&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;12&quot;</span><span class="p">);</span> <span class="w"> </span> <span class="n">Comparator</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span><span class="w"> </span><span class="n">numStringComparator</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Comparator</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">compare</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">str1</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">str2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">Integer</span><span class="p">.</span><span class="na">valueOf</span><span class="p">(</span><span class="n">str1</span><span class="p">).</span><span class="na">compareTo</span><span class="p">(</span><span class="n">Integer</span><span class="p">.</span><span class="na">valueOf</span><span class="p">(</span><span class="n">str2</span><span class="p">));</span> <span class="w"> </span><span class="p">}</span> <span class="p">};</span> <span class="n">Collections</span><span class="p">.</span><span class="na">sort</span><span class="p">(</span><span class="n">list</span><span class="p">,</span><span class="w"> </span><span class="n">numStringComparator</span><span class="p">);</span> </pre></div> <p>In Java 8+, this can be written as: </p> <div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span></span><span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span><span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Arrays</span><span class="p">.</span><span class="na">asList</span><span class="p">(</span><span class="s">&quot;10&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;1&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;20&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;11&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;21&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;12&quot;</span><span class="p">);</span> <span class="w"> </span> <span class="n">Comparator</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span><span class="w"> </span><span class="n">numStringComparator</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">str1</span><span class="p">,</span><span class="w"> </span><span class="n">str2</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">Integer</span><span class="p">.</span><span class="na">valueOf</span><span class="p">(</span><span class="n">str1</span><span class="p">).</span><span class="na">compareTo</span><span class="p">(</span><span class="n">Integer</span><span class="p">.</span><span class="na">valueOf</span><span class="p">(</span><span class="n">str2</span><span class="p">));</span> <span class="n">Collections</span><span class="p">.</span><span class="na">sort</span><span class="p">(</span><span class="n">list</span><span class="p">,</span><span class="w"> </span><span class="n">numStringComparator</span><span class="p">);</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="In_JavaScript">In JavaScript</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=8" title="Edit section: In JavaScript"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In <a href="/wiki/JavaScript" title="JavaScript">JavaScript</a>, functions are first class objects. JavaScript also supports closures. </p><p>Compare the following with the subsequent Python example. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">Accumulator</span><span class="p">(</span><span class="nx">start</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">current</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">start</span><span class="p">;</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="p">(</span><span class="nx">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">current</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="nx">x</span><span class="p">;</span> <span class="w"> </span><span class="p">};</span> <span class="p">}</span> </pre></div> <p>An example of this in use: </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Accumulator</span><span class="p">(</span><span class="mf">4</span><span class="p">);</span> <span class="kd">var</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">a</span><span class="p">(</span><span class="mf">5</span><span class="p">);</span><span class="w"> </span><span class="c1">// x has value 9</span> <span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">a</span><span class="p">(</span><span class="mf">2</span><span class="p">);</span><span class="w"> </span><span class="c1">// x has value 11</span> <span class="kd">var</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Accumulator</span><span class="p">(</span><span class="mf">42</span><span class="p">);</span> <span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">b</span><span class="p">(</span><span class="mf">7</span><span class="p">);</span><span class="w"> </span><span class="c1">// x has value 49 (current = 49 in closure b)</span> <span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">a</span><span class="p">(</span><span class="mf">7</span><span class="p">);</span><span class="w"> </span><span class="c1">// x has value 18 (current = 18 in closure a)</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="In_Julia">In Julia</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=9" title="Edit section: In Julia"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In <a href="/wiki/Julia_(programming_language)" title="Julia (programming language)">Julia</a>, methods are associated with types, so it is possible to make any arbitrary Julia object "callable" by adding methods to its type. (Such "callable" objects are sometimes called "functors.") </p><p>An example is this accumulator mutable struct (based on <a href="/wiki/Paul_Graham_(computer_programmer)" class="mw-redirect" title="Paul Graham (computer programmer)">Paul Graham's</a> study on programming language syntax and clarity):<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">&#91;</span>5<span class="cite-bracket">&#93;</span></a></sup> </p> <div class="mw-highlight mw-highlight-lang-julia-repl mw-content-ltr" dir="ltr"><pre><span></span><span class="gp">julia&gt;</span><span class="w"> </span><span class="k">mutable</span><span class="w"> </span><span class="k">struct</span> <span class="kt">Accumulator</span> <span class="w"> </span><span class="n">n</span><span class="o">::</span><span class="kt">Int</span> <span class="w"> </span><span class="k">end</span> <span class="gp">julia&gt;</span><span class="w"> </span><span class="k">function</span><span class="w"> </span><span class="p">(</span><span class="n">acc</span><span class="o">::</span><span class="kt">Accumulator</span><span class="p">)(</span><span class="n">n2</span><span class="p">)</span> <span class="w"> </span><span class="n">acc</span><span class="o">.</span><span class="n">n</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">n2</span> <span class="w"> </span><span class="k">end</span> <span class="gp">julia&gt;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Accumulator</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="go">Accumulator(4)</span> <span class="gp">julia&gt;</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="go">9</span> <span class="gp">julia&gt;</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="go">11</span> <span class="gp">julia&gt;</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Accumulator</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="go">Accumulator(42)</span> <span class="gp">julia&gt;</span><span class="w"> </span><span class="n">b</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="go">49</span> </pre></div> <p>Such an accumulator can also be implemented using closure: </p> <div class="mw-highlight mw-highlight-lang-julia-repl mw-content-ltr" dir="ltr"><pre><span></span><span class="gp">julia&gt;</span><span class="w"> </span><span class="k">function</span><span class="w"> </span><span class="n">Accumulator</span><span class="p">(</span><span class="n">n0</span><span class="p">)</span> <span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n0</span> <span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">n2</span><span class="p">)</span> <span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">n2</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="k">end</span> <span class="go">Accumulator (generic function with 1 method)</span> <span class="gp">julia&gt;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Accumulator</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="go">(::#1) (generic function with 1 method)</span> <span class="gp">julia&gt;</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="go">9</span> <span class="gp">julia&gt;</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="go">11</span> <span class="gp">julia&gt;</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Accumulator</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="go">(::#1) (generic function with 1 method)</span> <span class="gp">julia&gt;</span><span class="w"> </span><span class="n">b</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="go">49</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="In_Lisp_and_Scheme">In Lisp and Scheme</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=10" title="Edit section: In Lisp and Scheme"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In Lisp family languages such as <a href="/wiki/Common_Lisp" title="Common Lisp">Common Lisp</a>, <a href="/wiki/Scheme_(programming_language)" title="Scheme (programming language)">Scheme</a>, and others, functions are objects, just like strings, vectors, lists, and numbers. A closure-constructing operator creates a <i>function object</i> from a part of the program: the part of code given as an argument to the operator is part of the function, and so is the lexical environment: the bindings of the lexically visible variables are <i>captured</i> and stored in the function object, which is more commonly called a <a href="/wiki/Closure_(computer_science)" class="mw-redirect" title="Closure (computer science)">closure</a>. The captured bindings play the role of <i>member variables</i>, and the code part of the closure plays the role of the <i>anonymous member function</i>, just like operator () in C++. </p><p>The closure constructor has the syntax <code>(lambda (parameters ...) code ...)</code>. The <code>(parameters ...)</code> part allows an interface to be declared, so that the function takes the declared parameters. The <code>code ...</code> part consists of expressions that are evaluated when the functor is called. </p><p>Many uses of functors in languages like C++ are simply emulations of the missing closure constructor. Since the programmer cannot directly construct a closure, they must define a class that has all of the necessary state variables, and also a member function. Then, construct an instance of that class instead, ensuring that all the member variables are initialized through its constructor. The values are derived precisely from those local variables that ought to be captured directly by a closure. </p><p>A function-object using the class system in Common Lisp, no use of closures: </p> <div class="mw-highlight mw-highlight-lang-lisp mw-content-ltr" dir="ltr"><pre><span></span><span class="p">(</span><span class="nb">defclass</span><span class="w"> </span><span class="nv">counter</span><span class="w"> </span><span class="p">()</span> <span class="w"> </span><span class="p">((</span><span class="nv">value</span><span class="w"> </span><span class="ss">:initarg</span><span class="w"> </span><span class="ss">:value</span><span class="w"> </span><span class="ss">:accessor</span><span class="w"> </span><span class="nv">value-of</span><span class="p">)))</span> <span class="p">(</span><span class="nb">defmethod</span><span class="w"> </span><span class="nv">functor-call</span><span class="w"> </span><span class="p">((</span><span class="nv">c</span><span class="w"> </span><span class="nv">counter</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="nb">incf</span><span class="w"> </span><span class="p">(</span><span class="nv">value-of</span><span class="w"> </span><span class="nv">c</span><span class="p">)))</span> <span class="p">(</span><span class="nb">defun</span><span class="w"> </span><span class="nv">make-counter</span><span class="w"> </span><span class="p">(</span><span class="nv">initial-value</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nb">make-instance</span><span class="w"> </span><span class="ss">&#39;counter</span><span class="w"> </span><span class="ss">:value</span><span class="w"> </span><span class="nv">initial-value</span><span class="p">))</span> <span class="c1">;;; use the counter:</span> <span class="p">(</span><span class="nb">defvar</span><span class="w"> </span><span class="vg">*c*</span><span class="w"> </span><span class="p">(</span><span class="nv">make-counter</span><span class="w"> </span><span class="mi">10</span><span class="p">))</span> <span class="p">(</span><span class="nv">functor-call</span><span class="w"> </span><span class="vg">*c*</span><span class="p">)</span><span class="w"> </span><span class="nv">--&gt;</span><span class="w"> </span><span class="mi">11</span> <span class="p">(</span><span class="nv">functor-call</span><span class="w"> </span><span class="vg">*c*</span><span class="p">)</span><span class="w"> </span><span class="nv">--&gt;</span><span class="w"> </span><span class="mi">12</span> </pre></div> <p>Since there is no standard way to make funcallable objects in Common Lisp, we fake it by defining a generic function called FUNCTOR-CALL. This can be specialized for any class whatsoever. The standard FUNCALL function is not generic; it only takes function objects. </p><p>It is this FUNCTOR-CALL generic function that gives us function objects, which are <i>a computer programming construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax.</i> We have <i>almost</i> the same syntax: FUNCTOR-CALL instead of FUNCALL. Some Lisps provide <i>funcallable</i> objects as a simple extension. Making objects callable using the same syntax as functions is a fairly trivial business. Making a function call operator work with different kinds of <i>function things</i>, whether they be class objects or closures is no more complicated than making a + operator that works with different kinds of numbers, such as integers, reals or complex numbers. </p><p>Now, a counter implemented using a closure. This is much more brief and direct. The INITIAL-VALUE argument of the MAKE-COUNTER <a href="/wiki/Factory_function" class="mw-redirect" title="Factory function">factory function</a> is captured and used directly. It does not have to be copied into some auxiliary class object through a constructor. It <i>is</i> the counter. An auxiliary object is created, but that happens <i>behind the scenes</i>. </p> <div class="mw-highlight mw-highlight-lang-lisp mw-content-ltr" dir="ltr"><pre><span></span><span class="p">(</span><span class="nb">defun</span><span class="w"> </span><span class="nv">make-counter</span><span class="w"> </span><span class="p">(</span><span class="nv">value</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="k">lambda</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">(</span><span class="nb">incf</span><span class="w"> </span><span class="nv">value</span><span class="p">)))</span> <span class="c1">;;; use the counter</span> <span class="p">(</span><span class="nb">defvar</span><span class="w"> </span><span class="vg">*c*</span><span class="w"> </span><span class="p">(</span><span class="nv">make-counter</span><span class="w"> </span><span class="mi">10</span><span class="p">))</span> <span class="p">(</span><span class="nb">funcall</span><span class="w"> </span><span class="vg">*c*</span><span class="p">)</span><span class="w"> </span><span class="c1">; --&gt; 11</span> <span class="p">(</span><span class="nb">funcall</span><span class="w"> </span><span class="vg">*c*</span><span class="p">)</span><span class="w"> </span><span class="c1">; --&gt; 12</span> </pre></div> <p>Scheme makes closures even simpler, and Scheme code tends to use such higher-order programming somewhat more idiomatically. </p> <div class="mw-highlight mw-highlight-lang-scheme mw-content-ltr" dir="ltr"><pre><span></span><span class="p">(</span><span class="k">define</span><span class="w"> </span><span class="p">(</span><span class="nf">make-counter</span><span class="w"> </span><span class="nv">value</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="k">lambda</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">(</span><span class="k">set!</span><span class="w"> </span><span class="nv">value</span><span class="w"> </span><span class="p">(</span><span class="nb">+</span><span class="w"> </span><span class="nv">value</span><span class="w"> </span><span class="mi">1</span><span class="p">))</span><span class="w"> </span><span class="nv">value</span><span class="p">))</span> <span class="c1">;;; use the counter</span> <span class="p">(</span><span class="k">define</span><span class="w"> </span><span class="nv">c</span><span class="w"> </span><span class="p">(</span><span class="nf">make-counter</span><span class="w"> </span><span class="mi">10</span><span class="p">))</span> <span class="p">(</span><span class="nf">c</span><span class="p">)</span><span class="w"> </span><span class="c1">; --&gt; 11</span> <span class="p">(</span><span class="nf">c</span><span class="p">)</span><span class="w"> </span><span class="c1">; --&gt; 12</span> </pre></div> <p>More than one closure can be created in the same lexical environment. A vector of closures, each implementing a specific kind of operation, can quite faithfully emulate an object that has a set of virtual operations. That type of <a href="/wiki/Single_dispatch" class="mw-redirect" title="Single dispatch">single dispatch</a> object-oriented programming can be done fully with closures. </p><p>Thus there exists a kind of tunnel being dug from both sides of the proverbial mountain. Programmers in OOP languages discover function objects by restricting objects to have one <i>main</i> function to <i>do</i> that object's functional purpose, and even eliminate its name so that it looks like the object is being called! While programmers who use closures are not surprised that an object is called like a function, they discover that multiple closures sharing the same environment can provide a complete set of abstract operations like a virtual table for <a href="/wiki/Single_dispatch" class="mw-redirect" title="Single dispatch">single dispatch</a> type OOP. </p> <div class="mw-heading mw-heading2"><h2 id="In_Objective-C">In Objective-C</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=11" title="Edit section: In Objective-C"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In <a href="/wiki/Objective-C" title="Objective-C">Objective-C</a>, a function object can be created from the <code>NSInvocation</code> class. Construction of a function object requires a method signature, the target object, and the target selector. Here is an example for creating an invocation to the current object's <code>myMethod</code>: </p> <div class="mw-highlight mw-highlight-lang-objc mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// Construct a function object</span> <span class="kt">SEL</span><span class="w"> </span><span class="n">sel</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">@selector</span><span class="p">(</span><span class="n">myMethod</span><span class="p">);</span> <span class="bp">NSInvocation</span><span class="o">*</span><span class="w"> </span><span class="n">inv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="bp">NSInvocation</span><span class="w"> </span><span class="n">invocationWithMethodSignature</span><span class="o">:</span> <span class="w"> </span><span class="p">[</span><span class="nb">self</span><span class="w"> </span><span class="n">methodSignatureForSelector</span><span class="o">:</span><span class="n">sel</span><span class="p">]];</span> <span class="p">[</span><span class="n">inv</span><span class="w"> </span><span class="n">setTarget</span><span class="o">:</span><span class="nb">self</span><span class="p">];</span> <span class="p">[</span><span class="n">inv</span><span class="w"> </span><span class="n">setSelector</span><span class="o">:</span><span class="n">sel</span><span class="p">];</span> <span class="c1">// Do the actual invocation</span> <span class="p">[</span><span class="n">inv</span><span class="w"> </span><span class="n">invoke</span><span class="p">];</span> </pre></div> <p>An advantage of <code>NSInvocation</code> is that the target object can be modified after creation. A single <code>NSInvocation</code> can be created and then called for each of any number of targets, for instance from an observable object. An <code>NSInvocation</code> can be created from only a protocol, but it is not straightforward. See <a rel="nofollow" class="external text" href="http://www.a-coding.com/2010/10/making-nsinvocations.html">here</a>. </p> <div class="mw-heading mw-heading2"><h2 id="In_Perl">In Perl</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=12" title="Edit section: In Perl"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In <a href="/wiki/Perl" title="Perl">Perl</a>, a function object can be created either from a class's constructor returning a function closed over the object's instance data, blessed into the class: </p> <div class="mw-highlight mw-highlight-lang-perl mw-content-ltr" dir="ltr"><pre><span></span><span class="k">package</span><span class="w"> </span><span class="nn">Acc1</span><span class="p">;</span> <span class="k">sub</span><span class="w"> </span><span class="nf">new</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$class</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">shift</span><span class="p">;</span> <span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$arg</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">shift</span><span class="p">;</span> <span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$obj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">sub</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">shift</span><span class="p">;</span> <span class="w"> </span><span class="nv">$arg</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="nv">$num</span><span class="p">;</span> <span class="w"> </span><span class="p">};</span> <span class="w"> </span><span class="nb">bless</span><span class="w"> </span><span class="nv">$obj</span><span class="p">,</span><span class="w"> </span><span class="nv">$class</span><span class="p">;</span> <span class="p">}</span> <span class="mi">1</span><span class="p">;</span> </pre></div> <p>or by overloading the <code>&amp;{}</code> operator so that the object can be used as a function: </p> <div class="mw-highlight mw-highlight-lang-perl mw-content-ltr" dir="ltr"><pre><span></span><span class="k">package</span><span class="w"> </span><span class="nn">Acc2</span><span class="p">;</span> <span class="k">use</span><span class="w"> </span><span class="nn">overload</span> <span class="w"> </span><span class="s">&#39;&amp;{}&#39;</span><span class="w"> </span><span class="o">=&gt;</span> <span class="w"> </span><span class="k">sub</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$self</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">shift</span><span class="p">;</span> <span class="w"> </span><span class="k">sub</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">shift</span><span class="p">;</span> <span class="w"> </span><span class="nv">$self</span><span class="o">-&gt;</span><span class="p">{</span><span class="n">arg</span><span class="p">}</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="nv">$num</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">};</span> <span class="k">sub</span><span class="w"> </span><span class="nf">new</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$class</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">shift</span><span class="p">;</span> <span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$arg</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">shift</span><span class="p">;</span> <span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$obj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">arg</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="nv">$arg</span><span class="w"> </span><span class="p">};</span> <span class="w"> </span><span class="nb">bless</span><span class="w"> </span><span class="nv">$obj</span><span class="p">,</span><span class="w"> </span><span class="nv">$class</span><span class="p">;</span> <span class="p">}</span> <span class="mi">1</span><span class="p">;</span> </pre></div> <p>In both cases the function object can be used either using the dereferencing arrow syntax <i>$ref-&gt;(@arguments)</i>: </p> <div class="mw-highlight mw-highlight-lang-perl mw-content-ltr" dir="ltr"><pre><span></span><span class="k">use</span><span class="w"> </span><span class="nn">Acc1</span><span class="p">;</span> <span class="k">my</span><span class="w"> </span><span class="nv">$a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nn">Acc1</span><span class="o">-&gt;</span><span class="k">new</span><span class="p">(</span><span class="mi">42</span><span class="p">);</span> <span class="k">print</span><span class="w"> </span><span class="nv">$a</span><span class="o">-&gt;</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span><span class="w"> </span><span class="s">&quot;\n&quot;</span><span class="p">;</span><span class="w"> </span><span class="c1"># prints 52</span> <span class="k">print</span><span class="w"> </span><span class="nv">$a</span><span class="o">-&gt;</span><span class="p">(</span><span class="mi">8</span><span class="p">),</span><span class="w"> </span><span class="s">&quot;\n&quot;</span><span class="p">;</span><span class="w"> </span><span class="c1"># prints 60</span> </pre></div> <p>or using the coderef dereferencing syntax <i>&amp;$ref(@arguments)</i>: </p> <div class="mw-highlight mw-highlight-lang-perl mw-content-ltr" dir="ltr"><pre><span></span><span class="k">use</span><span class="w"> </span><span class="nn">Acc2</span><span class="p">;</span> <span class="k">my</span><span class="w"> </span><span class="nv">$a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nn">Acc2</span><span class="o">-&gt;</span><span class="k">new</span><span class="p">(</span><span class="mi">12</span><span class="p">);</span> <span class="k">print</span><span class="w"> </span><span class="o">&amp;</span><span class="nv">$a</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span><span class="w"> </span><span class="s">&quot;\n&quot;</span><span class="p">;</span><span class="w"> </span><span class="c1"># prints 22</span> <span class="k">print</span><span class="w"> </span><span class="o">&amp;</span><span class="nv">$a</span><span class="p">(</span><span class="mi">8</span><span class="p">),</span><span class="w"> </span><span class="s">&quot;\n&quot;</span><span class="p">;</span><span class="w"> </span><span class="c1"># prints 30</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="In_PHP">In PHP</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=13" title="Edit section: In PHP"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p><a href="/wiki/PHP" title="PHP">PHP</a> 5.3+ has <a href="/wiki/First-class_function" title="First-class function">first-class functions</a> that can be used e.g. as parameter to the usort() function: </p> <div class="mw-highlight mw-highlight-lang-php mw-content-ltr" dir="ltr"><pre><span></span><span class="nv">$a</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">);</span> <span class="nb">usort</span><span class="p">(</span><span class="nv">$a</span><span class="p">,</span> <span class="k">function</span> <span class="p">(</span><span class="nv">$x</span><span class="p">,</span> <span class="nv">$y</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nv">$x</span> <span class="o">-</span> <span class="nv">$y</span><span class="p">;</span> <span class="p">});</span> </pre></div> <p><a href="/wiki/PHP" title="PHP">PHP</a> 5.3+, supports also lambda functions and closures. </p> <div class="mw-highlight mw-highlight-lang-php mw-content-ltr" dir="ltr"><pre><span></span><span class="k">function</span> <span class="nf">Accumulator</span><span class="p">(</span><span class="nv">$start</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$current</span> <span class="o">=</span> <span class="nv">$start</span><span class="p">;</span> <span class="k">return</span> <span class="k">function</span><span class="p">(</span><span class="nv">$x</span><span class="p">)</span> <span class="k">use</span><span class="p">(</span><span class="o">&amp;</span><span class="nv">$current</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nv">$current</span> <span class="o">+=</span> <span class="nv">$x</span><span class="p">;</span> <span class="p">};</span> <span class="p">}</span> </pre></div> <p>An example of this in use: </p> <div class="mw-highlight mw-highlight-lang-php mw-content-ltr" dir="ltr"><pre><span></span><span class="nv">$a</span> <span class="o">=</span> <span class="nx">Accumulator</span><span class="p">(</span><span class="mi">4</span><span class="p">);</span> <span class="nv">$x</span> <span class="o">=</span> <span class="nv">$a</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span> <span class="k">echo</span> <span class="s2">&quot;x = </span><span class="si">$x</span><span class="s2">&lt;br/&gt;&quot;</span><span class="p">;</span> <span class="c1">// x = 9</span> <span class="nv">$x</span> <span class="o">=</span> <span class="nv">$a</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span> <span class="k">echo</span> <span class="s2">&quot;x = </span><span class="si">$x</span><span class="s2">&lt;br/&gt;&quot;</span><span class="p">;</span> <span class="c1">// x = 11</span> </pre></div> <p>It is also possible in PHP 5.3+ to make objects invokable by adding a magic __invoke() method to their class:<sup id="cite_ref-phpinvoke_7-0" class="reference"><a href="#cite_note-phpinvoke-7"><span class="cite-bracket">&#91;</span>6<span class="cite-bracket">&#93;</span></a></sup> </p> <div class="mw-highlight mw-highlight-lang-php mw-content-ltr" dir="ltr"><pre><span></span><span class="k">class</span> <span class="nc">Minus</span> <span class="p">{</span> <span class="k">public</span> <span class="k">function</span> <span class="fm">__invoke</span><span class="p">(</span><span class="nv">$x</span><span class="p">,</span> <span class="nv">$y</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nv">$x</span> <span class="o">-</span> <span class="nv">$y</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="nv">$a</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">);</span> <span class="nb">usort</span><span class="p">(</span><span class="nv">$a</span><span class="p">,</span> <span class="k">new</span> <span class="nx">Minus</span><span class="p">());</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="In_PowerShell">In PowerShell</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=14" title="Edit section: In PowerShell"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In the <a href="/wiki/Windows_PowerShell" class="mw-redirect" title="Windows PowerShell">Windows PowerShell</a> language, a script block is a collection of statements or expressions that can be used as a single unit. A script block can accept arguments and return values. A script block is an instance of a Microsoft <a href="/wiki/.NET_Framework" title=".NET Framework">.NET Framework</a> type System.Management.Automation.ScriptBlock. </p> <div class="mw-highlight mw-highlight-lang-powershell mw-content-ltr" dir="ltr"><pre><span></span><span class="k">Function</span> <span class="nb">Get-Accumulator</span><span class="p">(</span><span class="nv">$x</span><span class="p">)</span> <span class="p">{</span> <span class="p">{</span> <span class="k">param</span><span class="p">(</span><span class="nv">$y</span><span class="p">)</span> <span class="k">return</span> <span class="nv">$x</span> <span class="p">+=</span> <span class="nv">$y</span> <span class="p">}.</span><span class="n">GetNewClosure</span><span class="p">()</span> <span class="p">}</span> </pre></div> <div class="mw-highlight mw-highlight-lang-ps1con mw-content-ltr" dir="ltr"><pre><span></span><span class="gp">PS C:\&gt; </span><span class="nv">$a</span> <span class="p">=</span> <span class="nb">Get-Accumulator</span> <span class="n">4</span> <span class="gp">PS C:\&gt; </span><span class="p">&amp;</span> <span class="nv">$a</span> <span class="n">5</span> <span class="go">9</span> <span class="gp">PS C:\&gt; </span><span class="p">&amp;</span> <span class="nv">$a</span> <span class="n">2</span> <span class="go">11</span> <span class="gp">PS C:\&gt; </span><span class="nv">$b</span> <span class="p">=</span> <span class="nb">Get-Accumulator</span> <span class="n">32</span> <span class="gp">PS C:\&gt; </span><span class="p">&amp;</span> <span class="nv">$b</span> <span class="n">10</span> <span class="go">42</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="In_Python">In Python</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=15" title="Edit section: In Python"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In <a href="/wiki/Python_(programming_language)" title="Python (programming language)">Python</a>, functions are first-class objects, just like strings, numbers, lists etc. This feature eliminates the need to write a function object in many cases. Any object with a <code>__call__()</code> method can be called using function-call syntax. </p><p>An example is this accumulator class (based on <a href="/wiki/Paul_Graham_(computer_programmer)" class="mw-redirect" title="Paul Graham (computer programmer)">Paul Graham's</a> study on programming language syntax and clarity):<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">&#91;</span>7<span class="cite-bracket">&#93;</span></a></sup> </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="k">class</span> <span class="nc">Accumulator</span><span class="p">:</span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span> <span class="o">=</span> <span class="n">n</span> <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span> <span class="o">+=</span> <span class="n">x</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span> </pre></div> <p>An example of this in use (using the interactive interpreter): </p> <div class="mw-highlight mw-highlight-lang-pycon mw-content-ltr" dir="ltr"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Accumulator</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="go">9</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="go">11</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">Accumulator</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="go">49</span> </pre></div> <p>Since functions are objects, they can also be defined locally, given attributes, and returned by other functions, <sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">&#91;</span>8<span class="cite-bracket">&#93;</span></a></sup> as demonstrated in the following example: </p> <div class="mw-highlight mw-highlight-lang-python3 mw-content-ltr" dir="ltr"><pre><span></span><span class="k">def</span> <span class="nf">Accumulator</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="k">def</span> <span class="nf">inc</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">nonlocal</span> <span class="n">n</span> <span class="n">n</span> <span class="o">+=</span> <span class="n">x</span> <span class="k">return</span> <span class="n">n</span> <span class="k">return</span> <span class="n">inc</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="In_Ruby">In Ruby</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=16" title="Edit section: In Ruby"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In <a href="/wiki/Ruby_(programming_language)" title="Ruby (programming language)">Ruby</a>, several objects can be considered function objects, in particular Method and Proc objects. Ruby also has two kinds of objects that can be thought of as semi-function objects: UnboundMethod and block. UnboundMethods must first be bound to an object (thus becoming a Method) before they can be used as a function object. Blocks can be called like function objects, but to be used in any other capacity as an object (e.g. passed as an argument) they must first be converted to a Proc. More recently, symbols (accessed via the literal unary indicator <code>:</code>) can also be converted to <code>Proc</code>s. Using Ruby's unary <code>&amp;</code> operator&#8212;equivalent to calling <code>to_proc</code> on an object, and <a href="/wiki/Duck_typing" title="Duck typing">assuming that method exists</a>&#8212;the <a href="/w/index.php?title=Ruby_Extensions_Project&amp;action=edit&amp;redlink=1" class="new" title="Ruby Extensions Project (page does not exist)">Ruby Extensions Project</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20060425104650/http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/ToProc.rdoc">created a simple hack.</a> </p> <div class="mw-highlight mw-highlight-lang-ruby mw-content-ltr" dir="ltr"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">Symbol</span> <span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">to_proc</span> <span class="w"> </span><span class="nb">proc</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">|</span><span class="n">obj</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">args</span><span class="o">|</span><span class="w"> </span><span class="n">obj</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="nb">self</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">end</span> <span class="k">end</span> </pre></div> <p>Now, method <code>foo</code> can be a function object, i.e. a <code>Proc</code>, via <code>&amp;:foo</code> and used via <code>takes_a_functor(&amp;:foo)</code>. <code>Symbol.to_proc</code> was officially added to Ruby on June 11, 2006 during RubyKaigi2006. <a rel="nofollow" class="external autonumber" href="https://web.archive.org/web/20060820025032/http://redhanded.hobix.com/cult/symbolTo_procExonerated.html">[1]</a> </p><p>Because of the variety of forms, the term Functor is not generally used in Ruby to mean a Function object. Just a type of dispatch <a href="/wiki/Delegation_(programming)" class="mw-redirect" title="Delegation (programming)">delegation</a> introduced by the <a rel="nofollow" class="external text" href="https://web.archive.org/web/20070107205748/http://facets.rubyforge.org/">Ruby Facets</a> project is named as Functor. The most basic definition of which is: </p> <div class="mw-highlight mw-highlight-lang-ruby mw-content-ltr" dir="ltr"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">Functor</span> <span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">initialize</span><span class="p">(</span><span class="o">&amp;</span><span class="n">func</span><span class="p">)</span> <span class="w"> </span><span class="vi">@func</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">func</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">method_missing</span><span class="p">(</span><span class="n">op</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">args</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">blk</span><span class="p">)</span> <span class="w"> </span><span class="vi">@func</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="n">op</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">args</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">blk</span><span class="p">)</span> <span class="w"> </span><span class="k">end</span> <span class="k">end</span> </pre></div> <p>This usage is more akin to that used by functional programming languages, like <a href="/wiki/ML_(programming_language)" title="ML (programming language)">ML</a>, and the original mathematical terminology. </p> <div class="mw-heading mw-heading2"><h2 id="Other_meanings">Other meanings</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=17" title="Edit section: Other meanings"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In a more theoretical context a <i>function object</i> may be considered to be any instance of the class of functions, especially in languages such as <a href="/wiki/Common_Lisp" title="Common Lisp">Common Lisp</a> in which functions are <a href="/wiki/First-class_object" class="mw-redirect" title="First-class object">first-class objects</a>. </p><p>The <a href="/wiki/ML_(programming_language)" title="ML (programming language)">ML</a> family of <a href="/wiki/Functional_programming" title="Functional programming">functional programming</a> languages uses the term <i>functor</i> to represent a <a href="/wiki/Function_(mathematics)" title="Function (mathematics)">mapping</a> from modules to modules, or from types to types and is a technique for reusing code. Functors used in this manner are analogous to the original mathematical meaning of <a href="/wiki/Functor" title="Functor">functor</a> in <a href="/wiki/Category_theory" title="Category theory">category theory</a>, or to the use of generic programming in C++, Java or <a href="/wiki/Ada_(programming_language)" title="Ada (programming language)">Ada</a>. </p><p>In <a href="/wiki/Haskell_(programming_language)" class="mw-redirect" title="Haskell (programming language)">Haskell</a>, the term <i><a href="/wiki/Functor_(functional_programming)" title="Functor (functional programming)">functor</a></i> is also used for a concept related to the meaning of <i>functor</i> in category theory. </p><p>In <a href="/wiki/Prolog" title="Prolog">Prolog</a> and related languages, <i>functor</i> is a synonym for <a href="/wiki/Function_symbol" class="mw-redirect" title="Function symbol">function symbol</a>. </p> <div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=18" title="Edit section: See also"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li><a href="/wiki/Callback_(computer_science)" class="mw-redirect" title="Callback (computer science)">Callback (computer science)</a></li> <li><a href="/wiki/Closure_(computer_science)" class="mw-redirect" title="Closure (computer science)">Closure (computer science)</a></li> <li><a href="/wiki/Function_pointer" title="Function pointer">Function pointer</a></li> <li><a href="/wiki/Higher-order_function" title="Higher-order function">Higher-order function</a></li> <li><a href="/wiki/Command_pattern" title="Command pattern">Command pattern</a></li> <li><a href="/wiki/Currying" title="Currying">Currying</a></li></ul> <div class="mw-heading mw-heading2"><h2 id="Notes">Notes</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=19" title="Edit section: Notes"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <style data-mw-deduplicate="TemplateStyles:r1239543626">.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}</style><div class="reflist reflist-lower-alpha"> <div class="mw-references-wrap"><ol class="references"> <li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text">In C++, a <b>functionoid</b> is an object that has one major method, and a <b>functor</b> is a special case of a functionoid.<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">&#91;</span>1<span class="cite-bracket">&#93;</span></a></sup> They are similar to a function object, <i>but not the same</i>.</span> </li> </ol></div></div> <div class="mw-heading mw-heading2"><h2 id="References">References</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=20" title="Edit section: References"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1239543626"><div class="reflist"> <div class="mw-references-wrap"><ol class="references"> <li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://isocpp.org/wiki/faq/pointers-to-members#functor-vs-functionoid">What's the difference between a functionoid and a functor?</a></span> </li> <li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("//upload.wikimedia.org/wikipedia/commons/6/65/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("//upload.wikimedia.org/wikipedia/commons/d/d6/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("//upload.wikimedia.org/wikipedia/commons/a/aa/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("//upload.wikimedia.org/wikipedia/commons/4/4c/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}</style><cite id="CITEREFSilan_Liu" class="citation web cs1">Silan Liu. <a rel="nofollow" class="external text" href="http://progtutorials.tripod.com/cpp1.htm#_Toc50820124">"C++ Tutorial Part I - Basic: 5.10 Function pointers are mainly used to achieve call back technique, which will be discussed right after"</a>. TRIPOD: Programming Tutorials Copyright © Silan Liu 2002<span class="reference-accessdate">. Retrieved <span class="nowrap">2012-09-07</span></span>. <q>Function pointers are mainly used to achieve call back technique, which will be discussed right after.</q></cite><span title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=unknown&amp;rft.btitle=C%2B%2B+Tutorial+Part+I+-+Basic%3A+5.10+Function+pointers+are+mainly+used+to+achieve+call+back+technique%2C+which+will+be+discussed+right+after.&amp;rft.pub=TRIPOD%3A+Programming+Tutorials+Copyright+%C2%A9+Silan+Liu+2002&amp;rft.au=Silan+Liu&amp;rft_id=http%3A%2F%2Fprogtutorials.tripod.com%2Fcpp1.htm%23_Toc50820124&amp;rfr_id=info%3Asid%2Fen.wikipedia.org%3AFunction+object" class="Z3988"></span></span> </li> <li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite id="CITEREFPaweł_Turlejski2009" class="citation web cs1">Paweł Turlejski (2009-10-02). <a rel="nofollow" class="external text" href="http://progtutorials.tripod.com/cpp1.htm#_Toc50820124">"C++ Tutorial Part I - Basic: 5.10 Function pointers are mainly used to achieve call back technique, which will be discussed right after"</a>. Just a Few Lines<span class="reference-accessdate">. Retrieved <span class="nowrap">2012-09-07</span></span>. <q>PHP 5.3, along with many other features, introduced closures. So now we can finally do all the cool stuff that Ruby / Groovy / Scala / any_modern_language guys can do, right? Well, we can, but we probably won't… Here's why.</q></cite><span title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=unknown&amp;rft.btitle=C%2B%2B+Tutorial+Part+I+-+Basic%3A+5.10+Function+pointers+are+mainly+used+to+achieve+call+back+technique%2C+which+will+be+discussed+right+after.&amp;rft.pub=Just+a+Few+Lines&amp;rft.date=2009-10-02&amp;rft.au=Pawe%C5%82+Turlejski&amp;rft_id=http%3A%2F%2Fprogtutorials.tripod.com%2Fcpp1.htm%23_Toc50820124&amp;rfr_id=info%3Asid%2Fen.wikipedia.org%3AFunction+object" class="Z3988"></span></span> </li> <li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://en.cppreference.com/w/cpp/language/overload_resolution#Call_to_a_class_object">"Overload resolution§Call to a class object"</a>. <i>cppreference.com</i>.</cite><span title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&amp;rft.genre=unknown&amp;rft.jtitle=cppreference.com&amp;rft.atitle=Overload+resolution%C2%A7Call+to+a+class+object&amp;rft_id=https%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Flanguage%2Foverload_resolution%23Call_to_a_class_object&amp;rfr_id=info%3Asid%2Fen.wikipedia.org%3AFunction+object" class="Z3988"></span></span> </li> <li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.paulgraham.com/accgen.html">Accumulator Generator</a></span> </li> <li id="cite_note-phpinvoke-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-phpinvoke_7-0">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://php.net/manual/en/language.oop5.magic.php#object.invoke">PHP Documentation on Magic Methods</a></span> </li> <li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.paulgraham.com/accgen.html">Accumulator Generator</a></span> </li> <li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://docs.python.org/3/reference/compound_stmts.html#function-definitions">Python reference manual - Function definitions</a></span> </li> </ol></div></div> <div class="mw-heading mw-heading2"><h2 id="Further_reading">Further reading</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=21" title="Edit section: Further reading"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li>David Vandevoorde &amp; Nicolai M Josuttis (2006). <i>C++ Templates: The Complete Guide</i>, <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><a href="/wiki/ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&#160;<a href="/wiki/Special:BookSources/0-201-73484-2" title="Special:BookSources/0-201-73484-2">0-201-73484-2</a>: Specifically, chapter 22 is devoted to function objects.</li></ul> <div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Function_object&amp;action=edit&amp;section=22" title="Edit section: External links"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li><a rel="nofollow" class="external text" href="http://c2.com/cgi/wiki?FunctorObject">Description from the Portland Pattern Repository</a></li> <li><a rel="nofollow" class="external text" href="http://www.two-sdg.demon.co.uk/curbralan/papers/AsynchronousC++.pdf">C++ Advanced Design Issues - Asynchronous C++</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20200922012516/http://www.two-sdg.demon.co.uk/curbralan/papers/AsynchronousC++.pdf">Archived</a> 2020-09-22 at the <a href="/wiki/Wayback_Machine" title="Wayback Machine">Wayback Machine</a> by <a href="/wiki/Kevlin_Henney" title="Kevlin Henney">Kevlin Henney</a></li> <li><a rel="nofollow" class="external text" href="http://www.newty.de/fpt/index.html">The Function Pointer Tutorials</a> by Lars Haendel (2000/2001)</li> <li>Article "<a rel="nofollow" class="external text" href="https://web.archive.org/web/20041009232434/http://www.cuj.com/documents/s%3D8464/cujcexp0308sutter/">Generalized Function Pointers</a>" by <a href="/wiki/Herb_Sutter" title="Herb Sutter">Herb Sutter</a></li> <li><a rel="nofollow" class="external text" href="http://jga.sourceforge.net/">Generic Algorithms for Java</a></li> <li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20100330073950/http://www.amcgowan.ca/blog/computer-science/php-functors-function-objects-in-php/">PHP Functors - Function Objects in PHP</a></li> <li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20041013202445/http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.10">What the heck is a functionoid, and why would I use one?</a> (C++ FAQ)</li></ul> <!-- NewPP limit report Parsed by mw‐web.codfw.main‐f69cdc8f6‐b7zbx Cached time: 20241122141359 Cache expiry: 2592000 Reduced expiry: false Complications: [vary‐revision‐sha1, show‐toc] CPU time usage: 0.524 seconds Real time usage: 4.326 seconds Preprocessor visited node count: 1204/1000000 Post‐expand include size: 18756/2097152 bytes Template argument size: 947/2097152 bytes Highest expansion depth: 14/100 Expensive parser function count: 47/500 Unstrip recursion depth: 1/20 Unstrip post‐expand size: 98894/5000000 bytes Lua time usage: 0.227/10.000 seconds Lua memory usage: 4450443/52428800 bytes Number of Wikibase entities loaded: 0/400 --> <!-- Transclusion expansion time report (%,ms,calls,template) 100.00% 4201.355 1 -total 3.02% 126.684 1 Template:Short_description 2.95% 123.845 2 Template:Reflist 2.48% 104.262 3 Template:Cite_web 2.11% 88.596 2 Template:Pagetype 1.76% 74.074 1 Template:Refimprove 1.60% 67.094 1 Template:Ambox 0.87% 36.344 1 Template:About 0.55% 22.979 6 Template:Main_other 0.50% 21.144 1 Template:ISBN --> <!-- Saved in parser cache with key enwiki:pcache:idhash:509999-0!canonical and timestamp 20241122141359 and revision id 1223836041. Rendering was triggered because: page-view --> </div><!--esi <esi:include src="/esitest-fa8a495983347898/content" /> --><noscript><img src="https://login.wikimedia.org/wiki/Special:CentralAutoLogin/start?type=1x1" alt="" width="1" height="1" style="border: none; position: absolute;"></noscript> <div class="printfooter" data-nosnippet="">Retrieved from "<a dir="ltr" href="https://en.wikipedia.org/w/index.php?title=Function_object&amp;oldid=1223836041">https://en.wikipedia.org/w/index.php?title=Function_object&amp;oldid=1223836041</a>"</div></div> <div id="catlinks" class="catlinks" data-mw="interface"><div id="mw-normal-catlinks" class="mw-normal-catlinks"><a href="/wiki/Help:Category" title="Help:Category">Categories</a>: <ul><li><a href="/wiki/Category:Object_(computer_science)" title="Category:Object (computer science)">Object (computer science)</a></li><li><a href="/wiki/Category:Subroutines" title="Category:Subroutines">Subroutines</a></li></ul></div><div id="mw-hidden-catlinks" class="mw-hidden-catlinks mw-hidden-cats-hidden">Hidden categories: <ul><li><a href="/wiki/Category:Articles_with_short_description" title="Category:Articles with short description">Articles with short description</a></li><li><a href="/wiki/Category:Short_description_is_different_from_Wikidata" title="Category:Short description is different from Wikidata">Short description is different from Wikidata</a></li><li><a href="/wiki/Category:Articles_needing_additional_references_from_February_2009" title="Category:Articles needing additional references from February 2009">Articles needing additional references from February 2009</a></li><li><a href="/wiki/Category:All_articles_needing_additional_references" title="Category:All articles needing additional references">All articles needing additional references</a></li><li><a href="/wiki/Category:Webarchive_template_wayback_links" title="Category:Webarchive template wayback links">Webarchive template wayback links</a></li><li><a href="/wiki/Category:Articles_with_example_C_code" title="Category:Articles with example C code">Articles with example C code</a></li><li><a href="/wiki/Category:Articles_with_example_C%2B%2B_code" title="Category:Articles with example C++ code">Articles with example C++ code</a></li><li><a href="/wiki/Category:Articles_with_example_D_code" title="Category:Articles with example D code">Articles with example D code</a></li><li><a href="/wiki/Category:Articles_with_example_Eiffel_code" title="Category:Articles with example Eiffel code">Articles with example Eiffel code</a></li><li><a href="/wiki/Category:Articles_with_example_Java_code" title="Category:Articles with example Java code">Articles with example Java code</a></li><li><a href="/wiki/Category:Articles_with_example_JavaScript_code" title="Category:Articles with example JavaScript code">Articles with example JavaScript code</a></li><li><a href="/wiki/Category:Articles_with_example_Julia_code" title="Category:Articles with example Julia code">Articles with example Julia code</a></li><li><a href="/wiki/Category:Articles_with_example_Lisp_(programming_language)_code" title="Category:Articles with example Lisp (programming language) code">Articles with example Lisp (programming language) code</a></li><li><a href="/wiki/Category:Articles_with_example_Objective-C_code" title="Category:Articles with example Objective-C code">Articles with example Objective-C code</a></li><li><a href="/wiki/Category:Articles_with_example_Perl_code" title="Category:Articles with example Perl code">Articles with example Perl code</a></li><li><a href="/wiki/Category:Articles_with_example_PHP_code" title="Category:Articles with example PHP code">Articles with example PHP code</a></li><li><a href="/wiki/Category:Articles_with_example_Python_(programming_language)_code" title="Category:Articles with example Python (programming language) code">Articles with example Python (programming language) code</a></li><li><a href="/wiki/Category:Articles_with_example_Ruby_code" title="Category:Articles with example Ruby code">Articles with example Ruby code</a></li></ul></div></div> </div> </main> </div> <div class="mw-footer-container"> <footer id="footer" class="mw-footer" > <ul id="footer-info"> <li id="footer-info-lastmod"> This page was last edited on 14 May 2024, at 17:22<span class="anonymous-show">&#160;(UTC)</span>.</li> <li id="footer-info-copyright">Text is available under the <a href="/wiki/Wikipedia:Text_of_the_Creative_Commons_Attribution-ShareAlike_4.0_International_License" title="Wikipedia:Text of the Creative Commons Attribution-ShareAlike 4.0 International License">Creative Commons Attribution-ShareAlike 4.0 License</a>; additional terms may apply. By using this site, you agree to the <a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Terms_of_Use" class="extiw" title="foundation:Special:MyLanguage/Policy:Terms of Use">Terms of Use</a> and <a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy" class="extiw" title="foundation:Special:MyLanguage/Policy:Privacy policy">Privacy Policy</a>. Wikipedia® is a registered trademark of the <a rel="nofollow" class="external text" href="https://wikimediafoundation.org/">Wikimedia Foundation, Inc.</a>, a non-profit organization.</li> </ul> <ul id="footer-places"> <li id="footer-places-privacy"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy">Privacy policy</a></li> <li id="footer-places-about"><a href="/wiki/Wikipedia:About">About Wikipedia</a></li> <li id="footer-places-disclaimers"><a href="/wiki/Wikipedia:General_disclaimer">Disclaimers</a></li> <li id="footer-places-contact"><a href="//en.wikipedia.org/wiki/Wikipedia:Contact_us">Contact Wikipedia</a></li> <li id="footer-places-wm-codeofconduct"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Universal_Code_of_Conduct">Code of Conduct</a></li> <li id="footer-places-developers"><a href="https://developer.wikimedia.org">Developers</a></li> <li id="footer-places-statslink"><a href="https://stats.wikimedia.org/#/en.wikipedia.org">Statistics</a></li> <li id="footer-places-cookiestatement"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Cookie_statement">Cookie statement</a></li> <li id="footer-places-mobileview"><a href="//en.m.wikipedia.org/w/index.php?title=Function_object&amp;mobileaction=toggle_view_mobile" class="noprint stopMobileRedirectToggle">Mobile view</a></li> </ul> <ul id="footer-icons" class="noprint"> <li id="footer-copyrightico"><a href="https://wikimediafoundation.org/" class="cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled"><img src="/static/images/footer/wikimedia-button.svg" width="84" height="29" alt="Wikimedia Foundation" loading="lazy"></a></li> <li id="footer-poweredbyico"><a href="https://www.mediawiki.org/" class="cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled"><img src="/w/resources/assets/poweredby_mediawiki.svg" alt="Powered by MediaWiki" width="88" height="31" loading="lazy"></a></li> </ul> </footer> </div> </div> </div> <div class="vector-settings" id="p-dock-bottom"> <ul></ul> </div><script>(RLQ=window.RLQ||[]).push(function(){mw.config.set({"wgHostname":"mw-web.codfw.main-688fc9465-x8pwh","wgBackendResponseTime":175,"wgPageParseReport":{"limitreport":{"cputime":"0.524","walltime":"4.326","ppvisitednodes":{"value":1204,"limit":1000000},"postexpandincludesize":{"value":18756,"limit":2097152},"templateargumentsize":{"value":947,"limit":2097152},"expansiondepth":{"value":14,"limit":100},"expensivefunctioncount":{"value":47,"limit":500},"unstrip-depth":{"value":1,"limit":20},"unstrip-size":{"value":98894,"limit":5000000},"entityaccesscount":{"value":0,"limit":400},"timingprofile":["100.00% 4201.355 1 -total"," 3.02% 126.684 1 Template:Short_description"," 2.95% 123.845 2 Template:Reflist"," 2.48% 104.262 3 Template:Cite_web"," 2.11% 88.596 2 Template:Pagetype"," 1.76% 74.074 1 Template:Refimprove"," 1.60% 67.094 1 Template:Ambox"," 0.87% 36.344 1 Template:About"," 0.55% 22.979 6 Template:Main_other"," 0.50% 21.144 1 Template:ISBN"]},"scribunto":{"limitreport-timeusage":{"value":"0.227","limit":"10.000"},"limitreport-memusage":{"value":4450443,"limit":52428800}},"cachereport":{"origin":"mw-web.codfw.main-f69cdc8f6-b7zbx","timestamp":"20241122141359","ttl":2592000,"transientcontent":false}}});});</script> <script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":"Article","name":"Function object","url":"https:\/\/en.wikipedia.org\/wiki\/Function_object","sameAs":"http:\/\/www.wikidata.org\/entity\/Q2072111","mainEntity":"http:\/\/www.wikidata.org\/entity\/Q2072111","author":{"@type":"Organization","name":"Contributors to Wikimedia projects"},"publisher":{"@type":"Organization","name":"Wikimedia Foundation, Inc.","logo":{"@type":"ImageObject","url":"https:\/\/www.wikimedia.org\/static\/images\/wmf-hor-googpub.png"}},"datePublished":"2004-03-07T08:06:18Z","dateModified":"2024-05-14T17:22:16Z","headline":"programming construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax"}</script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10