Kotlin Function Variations With Examples
Last Updated :
25 Oct, 2021
While defining a function in Kotlin we have many optional annotations. We will learn each of them one by one.
Defining a function in Kotlin:
Visibility modifier fun functionName (argument name: type name, ...): return type{
.....
// function body
.....
return value
}
Normally, It is the proper way for defining a function in Kotlin.
Example:
private fun gfg1( a : Int, b :Int ): Int {
var c : Int
c = a + b
return c
}
Now we will look at the variation of this function.
Visibility Modifier
In Kotlin, we while defining a function it is optional to specify the Visibility modifier. If it is not mentioned while defining, by default it is considered public.
Example:
fun gfg2( a : Int, b :Int ): Int {
var c : Int
c = a + b
return c
}
Both the gfg1( ) and gfg2( ) are doing the same job, but gfg1 is a private function and gfg2 is a public function as we haven't mentioned its Visibility modifier.
Single Expression Function
In Kotlin if the function has only one expression we can omit the curly braces.
Example:
fun gfg3( a: Int , b :Int ) : Int = a + b
Instead of curly braces, we using the' = ' sign for the function body. We can also omit the return type in the Single expression function.
fun gfg4( a: Int , b :Int ) = a + b
gfg4( ) and gfg3( ) is same.
Return Type
In Kotlin, while defining the function we need to declare the type of data that is returning in the function. If we are returning nothing then we have the option to specify 'unit' or if we do not specify anything, Kotlin will assume it as a function is returning nothing.
Example:
// declaring the return type as
// unit as it isn't returning anything.
fun gfg5( a : Int): Unit {
print(a)
}
// Here we have mention the return type,
// kotlin can infer the return type
fun gfg6( a : Int){
print(a)
}
both gfg5 ( ) and gfg6 ( ) is same.
Argument
When we have lots of arguments in a function, it is difficult to remember the position of each of them. So, here we can use Name Arguments, Here we need to specify the name along with the values to the function. We can change the sequence of the values, the value will assign to an argument that will have the same name as the value have.
Example:
fun main( ){
val res1 = gfg7( arg1 = 2, arg2 = 3) //using name argument
val res2 = gfg7( arg2 = 3, arg1 = 2 ) // sequence doesn't matter here.
}
fun gfg7( arg1 : Int, arg2 :Int ): Int {
return arg1 + arg2
}
Similar Reads
Kotlin partition() Method with Examples In this article, we are going to discuss how to split the original collection into pair of collections, because sometimes while coding, you wish that you could just split a list into sublists without going into the for and while loops. Kotlin provides you with a function just for this occasion. In t
4 min read
Kotlin Collection Write operations Collection Write operations are used to change the content of MutableCollection. MutableCollection is defined as the Collection with write operations like add and remove. Operations supported are as follows: Adding elements Removing elements and Updating elements Addition of elements - add() functio
4 min read
Working with Anonymous Function in Kotlin In Kotlin, we can have functions as expressions by creating lambdas. Lambdas are function literals that is, they are not declared as they are expressions and can be passed as parameters. However, we cannot declare return types in lambdas. Although the return type is inferred automatically by the Kot
3 min read
Kotlin extension function Kotlin provides a powerful feature called Extension Functions that allows us to add new functions to existing classes without modifying them or using inheritance. This makes our code more readable, reusable, and clean.What is an Extension Function?An extension function is a function that is defined
4 min read
Kotlin - Collection Operations Overview Kotlin provides a rich set of tools for working with collections like lists, sets, and maps. Whether you want to add, remove, sort, filter, or transform data, Kotlinâs standard library makes it easy and expressive.Two Ways Kotlin Defines Collection FunctionsCollection operations are declared in two
4 min read
Kotlin | Collection Transformation Kotlin standard library provides different set of extension functions for collection transformations. These functions help in building new collections from existing collections based on the rules defined by the transformation. There are different number of transformation functions:  MappingZippingA
5 min read
Kotlin infix function notation In this article, we will learn about infix notation used in Kotlin functions. In Kotlin, a function marked with infix keyword can also be called using infix notation means calling without using parenthesis and dot. There are two types of infix function notation in KotlinTable of ContentStandard libr
5 min read
Passing Variable Arguments to a Function in Kotlin There are a lot of scenarios in which we need to pass variable arguments to a function. In Kotlin, You can pass a variable number of arguments to a function by declaring the function with a vararg parameter. a vararg parameter of type T is internally represented as an array of type T ( Array<T
4 min read
Kotlin | apply vs with Kotlin :: apply In Kotlin, apply is an extension function on a particular type and sets its scope to object on which apply is invoked. Apply runs on the object reference into the expression and also returns the object reference on completion. It does not simply setting properties of course but do mu
2 min read
Function Literals with Receiver in Kotlin In Kotlin, functions are first-class citizens. It means that functions can be assigned to the variables, passed as an argument, or returned from another function. While Kotlin is statically typed, to make it possible, functions need to have a type. It exists and it is called function type and these
3 min read