title: Erlang: case..of construct for function call return?
body: <p>The errors generated by the code are </p>
  
  <pre><code>2&gt; X = "2".
  "2"
  3&gt; case_:main(X).
  main 50
  sender 50
  ** exception error: bad argument
       in function  case_:sender/1 (case_.erl, line 14)
       in call from case_:main/1 (case_.erl, line 6)
  4&gt; Z = 2.
  2
  5&gt; case_:main(Z).
  ** exception error: bad argument
       in function  io:format/3
          called as io:format(&lt;0.25.0&gt;,"main ~p~n",2)
       in call from case_:main/1 (case_.erl, line 5)
  6&gt; 
  </code></pre>
  
  <p>On the first try I tried to pass an string which made it a lot farther than the second try passing an integer. I'm not sure why this doesn't work. </p>
  
  <p>The function call <code>sender(Input)</code> should return <code>{Data}</code> from the <code>receiver()</code> function call.</p>
  
  <p>I definitely need the message passing part of the program since I'm trying to write a loop that takes messages, evaluates them and returns the result; but maybe the <code>case...of</code> statement could be thrown out. </p>
  
  <pre><code>-module(case_).
  -export([main/1, sender/1, receiver/0]).
  
  main(Input) -&gt;
      io:format("main ~p~n",Input),
      case sender(Input) of
          {Data} -&gt;
              io:format("Received ~p~n",Data)
      end.
  
  sender(Input) -&gt;
      io:format("sender ~p~n",Input),
      Ref = make_ref(),
      ?MODULE  ! { self(), Ref, {send_data, Input}},
      receive
          {Ref, ok, Data} -&gt;
              {Data}      
      end.    
  
  receiver() -&gt;
      io:format("receiver ~n"),
      receive
          {Pid, Ref, {send_data, Input}} -&gt;
              Pid ! { Ref, ok, Input + Input} 
      end.
  </code></pre>