Function As Parameter In Kotlin — (Easy Explanation)

JAVING
Javarevisited
Published in
3 min readOct 8, 2022

--

A complex function machine

If you come from Java like myself, using functions as parameters would likely be something new for you. The closest we have in Java to that would be declaring a functional interface as a method parameter. Programming to the interface is of course a good practice but beyond that, Java does not allow to use/pass methods as parameters of others methods.

Let’s have a look at an example. The code below declares a function that adds two numbers and then a caller function uses it.

calling a function in Kotlin

There’s nothing wrong really with the code above. But using functions as parameters can allow us to have more flexibility because it allows us to extract a partial implementation and re-utilize it somewhere else. Also would allow us to easily replace the function for another function if we wanted to. Have a look at this next bit of code

Function as a parameter

What before was the sum() function now is the mathFunction() this function takes the same two parameters a and b but also takes a function.
The advantages are multiple:

  • The parameter mathFunc represents an interchangeable piece of implementation of the function mathFuncion()
  • The calling function can pass a different implementation if it wants
  • The code function is more granular, re-usable and easy to maintain

Syntax
Now I am going to explain a bit more in depth the syntax and also I will show some variations that are also possible.

Let’s start first with the declaration of mathFunction().

Function that uses another function as parameter

There’s nothing special about the two first parameters a and b, but the function parameter is a little bit different. The reason for this is that, the third parameter(mathFunc) is a function. So basically what we are doing is define the blueprint of the function that would have to be passed.

Function parameter
Meaning explanation

Whatever function is passed will have to take two Int inputs and will have to return Int.

Now that we understood that, let’s look at the caller:

The actual function is on the right side of the image above. Everything before the ‘=’ sign is just part of the declaration of the value. This is not the only way we can pass this function, actually we can also pass it as an anonymous function without declaring a val.

Passing anonymous function

That looks nicer. One last thing. Notice the popup message on my editor?
If the function parameter is the last in a series of parameters, the convention is to take it out of the parenthesis. This is how it would look.

Calling Function According To Convention

Thanks for your time reading I hope you liked this article and/or found it useful. If you enjoyed this kind off content, I would very much appreciate if you give me some claps, follow and share on your social media. I am aiming to publish 1 article monthly.

--

--

JAVING
Javarevisited

The present continuous form of “to program in Java”.