Declaring Functions
Parameters, return types, and defaults.
What Is a Function?
A function is a named block of code you can run again and again. Instead of repeating lines, you call the function by its name.
In Kotlin you declare a function with the fun keyword, followed by a name and a pair of parentheses.
Your First Function
Here is a simple function called greet. The body lives inside the curly braces { }.
We call it from main by writing its name with parentheses: greet().
fun greet() {
println("Hello, Kotlin!")
}
fun main() {
greet()
}All lessons in this course
- Declaring Functions
- Classes and Properties
- Constructors and init
- Data Classes