Pure Functions - Side Effects


fun addNewMessageSideEffect(messages: ArrayList<ChatMessage>, newMessage: ChatMessage) { 
  messages.add(newMessage)
  // Modifies input
}

val externalMessages: List<ChatMessage> = ArrayList()

fun addNewMessageSideEffect2(newMessage: ChatMessage) { 
  externalMessages.add(newMessage)
  // Modifies something outside of scope
}


fun addNewMessage(messages: List<ChatMessage>, newMessage: ChatMessage) : List<ChatMessage> { 
  return messages.plus(newMessage)
}

Back Home Next