Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A language that considers procedures to be "first-class" <strong><em>allows functions to be passed around just like any other value</em></strong>.</p> <p>Languages like Java 7 (and earlier) and C "kind of" have this capability: C allows function pointers to be passed around, but you can't dynamically define a function in those languages and suddenly pass that somewhere else. Java before version 8 can <em>simulate</em> this to a certain extent with anonymous classes, but it doesn't technically have first-class functions.</p> <p>On the other hand, C++, D, C#, Visual Basic .NET, Java 8+, and functional languages (like Scheme and Haskell) <em>do</em> allow you to pass around functions like variables. For example, the code below <em>returns</em> a function that adds <code>addend</code> to its input:</p> <p>Written in D:</p> <pre><code>int delegate(int) makeAdder(int addend) //Returns a function { return delegate int(int x) //Long way { return x + addend; //Notice that addend came from _outside_ the function }; return (int x) { return x + addend; }; //Short way return x =&gt; addend + x; //Super-short way, introduced in D 2.058 } </code></pre> <p>Written in C#:</p> <pre><code>Func&lt;int, int&gt; MakeAdder(int addend) //Returns a function { return delegate(int x) //The long way. Note: Return type is implicitly 'int' { return x + addend; }; return x =&gt; x + addend; //Short way: x "goes to" (x + addend); inferred types } </code></pre> <p>Written in C++:</p> <pre><code>#include &lt;functional&gt; std::function&lt;int(int)&gt; make_adder(int addend) { return [=](int x) { return addend + x; }; } </code></pre> <p>Written in Scala:</p> <pre><code>def makeAdder(addend: Int) = (x: Int) =&gt; addend + x </code></pre> <p>Written in Python:</p> <pre><code>def make_adder(addend): def f(x): return addend + x return f # or... return lambda x: addend + x </code></pre> <p>Written in Erlang:</p> <pre><code>make_adder(Addend) -&gt; fun(X) -&gt; Addend + X end. </code></pre> <p>Written in JavaScript:</p> <pre><code>function makeAdder(addend) { return function(x) { return addend + x; }; } </code></pre> <p>Written in JavaScript (ES2015 arrow function syntax):</p> <pre><code>const makeAdder = addend =&gt; x =&gt; addend + x; </code></pre> <p>Written in Scheme:</p> <pre><code>(define (makeAdder addend) (lambda (x) (+ x addend))) </code></pre> <p>Written in Haskell:</p> <pre><code>makeAdder :: Int -&gt; (Int -&gt; Int) makeAdder addend = \x -&gt; addend + x </code></pre> <p>Written in Visual Basic 2008:</p> <pre><code>Function MakeAdder(addend As Integer) As Func(Of Integer, Integer) Return Function(x) (x + addend) End Function </code></pre> <p>Written in Swift (both verbose and short-hand implementations):</p> <pre><code>func makeAdder(append: Int) -&gt; (x: Int) -&gt; Int { return { (x: Int) -&gt; Int in return x + append }; } func makeAdder(append: Int) -&gt; (Int) -&gt; Int { return {$0 + append}; } </code></pre> <p>(By the way, a "lambda" is just a function without a name. Lambdas are only supported in languages that support first-class functions.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload