title: Erlang: Returning a function from a function
body: <p>I know Erlang supports anonymous functions. My question is, can I return a function from a function then call that returned function from outside? If so, how do I do it? I know this is possible in many languages such as C and Python. Here is what I tried to do, but it doesn't work:</p>
  
  <pre><code>-module(test).
  -export([run/0]).
  
  test() -&gt;
      io:format("toasters", []).
  
  bagel() -&gt;
      test.
  
  run() -&gt;
      (bagel())().
  </code></pre>
  
  <p>Results:</p>
  
  <pre><code>Erlang/OTP 17 [erts-6.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
  
  Eshell V6.2  (abort with ^G)
  1&gt; c(test).
  test.erl:4: Warning: function test/0 is unused
  {ok,test}
  2&gt; test:run().
  ** exception error: bad function test
       in function  test:run/0 (test.erl, line 11)
  3&gt; 
  </code></pre>