I have these code
start() ->
spawn(?MODULE, init, [self()]).
init(From) ->
spawn(?MODULE, broadcast, []).
broadcast() ->
Msg = "helloworld",
timer:sleep(10000),
broadcast().
When I test these code in Erlang shell, it give me undef error, I need to export broadcast function, I am just refuse
Code
means you will spawn process which initial call will be
?MODULE:init(self())or more precisely equivalent ofapply(?MODULE,init,[self()]).?MODULEis macro evaluated to current module name but anyway It is external function call so this function have to be exported even there is?MODULEused. In contraryis spawn with initial call to the func
fun() -> init(self()) endwhich callsinit/1with result of functionself(). It is local call which meansinit/1may not be exported. There is another issue with it whenself()is performed inside new process so you have to writeto achieve same effect as in
spawn(?MODULE, init, [self()])whereself()is evaluated as parameter ofspawn/3.