Answers for "kotlin define function"

2

function in kotlin

-------------------------------------------------------------------------------------------------------------------------------
(Extension functions) ---------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
    1. can "add" function to the class without declaring it
    2. the new added functions behaves like "static"

    - this is used to add function in the perdefine class

    @for my coustom class
        # var student = Student()
        # println("Pass status ${student.hasPassed(57)}")
        # println("Scholar status ${student.isScholar(57)}")
        # 
        # fun Student.isScholar(marks: Int):Boolean{
        #     return marks>95
        # }
        # 
        # class Student(){
        # 
        #     fun hasPassed(marks:Int):Boolean{
        #         return marks > 40
        #     }
        # 
        # }

    @example for perdefine class
        # val str1:String = "hello"
        # val str2:String = "world"
        # val str3:String = "hy"
        # 
        # println(str3.add(str1,str2))
        # 
        # fun String.add(s1:String,s2:String):String{
        #     return this+s1+s2
        # }




-------------------------------------------------------------------------------------------------------------------------------
(Infix functions) -------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
    1. "infix function can be a Membory function or Extension function"
    2. they have "single parameter"
    3. they have perfix of "infox"

    # var student = Student()
    # println("Scholar status ${student isScholar 80}")
    # 
    # infix fun Student.isScholar(marks: Int):Boolean{
    #     return marks>95
    # }
    # 
    # class Student()



-------------------------------------------------------------------------------------------------------------------------------
(TailRec functions) -----------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
    1. "use recursion in optimised way"
    2. prevent "stack overflowException"
    3. perfix of "tailrec" is used

    @Fibonacci number
        # println(getFibonacciNumber(50000000 , BigInteger("0"),BigInteger("1")))
        # 
        # tailrec fun getFibonacciNumber(n:Int , a:BigInteger , b:BigInteger):BigInteger{
        #     return if (n==0) b else getFibonacciNumber(n-1,a+b,a)
        # }
Posted by: Guest on November-13-2021
2

how to write a function in kotlin

// Use 'fun' keyword to declare function
// 'num' is the parameter and is of type Int
// The ':Int' indicates the return type
fun square(num: Int):Int {
    return num * num
}
Posted by: Guest on November-06-2020
2

kotlin function

fun sum(a: Int, b: Int): Int {
 return a + b
}
Posted by: Guest on January-15-2021
0

make function kotlin

fun main() {
	println("This is a function")
}
Posted by: Guest on January-14-2021
0

how to do a function kotlin

//Okay I found a couple of examples, but I think this one should help the beginners out there who need the syntax!

fun callMe() {
    println("Hello")
    println("cool")
}

fun main() {
    callMe()
    println("Printing outside from callMe() function.")
}

//I hope it helps out! I try to make new languages hopefully easy for you to learn!
Posted by: Guest on July-26-2021
-1

function in kotlin

kotlin function
Posted by: Guest on June-27-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language