1- functions
1- functions
For example: If we have to compute the sum of two numbers then define a fun
sum().
We can call sum(x, y) at any number of times and it will return sum of two
numbers. So, function avoids the repetition of code and makes the code more
reusable. In Kotlin, there are two types of function-
Kotlin
Output:
Kotlin
Output:
In Kotlin, function can be declared at the top, and no need to create a class to
hold a function, which we are used to do in other languages such as Java or
Scala. Generally we define a function as:
Kotlin
Kotlin
Output:
Trending
TheNowmultiplication
Data Structures ofAlgorithms Topic-wise
two numbers is: Practice
15 Python Machine Learning Data Science J
Kotlin
}
fun main(args: Array<String>) {
val name = "Praveen"
val rollno = 25
val grade = 'A'
student(name,grade,rollno)
student("Gaurav",'B',30)
}
Output:
1. Overhead: Functions can increase the size of your code and increase the
amount of memory required to execute it, especially if you have many
functions.
2. Debugging: Functions can make debugging more difficult if you have
complex logic inside your functions, especially if you have multiple functions
that call each other.
Similar Reads
Kotlin | Lambdas Expressions Kotlin Higher-Order
and Anonymous Functions Functions