-module(prog).
-export([main/0]).

main() ->
  Pid = spawn(fun() -> loop(0) end),
  procidurka(Pid, update_n),
  R1 = tipa_function(Pid, get_n),
  io:format("after procidurka call tipa_function returned: ~p~n",[R1]),
  procidurka(Pid, update_n),
  R2 = tipa_function(Pid, get_n),
  io:format("after procidurka call tipa_function returned: ~p~n",[R2]),
  procidurka(Pid, update_n),
  R3 = tipa_function(Pid, get_n),
  io:format("after procidurka call tipa_function returned: ~p~n",[R3]).
	
procidurka(Pid, Request) ->
  Pid ! {self(), Request}.  
  
tipa_function(Pid, Request) ->
  Pid ! {self(), Request},
  receive
    Response
      ->
        Response
  end.  
  
loop(N) ->
  receive
    {From, get_n} ->
      From ! N,
      loop(N);
    {From, update_n} ->
      loop(N+1);
    {From, Other} ->
      From ! {error,Other},
      loop(N)
  end.