Junior — Middle
Kas saaks anda näite koodist, mis ei muutu käivitamisel ja ei põhjusta kõrvaltoimeid?
sobes.tech AI
Vastus AI-lt
An example of code without state changes and side effects is a pure function. It always returns the same result with the same input data 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 change any variables outside itself and does not cause side effects — it simply returns the sum of the arguments.
Another example — getting a string from resources without changes:
fun getWelcomeMessage(): String {
return "Welcome!"
}
Such functions are easy to test and predictable.