Sharing our technical trials in terms of Ubuntu software installs, Blogger hacks, Android and Kotlin learning.
Know about function parameter and return type – Kotlin
Know about function parameter and return type – Kotlin

Know about function parameter and return type – Kotlin

Function parameter and return type explained with examples

Before we start off with function parameter and return type know more about functions and implementation.

I. Function parameter

  • Information passed to a function are passed as parameters
  • Mentioned inside the function parentheses
  • Each parameter will have name and type specified
Example

Like numbers for the addition operation

fun addNumbers(x: Int, y:Int) 
{
     var z = x+y
}

a) Parameters can be of similar or different type

Example

Function with parameters of similar type

fun main (args:Array<String>)
{
    addNumbers(5,5) // function call
}
// function declaration
fun addNumbers(x:Int, y:Int) 
{
    var z = x+y
    println("Result $z")
}
Explanation
  • Parameters mentioned in the declaration are of type Int
  • While calling the function parameters passed are of type Int
  • Code runs successfully and prints result
Output

Result 10

Example

The function which takes different types of parameters

fun main (args:Array<String>)
{
    displayMessage("Happy New year",2024) //function call
}
// function declaration
fun displayMessage(x:String, y:Int)
{
    println("$x $y")
}
Output

Happy New year 2024

b) Type passed must match the declaration

Example
fun main (args:Array<String>)
{
    addNumbers('a','b') //function call
}
// function declaration
fun addNumbers(x:Int, y:Int) 
{
    var z = x+y
    println("Result $z")
}
Explanation
  • Function declared takes two parameters both of type Int
  • While calling the function parameters passed are of type character
  • Type mismatch results in compile time error
Output

Compilation errorThe character literal does not conform to the expected type Int

c) Parameter order should be maintained

Example
fun main (args:Array<String>)
{
    displayMessage(2024,"Happy New year") //function call
}
// function declaration
fun displayMessage(x:String, y:Int)
{
    println("$x $y")
}
Explanation
  • The two parameters mentioned in the declaration are of type String and Int
  • While calling the function type String should be passed first then Int
  • Because of the order mismatch compiler throws error
Output

Compiler error – Type mismatch

The integer literal does not conform to the expected type String

d) Number of parameters passed should match with declaration

Example
fun main (args:Array<String>)
{
    addNumbers() // function call
}
// function declaration
fun addNumbers(x:Int, y:Int) 
{
    var z = x+y
    println("Result $z")
}
Explanation
  • Function declared takes two int parameters
  • No parameters passed for the function call
  • Compiler throws error
Output

Compilation error

No value passed for parameter ‘x’

No value passed for parameter ‘y’

Example
fun main (args:Array<String>)
{
    displayMessage("Happy New year") //function call
}
// function declaration
fun displayMessage(x:String, y:Int)
{
    println("$x $y")
}
Explanation

A function declaration has two parameters

Function call has a single parameter passed -> Compiler throws an error

Output

Compiler error No value passed for parameter ‘y’

e) Variable declared inside the function are accessible only inside the function

Example
fun main (args:Array<String>)
{
    addNumbers(5,5) // function call
    print("Result $z")
}
// function declaration
fun addNumbers(x:Int, y:Int) 
{
    var z = x+y
}
Explanation
  • variable ‘z’ -> declared inside the function addNumbers
  • Accessing it outside the function, gives error
Output

Compile-time errorUnresolved reference: z

The IDE shows error even before compilation by marking the variable in red

f) Variables defined outside, cannot be accessed inside the function

Example
fun main (args:Array<String>)
{
    var x = 12
    addNumbers() // function call
}
// function declaration
fun addNumbers()
{
    var z = x+13
}
Explanation
  • Variable x is declared outside the function addNumbers()
  • Trying to access it inside the function, gives error
Output

Compile-time errorUnresolved reference: x

The IDE shows error even before compilation by marking the variable in red

g) variable defined inside the function must be initialised before use

Example
fun main (args:Array<String>)
{
    addNumbers(4,3) // function call
}
// function declaration
fun addNumbers(x:Int, y:Int)
{
    var z
    z = x+y
}
Explanation
  • The variable z declared in the function is getting used without getting initialised
  • Compiler throws error
Output

Compile-time error

h) The parameters passed to a function are of type val by default

Example
fun main (args:Array<String>)
{
    addNumbers(4,3) // function call
}
// function declaration
fun addNumbers(x:Int, y:Int)
{
    x = 5
    var z = x+y
}
Explanation
  • Parameters x and y passed to function are of type val
  • Value of val cannot be changed
  • In our example as we are trying to change the value of parameter x
  • Compiler throws error
Output

Compile-time error

II. Function return type

  • When you want the function to return something after successful execution
  • Return type is added to the declaration
Example
fun addNumbers(x: Int, y: Int) : Int
{
     var z = x+y
     return z
}

a) Value returned should match the declaration

Example
fun main (args:Array<String>)
{
    var value = addNumbers(5,1) //function call
    println("Result $value")
}
// function declaration
fun addNumbers(x:Int, y:Int) : Int
{
    var z = x+y
    return z
}
Explanation
  • The return type declared is of type Int
  • Value returned is of type Int
  • Code runs successfully printing result
Output

Result 6

Example
fun main (args:Array<String>)
{
    var value = addNumbers(5,1) //function call
    println("Result $value")
}
// function declaration
fun addNumbers(x:Int, y:Int) : Int
{
    var z = x+y
    return true
}
Explanation
  • The return type mentioned in declaration is Int
  • value returned is of type boolean therefore the compiler throws error
Output

The boolean literal does not conform to the expected type Int

b) Unit as return type

Example

If you don’t want the function to return any value

  • Skip the return type from the declaration
  • or mentioning the type as Unit
fun main (args:Array<String>)
{
    addNumbers(1,2) //function call

}
// function declaration
fun addNumbers(x:Int, y:Int) : Unit
{
    var z = x+y
    println("Result $z")
}
Output

Result 3

Example

Mentioning the type as Unit is equivalent to not returning any type

fun main (args:Array<String>)
{
    var value = addNumbers(1,2) //function call

}
// function declaration
fun addNumbers(x:Int, y:Int) : Unit
{
    var z = x+y
    return z
}
Explanation
  • Return type mentioned is Unit equivalent to no return type
  • In the example we are trying to return a Int value
  • Compiler throws error because of the mismatch
Output

Compiler error – Type mismatch

c) Function can return only one value

Example
fun main (args:Array<String>)
{
    var value = addNumbers(10,20) //function call
    println("Result $value")
}
fun addNumbers(x: Int, y:Int) : Int
{
    var z = x+y
    var z1 = x+1
    return z
    return z1
}
Explanation
  • keyword “return”, returns the control to where the method was called
  • any code written after the keyword “return”, will not get executed
  • As control is returned the first time return is encountered
  • The second “return” statement in the example does not get executed
Output

Result 30

That brings us to the end of this topic. Happy Coding 🙂