FUNCTION KOTLIN PROGRAMS
Thu Oct 31 2024 16:14:43 GMT+0000 (Coordinated Universal Time)
Saved by @carona
1.//Standard Library Function fun main(args: Array<String>) { // arrayOf()-- to create an array by passing the values of the elements to the function. var sum = arrayOf(1,2,3,4,5,6,7,8,9,10).sum() println("The sum of all the elements of an array is: $sum") } Output: The sum of all the elements of an array is: 55 2.// Kotlin user-defined function student() having different types of parameters- fun student(name: String , roll_no: Int , grade: Char) { println("Name of the student is : $name") println("Roll no of the student is: $roll_no") println("Grade of the student is: $grade") } fun main(args: Array<String>) { var result = student("CSE-B",66,'A') println("Details of Student: $result") } Output: Name of the student is : CSE-B Roll no of the student is: 66 Grade of the student is: A [OR] // Kotlin user-defined function student() having different types of parameters- fun student(name: String , roll_no: Int , grade: Char) { println("Name of the student is : $name") println("Roll no of the student is: $roll_no") println("Grade of the student is: $grade") } fun main(args: Array<String>) { student("CSE-B",66,'A') } Output: Name of the student is : CSE-B Roll no of the student is: 66 Grade of the student is: A 3. // function with parameter & with return type fun CSE_B(x: Int): Int { return (x + 5) } fun main() { var result = CSE_B(3) println(result) } Output: 8 4.// function WITHOUT PARAMETERS & WITHOUT RETURN TYPE fun CSE_B() { println("WELCOME TO MAD LAB") } fun main() { CSE_B() } Output: WELCOME TO MAD LAB 5.// demonstrate how to pass a variable number of arguments to a function Using vararg fun main (args: Array<String>) { CSE_B ( "abc", "def", "ghi", "123", "sun") } fun CSE_B (vararg a: String) { for (a_ in a) { println(a_) } } Output: abc def ghi 123 sun 6.// lambda function fun main(args: Array<String>){ val myLambda: (Int) -> Unit= {s: Int -> println(s) } //lambdafunction addNumber(5,10,myLambda) } //The variable mylambda in function definition is actually a lambdafunction. fun addNumber(a: Int, b: Int, mylambda: (Int) -> Unit ){ //high level function lambda as parameter val add = a + b mylambda(add) // println(add) } Output: 15 7.// lambda function // with type annotation in lambda expression val sum1 = { a: Int, b: Int -> a + b } // Kotlin program of using lambda expression- // without type annotation in lambda expression val sum2:(Int,Int)-> Int = { a , b -> a + b} fun main(args: Array<String>) { val result1 = sum1(2,3) val result2 = sum2(3,4) println("The sum of two numbers is: $result1") println("The sum of two numbers is: $result2") // directly print the return value of lambda // without storing in a variable. println(sum1(5,7)) } Output: The sum of two numbers is: 5 The sum of two numbers is: 7 12 8.// anonymous function // anonymous function with body as an expression val anonymous1 = fun(x: Int, y: Int): Int = x + y // anonymous function with body as a block val anonymous2 = fun(a: Int, b: Int): Int { val mul = a * b return mul } fun main(args: Array<String>) { //invoking functions val sum = anonymous1(3,5) val mul = anonymous2(3,5) println("The sum of two numbers is: $sum") println("The multiply of two numbers is: $mul") } Output: The sum of two numbers is: 8 The multiply of two numbers is: 15 9.//Kotlin program of lambda expression which returns Unit- var lambda = {println("WELCOME TO CSE-B MAD LAB")} // lambda expression // higher-order function fun higherfunc( lmbd: () -> Unit ) { // accepting lambda as parameter lmbd() //invokes lambda expression } fun main(args: Array<String>) { //invoke higher-order function higherfunc(lambda) // passing lambda as parameter } Output: WELCOME TO CSE-B MAD LAB 10. //Kotlin program of lambda expression which returns Integer value – var lambda = {a: Int , b: Int -> a + b } // lambda expression // higher order function fun higherfunc( lmbd: (Int, Int) -> Int) { // accepting lambda as parameter var result = lmbd(2,4) // invokes the lambda expression by passing parameters println("The sum of two numbers is: $result") } fun main(args: Array<String>) { higherfunc(lambda) //passing lambda as parameter } Output: The sum of two numbers is: 6 11. //Take input from user using readline() method fun main(args : Array<String>) { println("Enter text: ") var input = readLine() print("You entered: $input") } Output: Enter text: CSE-B You entered: CSE-B
Comments