डिफॉल्ट arguments function

डिफॉल्ट arguments function

कॅटलीन मध्ये function  ला डिफॉल्ट argument पास करता येतात, जर तुम्ही डिफॉल्ट value पास केली नसेल तर तुम्ही function हेडर मध्ये define केलेली value  use केली जाते , आणि जर तुम्ही default value पास केलीत तर तुम्ही जी value पास केलेली आहे ती value use केली जाते.

fun main(args : Array<String>)
{
    //default argument height is not passed    println("Area is : " + defaultArgument(2, 4))
    
    //default argument height is passed    println("Area is : " + defaultArgument(2, 4, 15))
}

fun defaultArgument(lenght:Int, width:Int, height:Int = 10):Int
{
    return lenght * width * height
}

आउटपुट 
Area is : 80
Area is : 120



जावा मध्ये डिफॉल्ट arguments नसतात त्यामुळे जावा मधून कॅटलिन चा कोड कॉल करण्यापूर्वी कॅटलिन 
मधील function ला @JvmOverloads हे अनोटेशन अप्लाय करावे लागते त्यामुळे आपल्याला जावा कोड मधून
कॅटलीन चा कोड कॉल करता येतो. 

कॅटलीन कोड 

@JvmOverloadsfun defaultArgument(lenght:Int, width:Int, height:Int = 10):Int
{
    return lenght * width * height
}

----------------------------------------------------------------


जावा कोड 
public static void main(String[] args) {

        codeTesting codeTesting = new codeTesting();
        // calling with default value        System.out.println( "Area is " + codeTesting.defaultArgument(2,4));
        // calling without default value        System.out.println( "Area is " + codeTesting.defaultArgument(2,4, 15));    }

आउटपुट 

Area is 80
Area is 120

Comments

Popular posts from this blog

सिम्पल क्लास example

कॉटलीन डेटाटाइप