Skip to main content

Defining Function and Its Type 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. A function can be declared in many ways and are described below

Syntax

fun method_name(){
//Functionality code
}

OR

fun method_name(variable_name:data_type):return_type{
//Functionality code
}

OR

fun method_name(variable_name:data_type){
//Functionality code
}

Defining Function Example Program

fun main(args: Array < String > ) {
    println("Output from multiplyFunctionOne is : " + multiplyFunctionOne(3, 5))
    println("Output from multiplyFunctionTwo is : " + multiplyFunctionTwo(4, 5))
    multiplyFunctionThree(5, 5)
    multiplyFunctionFour(6, 5)
}

//Function with two Int inputs and Int return type
fun multiplyFunctionOne(a: Int, b: Int): Int {
    return a * b
}

//Function with two Int inputs and Result is returned without mentioning return type
fun multiplyFunctionTwo(a: Int, b: Int) = a * b

//Function with two Int inputs and Unit return type
fun multiplyFunctionThree(a: Int, b: Int): Unit {
    println("Multiplication of $a and $b is ${a * b}")
}

//Function with Unit return type need not be mentioned and can be declared like below
fun multiplyFunctionFour(a: Int, b: Int) {
    println("Multiplication of $a and $b is ${a * b}")
}

Sample Output

Output from multiplyFunctionOne is : 15
Output from multiplyFunctionTwo is : 20
Multiplication of 5 and 5 is 25
Multiplication of 6 and 5 is 30

How It Works

This Kotlin program demonstrates Defining Function and Its Type. It first prepares the data it needs, then performs the core operation step by step, and finally prints the output shown in the Sample Output above.

  1. Declare the variables that hold the program’s data.
  2. Print the final result to the console so you can compare it with the sample output.

Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.

Frequently Asked Questions

What is Defining Function and Its Type 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 Defining Function and Its Type?
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