Kotlin Functions Basic Functions

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Functions are declared using the fun keyword, followed by a function name and any parameters. You can also specify the return type of a function, which defaults to Unit. The body of the function is enclosed in braces {}. If the return type is other than Unit, the body must issue a return statement for every terminating branch within the body.

fun sayMyName(name: String): String {
    return "Your name is $name" 
} 

A shorthand version of the same:

fun sayMyName(name: String): String = "Your name is $name" 

And the type can be omitted since it can be inferred:

fun sayMyName(name: String) = "Your name is $name" 


Got any Kotlin Question?