Junior — Middle
Can you give an example of code that does not change when executed and does not cause side effects?
sobes.tech AI
Answer from AI
An example of code without state changes and side effects is a pure function. It always returns the same result for the same inputs and does not modify external state.
Example in Kotlin for Android:
fun sum(a: Int, b: Int): Int {
return a + b
}
This function does not modify any variables outside itself and does not cause side effects — it simply returns the sum of its arguments.
Another example is retrieving a string from resources without modifications:
fun getWelcomeMessage(): String {
return "Welcome!"
}
Such functions are easy to test and predictable.