Kotlin. Function overloading. Function names in backticks
Contents
- 1. The concepts of function overloading
- 2. Examples of function overloading
- 2.1. Function Min(). Finding the minimum value between different numbers of numbers
- 2.2. Function ConcatStrings(). Concatenation of different number of lines
- 2.3. Function Average(). Calculating the average for a different number of integers and real numbers
- 2.4. Function AscSequence(). Determines if numbers form an ascending sequence
- 3. Functions in back quotes (backticks). The need to use
- 4. Examples of functions whose names are formed in backticks
- Related topics
Search other resources:
1. The concepts of function overloading
In the Kotlin programming language, a function can be overloaded. Function overloading means declaring a function with the same name multiple times but with different parameters.
In the most general case, function overloading looks like this:
fun FuncName(list_of_parameters1) : return_type1 { // ... } fun FuncName(list_of_parameters2) : return_type2 { // ... } ... fun FuncName(list_of_parametersN) : return_typeN { // ... }
here
- FuncName – the overloaded function name;
- list_of_parameters1, list_of_parameters2, list_of_parametersN – the list of parameters of the corresponding variant of the overloaded function named FuncName. Each parameter list must differ in the number or order of the parameter types;
- return_type1, return_type2, return_typeN – the type returned by the corresponding variant of the FuncName function. The compiler does not consider the return type to be a means of overloading a function.
It’s important. To overload the name of a function (function), it is necessary that the signature of its parameters is different in the declaration of each overloaded version of the function. Otherwise, the compiler will throw an error.
⇑
2. Examples of function overloading
2.1. Function Min(). Finding the minimum value between different numbers of numbers
An overloaded Min() function is declared that calculates the minimum value between two, three, four, five Double numbers.
// Overloading the function named Min // The function finds the minimum value between different numbers of numbers. // Between 2 numbers fun Min(num1:Double, num2:Double):Double { if (num1<num2) return num1 return num2 } // Between 3 numbers fun Min(num1:Double, num2:Double, num3:Double):Double { var min = num1 if (min>num2) min=num2 if (min>num3) min=num3 return min } // Between 4 numbers fun Min(num1:Double, num2:Double, num3:Double, num4:Double):Double { var min = num1; if (min>num2) min = num2 if (min>num3) min = num3 if (min>num4) min = num4 return min } // Between 5 numbers fun Min(num1:Double, num2:Double, num3:Double, num4:Double, num5:Double):Double { var min = num1 if (min>num2) min = num2 if (min>num3) min = num3 if (min>num4) min = num4 if (min>num5) min = num5 return min } fun main(args:Array<String>) { // Demonstration of calling the Min function // 1. The Min() function with 2 parameters is called var min = Min(2.5, 3.8) println(min) // 2. Call the Min() function with 3 parameters min = Min(2.6, 3.8, 1.2) println(min) // 3. Call the Min() function with 3 parameters println(Min(1.7, 3.2, 0.8, -3.5)) // 4. Call the Min() function with 3 parameters println(Min(7.2, 8.1, 6.5, 4.4, 2.8)) }
Program result
2.5 1.2 -3.5 2.8
⇑
2.2. Function ConcatStrings(). Concatenation of different number of lines
The example shows an overload of the ConcatStrings() function that concatenates a different number of strings. Implemented 3 function overloads.
// Overloading a function named ConcatStrings // Function concatenates different number of lines // 2 lines fun ConcatStrings(s1:String, s2:String):String { return s1+s2 } // 3 lines, a variant of a function with a single expression fun ConcatStrings(s1:String, s2:String, s3:String) : String = s1+s2+s3 // 4 lines, a variant of the function with a single expression // without explicitly specifying the type of the returned result fun ConcatStrings(s1:String, s2:String, s3:String, s4:String) = s1+s2+s3+s4 fun main(args:Array<String>) { // Demonstration of calling the ConcatStrings() function // 1. The ConcatStrings() function is called with 2 parameters var resStr:String resStr=ConcatStrings("Hello,", " world!") println(resStr) // 2. Calling the ConcatStrings() function with 3 parameters resStr = ConcatStrings("bestprog",".","net") println(resStr) // 3. Calling the ConcatStrings() function with 4 parameters println(ConcatStrings("Red-","Green-","Blue-","Yellow")) }
Program result
Hello world! bestprog.net Red-Green-Blue-Yellow
⇑
2.3. Function Average(). Calculating the average for a different number of integers and real numbers
The example shows an overload of the Average() function, in which the input parameters are a different number of different types of numbers of types Int, Double, Float, Long.
// Function Average() // Calculating the average for a different number of numbers // 1. A variant where the input parameters are Int, Double types fun Average(a:Int, b:Double) = (a+b)/2.0 // 2. Variant for the case Double, Int fun Average(a:Double,b:Int):Double { return (a+b)/2.0 } // 3. Variant for the case Long, Float fun Average(a:Long, b:Float):Double = (a+b)/2.0 // 4. Variant for the case Int, Float, Double fun Average(a:Int, b:Float, c:Double) = (a+b+c)/3 fun main(args:Array<String>) { // Demonstration of using the Average() function // 1. Types Int, Double val avg1 = Average(5, 6.8) println("avg1 = " + avg1) // 2. Types Double, Int val avg2 = Average(7.2, 8) println("avg2 = " + avg2) // 3. Types Long, Float val avg3 = Average(70L, 3.8f) println("avg3 = " + avg3) // 4. Types Int, Float, Double val avg4 = Average(10, 20.0f, 15.0) println("avg4 = " + avg4) }
The result of the program
avg1 = 5.9 avg2 = 7.6 avg3 = 36.900001525878906 avg4 = 15.0
⇑
2.4. Function AscSequence(). Determines if numbers form an ascending sequence
The example shows the declaration and use of 4 overloaded functions that determine whether multiple numbers form an ascending sequence.
// Function AscSequence() // Determine if numbers form an ascending sequence // 1. A variant where the input parameters are the types Int, Int, Int fun AscSequence(a:Int, b:Int, c:Int) : Boolean { return (a<b)and(b<c) } // 2. Variant for the case Double, Double, Double fun AscSequence(a:Double,b:Double, c:Double):Boolean { return (a<b)and(b<c) } // 3. Variant for the case Int, Int, Int, Int fun AscSequence(a:Int,b:Int,c:Int,d:Int):Boolean = (a<b)and(b<c)and(c<d) // 4. Variant for the case Float, Int, Double, Double fun AscSequence(a:Float,b:Int,c:Double,d:Double) = (a<b)and(b<c)and(c<d) fun main(args:Array<String>) { // Demonstration of using the AscSequence() function // 1. Case AscSequence(Int, Int, Int) println("5<6<8 => " + AscSequence(5, 6, 8)) // 2. Case AscSequence(Double, Double, Double) val res = AscSequence(2.8, 3.5, 1.7) println("2.8<3.5<1.7 => " + res) // 3. Case AscSequence(Int, Int, Int, Int) println("6<8<9<7 = " + AscSequence(6, 8, 9, 7)) // 4. Case AscSequence(Float, Int, Double, Double) println("7.5f<2<3.4<5.0 => " + AscSequence(7.5f, 2, 3.5, 5.0)) }
Program result
5<6<8 => true 2.8<3.5<1.7 => false 6<8<9<7 = false 7.5f<2<3.4<5.0 => false
⇑
3. Functions in back quotes (backticks). The need to use
In the Kotlin language, there is a means of specifying the name of a function in backticks – ` `. In this case, the function name can contain spaces or other non-standard characters.
In the most general use, the declaration of such a function looks like this:
fun `set_of_characters`(list_of_parameters):return_type {
// ...
}
here
- set_of_characters – a set of characters defining the name of the function. A space or other non-standard characters are allowed here;
- list_of_parameters – list of function parameters;
- return_type – the type returned by the function.
The call to the above function declaration could be something like this
`set_of_characters`(...)
here … means a list of arguments passed to the function.
Function names in backticks are necessary for the following reasons:
- to maintain Java compatibility. These might be cases where Java methods are called from code in a Kotlin language file. An example of this use is the use of various Java reserved keywords that cannot be used as names for functions in the Kotlin language (for example, the is keyword);
- making the use of functions more clear with the help of whole sentences, taken in back quotes.
⇑
4. Examples of functions whose names are formed in backticks
The following are examples of functions whose names are enclosed in backticks. Some of the function names are more eloquent in their purpose.
// Functions enclosed in backquotes `` // A function that calculates the sum of three parameters: a + b + c fun `a + b + c`(a:Int, b:Int, c:Int) = a+b+c // Function that returns a reversed string fun `abc = cba`(s:String) : String { var s2 : String = "" for (c : Char in s) s2 = c + s2 return s2 } // Function that returns the product of two numbers fun `a*b`(a:Double, b:Double) = a*b fun main(args:Array<String>) { // Call the function `a + b + c`() val res1 = `a + b + c`(6, 5, 9) println("res1 = " + res1) // Call the function `abc = cba`() val res2 = `abc = cba`("abcdef") println("res2 = " + res2) // Calling the function of multiplying two numbers of type Double println(`a*b`(9.5, 3.2)) }
Program result
res1 = 20 res2 = fedcba 30.400000000000002
⇑
Related topics
- Functions. The fun keyword. Refactoring a function. The return statement
- Function scope. Local scope. Default arguments in function parameters
- Functions with a single expression. Unit type. Functions that return the Unit type. Examples
- Named arguments of functions. The Nothing type. Overloading functions
⇑