Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - FUNCTION ARGUMENTS


Say, you want to pass some information to a Function.


For example, if you want a Function to add two numbers. You need to pass those two numbers to the Function.


Say, in the below example, you want the add Function to add two numbers for you. And you have passed those numbers in between the brackets () of the add Function(i.e. firstNumber and secondNumber).


And those elements inside the brackets () of the add Function is called as Parameters and the actual values passed to them are called as Arguments.


Example :



fun main() {

    var firstNum: Int = 5
    var secondNum: Int = 4
    var value: Int = add(firstNum, secondNum)
    println("The added result is : "+value)
}

fun add(firstNumber: Int, secondNumber: Int): Int {
    var result: Int = firstNumber + secondNumber
    return result
}


Output :



  The added result is : 9

So, the variables inside the add Function is called as Parameter.

java_Collections

And the actual values (i.e. 5 and 4) to be sent to the Function are the Arguments.


Throughout the tutorial we will be calling the Parameters as Arguments to avoid unnecessary confusion.


Now, what if, you have defined a Function with two Argument but made the Function call with one argument.


What if there is an Argument mismatch?


Now, what if, you have defined a Function with two Argument but made the Function call with one argument.


Example :



fun main() {

    var firstNum: Int = 5
    var secondNum: Int = 4
    var value: Int = add(firstNum, secondNum)
    println("The added result is : "+value)
}

fun add(firstNumber: Int, secondNumber: Int, thirdNumber: Int): Int {
    var result: Int = firstNumber + secondNumber + thirdNumber
    return result
}


Output :



  No value passed for parameter 'thirdNumber'

So, in the above code we have the add Function with three Arguments.


fun add(firstNumber Int, secondNumber Int, thirdNumber Int) Int {
java_Collections


But when the add Function is called, it is called with two Arguments only.

java_Collections

And there is a mismatch of Arguments. And we end up with the below error.


No value passed for parameter 'thirdNumber'

Let us see in the next example, how to fix it.


Fixing Argument mismatch with default arguments in function definition


To solve the problem of Argument mismatch, we use default arguments in function definition.


Let us see in the below example.


Example :



fun main() {

    var firstNum: Int = 5
    var secondNum: Int = 4
    var value: Int = add(firstNum, secondNum)
    println("The added result is : "+value)
}

fun add(firstNumber: Int, secondNumber: Int, thirdNumber: Int = 11): Int {
    var result: Int = firstNumber + secondNumber + thirdNumber
    return result
}


Output :



  The added result is : 20

So, in the previous example, we got the error as,


No value passed for parameter 'thirdNumber'

And, to avoid the Argument mismatch problem, we have used default arguments in function definition.


Placing default arguments in function definition.

java_Collections

So, we have placed the default value, 11 for the third argument


Now, although the function is called with two parameters,


var value: Int = add(firstNum, secondNum)

With values, 5 and 4.


var firstNum: Int = 5
var secondNum: Int = 4
java_Collections

java_Collections

The function add() is called. And firstNum is passed to firstNumber and secondNum to secondNumber.


So, what happens to the third argument?


Well! It gets initialised with the default value 11.

java_Collections