Home > front end >  Whats the difference between these two function callings in kotlin?
Whats the difference between these two function callings in kotlin?

Time:12-02

The first way

fun add(a: Int, b: Int): Int {
    return a   b
}

fun main() {
    print(add(a = 2, b = 3))
}

The second way

fun add(a: Int, b: Int): Int {
    return a   b
}

fun main() {
    print(add(2, 3))
}

The end result of the two functions is the same but i was wondering if there is any internal difference between the two ways of function calling.

CodePudding user response:

In the first case you're explicitly stating to which field of the add() method constructor you're assigning the value. In this way the order in which you put the values doesn't matter, as long as each value is explicitly assigned to a parameter. For example, in this case you can also write:

print(add(b=3, a=2))

still works.

As instead, in the second way you are forced to follow the order in which the fields are written in the method implementation (the first value is implicitly assigned to a, the second to b and so on)

CodePudding user response:

For this example there is no difference, because you are adding the arguments in order

add(a=2,b=3): here a is going to take 2 and b is going to take 3

add(2,3): and here a is the first argument so it's going to take the first passed argument which is 2 and the same for b

But here is the difference (a b == b a so I add minus function to see the difference because a - b != b - a) :

fun minus(a : Int,b:Int):Int{
    return a-b;
}
fun main()
{
    print(minus(a=2,b=3)) // a = 2, b = 3 -> a - b = 2 - 3 = -1
    print(minus(b=2,a=3)) // a = 3, b = 2 -> a - b = 3 - 2 = 1
    print(minus(2,3)) // a = 2, b = 3 -> a - b = 2 - 3 = -1
}

So if you add minus(a=2,b=3) you are saying that a is going to take 2 and b is going to take 3,

and here minus(2,3) you are saying that the first parameter (a) is going to take 2 and the second parameter (b) is going to take 3

But let's say for some reason you change the order of the parameters of your function:

fun add(b : Int,a:Int):Int{
    return a b;
}

Now if you add minus(a=2,b=3) you are saying that a is going to take 2 and b is going to take 3 so nothing changes for this case and your code will work fine.

But here minus(2,3) you are saying that the first parameter (b) is going to take 2 and the second parameter (a) is going to take 3 so you will not get the same result before changing the order of the parameters of the function. So adding parameter name when you call a function is a best practice to say that you want this value for that exact argument.

Also there's other example, let's say that you have a function that has default values:

fun test(a : Int = 10, b:Int = 5):Int { return a b; }

So the you can call it like that test() without passing any argument, but let's say that you want to change only b to 15, if you write test(15), a is going to take 15 not b so here you need to specify that the 15 is for b: test(b = 15)

CodePudding user response:

There is no difference, the only difference is readability by using named arguments in the first example.

The fun thing about using named arguments when calling your method is that you can also change the order or even leave out some of the values if they are default i.e:

fun add(a :Int,b: Int):Int {
    return a b;
}

Can also be written with a default value like so:

fun add(a :Int = 2,b: Int = 3, c: Int = 4):Int {
    return a b c;
}

Now you can skip some of the values like so:

fun main() {
    print(add(a = 2, c = 3))
}

// So we did 2   3   3
// prints 8
// Notice we skipped b

  • Related