[erlang-questions] Re: Concept of Side-effect

jm jeffm@REDACTED
Fri Sep 18 05:00:29 CEST 2009



Kaiduan Xie wrote:
> Thanks Benjamin, but sometimes printing out to the console is really
> what you want. Maybe this is not a good example :)
>   

Even when you do want the program to print to the screen it is a 
side-effect.

Lets try breaking it down:


>>> add(X,Y) ->
>>>     X+Y.
>>>       

call add/2 with X and Y
   calculate X+Y
   return result
result now available in calling function

>>> add_effects(X,Y) ->
>>>     io:format("~p~n",[X+Y]).
>>>
>>>       

call add/2 with X and Y
   calculate X+Y
   pass message to io to print to screen (** side effect **)
   return result
result now available in calling function

>>> What side-effect add_effects generates? Why we say io:format()
>>> generates side-effect?
>>>
>>>
>>>       
note how in the second example the calling function doesn't know 
anything about the effects of the io:format/2 function call. It is an 
effect outside the, or aside from, main excution path and hence a 
side-effect.

Jeff.



More information about the erlang-questions mailing list