Skip to content

Commit 36f133a

Browse files
author
Rajeev Kumar Singh
committed
functions
1 parent d6d7666 commit 36f133a

File tree

6 files changed

+136
-0
lines changed

6 files changed

+136
-0
lines changed

src/t9_functions/DefaultArguments.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package t9_functions
2+
3+
// Function with Default Argument
4+
fun displayGreeting(message: String, name: String = "Guest") {
5+
println("Hello $name, $message")
6+
}
7+
8+
// Function with a default parameter preceding a non-default parameter
9+
fun arithmeticSeriesSum(a: Int = 1, n: Int, d: Int = 1): Int {
10+
return n/2 * (2*a + (n-1)*d)
11+
}
12+
13+
fun main(args: Array<String>) {
14+
displayGreeting("Welcome to the CalliCoder Blog", "John")
15+
displayGreeting("Welcome to the CalliCoder Blog")
16+
17+
arithmeticSeriesSum(1, 10)
18+
}

src/t9_functions/Functions.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package t9_functions
2+
3+
// Function
4+
fun avg(a: Double, b: Double): Double {
5+
return (a + b)/2
6+
}
7+
8+
// Single Expression Function
9+
fun avgSingleExpression(a: Double, b: Double) = (a + b)/2
10+
11+
12+
// Unit returning Function
13+
fun printAverage(a: Double, b: Double): Unit {
14+
println("Avg of ($a, $b) = ${(a + b)/2}")
15+
}
16+
17+
fun main(args: Array<String>) {
18+
println(avg(4.6, 9.0))
19+
println(avgSingleExpression(4.6, 9.0))
20+
printAverage(4.6, 9.0);
21+
}

src/t9_functions/InfixNotation.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package t9_functions
2+
3+
data class ComplexNumber(val realPart: Double, val imaginaryPart: Double) {
4+
// Infix function for adding two complex numbers
5+
infix fun add(c: ComplexNumber): ComplexNumber {
6+
return ComplexNumber(realPart + c.realPart, imaginaryPart + c.imaginaryPart)
7+
}
8+
}
9+
10+
fun main(args: Array<String>) {
11+
// Infix function example
12+
13+
val c1 = ComplexNumber(3.0, 5.0)
14+
val c2 = ComplexNumber(4.0, 7.0)
15+
16+
// Usual call
17+
c1.add(c2) // produces - ComplexNumber(realPart=7.0, imaginaryPart=12.0)
18+
19+
// Infix call
20+
c1 add c2 // produces - ComplexNumber(realPart=7.0, imaginaryPart=12.0)
21+
}

src/t9_functions/LocalFunctions.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package t9_functions
2+
3+
// Local/Nested Function
4+
fun findBodyMassIndex(weightInKg: Double, heightInCm: Double): Double {
5+
if(weightInKg <= 0) {
6+
throw IllegalArgumentException("Weight must be greater than zero")
7+
}
8+
if(heightInCm <= 0) {
9+
throw IllegalArgumentException("Height must be greater than zero")
10+
}
11+
12+
// Nested function has access to the local variables of the outer function
13+
fun calculateBMI(): Double {
14+
val heightInMeter = heightInCm / 100
15+
return weightInKg / (heightInMeter * heightInMeter)
16+
}
17+
18+
return calculateBMI()
19+
}
20+
21+
fun main(args: Array<String>) {
22+
println(findBodyMassIndex(68.5, 170.0))
23+
}

src/t9_functions/NamedArguments.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package t9_functions
2+
3+
fun main(args: Array<String>) {
4+
// Function call with Named Arguments
5+
arithmeticSeriesSum(n=10)
6+
arithmeticSeriesSum(a=3, n=10, d=2)
7+
8+
// You can also reorder the arguments if you’re specifying the names
9+
arithmeticSeriesSum(n=10, d=2, a=3)
10+
11+
// You can use a mix of named arguments and position-based arguments
12+
// as long as all the position-based arguments are placed before the named arguments
13+
arithmeticSeriesSum(3, n=10)
14+
}

src/t9_functions/Varargs.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package t9_functions
2+
3+
// Vararg function
4+
fun sumOfNumbers(vararg numbers: Double): Double {
5+
var sum: Double = 0.0
6+
for(number in numbers) {
7+
sum += number
8+
}
9+
return sum
10+
}
11+
12+
// Vararg function with another parameter
13+
fun sumOfNumbers(vararg numbers: Double, initialSum: Double): Double {
14+
var sum = initialSum
15+
for(number in numbers) {
16+
sum += number
17+
}
18+
return sum
19+
}
20+
21+
fun main(args: Array<String>) {
22+
// Calling vararg function
23+
24+
sumOfNumbers(1.5, 2.0) // Result = 3.5
25+
26+
sumOfNumbers(1.5, 2.0, 3.5, 4.0, 5.8, 6.2) // Result = 23.0
27+
28+
sumOfNumbers(1.5, 2.0, 3.5, 4.0, 5.8, 6.2, 8.1, 12.4, 16.5) // Result = 60.0
29+
30+
31+
// If there are other parameters following the vararg parameter,
32+
// then the values for those parameters can be passed using the named argument syntax -
33+
sumOfNumbers(1.5, 2.5, initialSum=100.0)
34+
35+
36+
// Pass an array to the vararg function using spread operator
37+
val a = doubleArrayOf(1.5, 2.6, 5.4)
38+
sumOfNumbers(*a) // Result = 9.5
39+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy