# Optional function parameters

**URL:** <https://discuss.kotlinlang.org/t/optional-function-parameters/905>\
**Category:** Uncategorized\
**Created:** [February 19, 2013, 11:44am UTC](https://discuss.kotlinlang.org/t/optional-function-parameters/905 "2013-02-19T11:44:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddimitrov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ddimitrov/32/1519_2.png) [@ddimitrov](https://discuss.kotlinlang.org/u/ddimitrov)\
**Post date:** [February 19, 2013, 11:44am UTC](https://discuss.kotlinlang.org/t/optional-function-parameters/905/1 "2013-02-19T11:44:33Z")

</div>

I want to declare a function that takes an optional callback. Right now I am using:

fun foo(callback: () -\> Unit = { /\* nop \*/ } ) { … }

I assume the compiler would translate the empty function to empty private method, which can be optimized by the JIT. Is that really the case?

Just out of curiosity, Is there a syntax to specify nullable function type?

---

<div class="post-metadata">

**Author:** ![geevee](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/geevee/32/1475_2.png) [@geevee](https://discuss.kotlinlang.org/u/geevee)\
**Post date:** [February 19, 2013, 12:05pm UTC](https://discuss.kotlinlang.org/t/optional-function-parameters/905/2 "2013-02-19T12:05:36Z")

</div>

Unfortunately, it will be compiled to invocation of function class with empty implementation. We are planning to implement function inlining later, which will compile your code in more effective way.

Function type surely can be nullable, here is an example:

``

```kotlin
fun foo(callback: (() -> Unit)? = null) { … }
```

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [February 19, 2013, 12:43pm UTC](https://discuss.kotlinlang.org/t/optional-function-parameters/905/3 "2013-02-19T12:43:01Z")

</div>

Eventually, we will use method handles to implement lambda expression, but so far we just create a new (anonymous) class for every lambda expression

---

<div class="post-metadata">

**Author:** ![blieque](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/blieque/32/4981_2.png) [@blieque](https://discuss.kotlinlang.org/u/blieque)\
**Post date:** [April 2, 2018, 10:01pm UTC](https://discuss.kotlinlang.org/t/optional-function-parameters/905/4 "2018-04-02T22:01:43Z")

</div>

Forgive me if there is an obvious answer, but has the situation on this changed? Can we pass optional lambda parameters like we can with `param: String?`? Kotlin syntax is usually beautiful; `fun foo(() -> Unit = {})` isn’t the best.
