Skip to main content

Simple Function Example Kotlin Program

2 min read Updated June 30, 2026
Share:
On this page (6sections)

Definition

A function is a block of code written to perform a task. A function may be a parameterized function or a parameter-less function. A function may return a value or it may not return a value. The name of a function starts with a small letter and then follows CamelCase.

Syntax

fun method_name(){
//Functionality code
}

Function Example Program

fun main(args: Array < String > ) {
    val num1 = 11
    val num2 = 6

    //Calling function with no parameters and return type
    print()
    //Calling function with 2 parameters num1 and num2 and assigning the result to num3
    val num3 = add(num1, num2)

    //Printing result
    println("After addition of num1 and num2, the result is : " + num3)
}

//Function with parameters and Int return type
fun add(i: Int, j: Int): Int {
    return i + j
}

//Function without parameters and no return type
fun print() {
    println("I am inside print function.")
}

Sample Output

I am inside print function.
After addition of num1 and num2, the result is : 17

How It Works

Here is a step-by-step walkthrough of how the Kotlin program for Simple Function Example Kotlin Program runs, line by line:

  1. fun main(args: Array < String > ) { — defines a function used by the program.
  2. val num1 = 11 — declares or assigns a value the program uses.
  3. val num2 = 6 — declares or assigns a value the program uses.
  4. print() — writes a line of output shown in the sample output.
  5. val num3 = add(num1, num2) — declares or assigns a value the program uses.
  6. println("After addition of num1 and num2, the result is : " + num3) — writes a line of output shown in the sample output.
  7. fun add(i: Int, j: Int): Int { — defines a function used by the program.
  8. return i + j — returns the computed result.

After running it, compare your console output with the Sample Output above. Try changing the values and re-running the program to see how the result changes — experimenting is the fastest way to understand the logic.

Frequently Asked Questions

What is Simple Function Example in Kotlin?
A function is a block of code written to perform a task.
How do I run this Kotlin example?
Run it in IntelliJ IDEA or Android Studio, or compile from the command line with `kotlinc file.kt -include-runtime -d file.jar` and run `java -jar file.jar`.
How can I practice Simple Function Example?
Copy the example into IntelliJ IDEA or Android Studio, run it, then change the values or add print statements to see how the output changes.

Related Tutorials

Search tutorials