Avatar ·

What does suspend function mean in Kotlin coroutines? Explanation for beginners

Hi everyone! I started learning coroutines in Kotlin and I can't understand what a suspend function is. The documentation says it's a "suspending function," but that doesn't tell me anything.\n\nI tried to write simple code, but it doesn't compile:\n\n```kotlin\nfun main() {\n fetchUserData() // Error: Suspend function 'fetchUserData' should be called only from a coroutine or another suspend function\n}\n\nsuspend fun fetchUserData(): String {\n delay(1000) // simulating a long operation\n return "User data"\n}\n```\n\nWhy can't I just call this function from main? What is "suspend" really? How does it work under the hood? I've read about async/await in other languages, but here everything looks different. Please explain in simple terms with examples what a suspend function is.