Parameterized Function Example Kotlin Program
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 methodName(variable_name_1 : data_type, variable_name_2 : data_type) : return_data_type{
//Functionality code
}
Parameterized Function Example Program
fun main(args: Array < String > ) {
val num1 = 11
val num2 = 6
//Calling function with 2 parameters num1 and num2 and assigning the result to num3
val num3 = minValue(num1, num2)
//Printing result
println("Minimum Value while comparison of num1 and num2 is = " + num3)
}
//Function with Int return type
fun minValue(i: Int, j: Int): Int {
val min: Int
if (i > j) {
min = j
} else {
min = i
}
return min
}
Sample Output
Minimum Value while comparison of num1 and num2 is = 6
Read More Articles
- Declare Variables In Kotlin
- Read Data Input using Scanner in Kotlin
- print and println Data Output in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Is Operator (is and !is) in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Read Data Input from Command Line in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Read String Data Input in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Logical Operators in Kotlin
- Elvis Operator (?:) in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin