Fun inside foreach

I can’t understand how the fun inside the foreach being called, like this example from kotlin documentation:

fun foo() {
listOf(1, 2, 3, 4, 5).forEach(fun(value: Int) {
    if (value == 3) return  // local return to the caller of the anonymous fun, i.e. the forEach loop
print(" done with anonymous function")


The forEach function applies its argument, i.e. an anonymous function here, to each member of the list in turn.

It therefore prints 12 but when it reaches 3, it returns from the anonymous function without printing it - but doesn’t return from the forEach function itself.

The forEach function then applies the anonymous function to 4 and 5, printing them, before it finally completes.