# Syntax to pass a function as parameter

**URL:** <https://discuss.kotlinlang.org/t/syntax-to-pass-a-function-as-parameter/724>\
**Category:** Uncategorized\
**Created:** [October 18, 2013, 1:13pm UTC](https://discuss.kotlinlang.org/t/syntax-to-pass-a-function-as-parameter/724 "2013-10-18T13:13:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dittert](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dittert/32/1736_2.png) [@Dittert](https://discuss.kotlinlang.org/u/Dittert)\
**Post date:** [October 18, 2013, 1:13pm UTC](https://discuss.kotlinlang.org/t/syntax-to-pass-a-function-as-parameter/724/1 "2013-10-18T13:13:39Z")

</div>

How do I pass a function as parameter?

```
**class** PropTest { &nbsp;&nbsp; **val**  **prop**  **by**  **Delegates**.observable( **"initial"** , ::observe) &nbsp;&nbsp; **fun** observe(meta: PropertyMetadata, old: String, new: String) { &nbsp;&nbsp;} }
```

The code above doesn't compile when function observe is within class PropTest. It seems to be ok, when I move the function outside of the class. Why is this the case?

---

<div class="post-metadata">

**Author:** ![udalov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/udalov/32/5679_2.png) [@udalov](https://discuss.kotlinlang.org/u/udalov)\
**Post date:** [October 18, 2013, 2:14pm UTC](https://discuss.kotlinlang.org/t/syntax-to-pass-a-function-as-parameter/724/2 "2013-10-18T14:14:02Z")

</div>

This is because when a function is declared within a class, it's a member function and has an impllicit "this" parameter, thus it doesn't correspond to the expected parameter type of Delegates.observable, which takes 3 parameters.

What _could_ work is capturing current “this” in the context where it exists, passing a reference to the function closed on its receiver parameter. This would have other syntax and isn’t implemented yet (but is planned, see [KT-1183](http://youtrack.jetbrains.com/issue/KT-1183))

---

<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:** [October 18, 2013, 2:14pm UTC](https://discuss.kotlinlang.org/t/syntax-to-pass-a-function-as-parameter/724/3 "2013-10-18T14:14:45Z")

</div>

I guess the typechecker tell you that there is a type mismatch: a member of a class when used as a value is trated as an extension function, for it has one extra parameter: the receiving instance of the class

---

<div class="post-metadata">

**Author:** ![Dittert](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dittert/32/1736_2.png) [@Dittert](https://discuss.kotlinlang.org/u/Dittert)\
**Post date:** [October 18, 2013, 2:39pm UTC](https://discuss.kotlinlang.org/t/syntax-to-pass-a-function-as-parameter/724/4 "2013-10-18T14:39:41Z")

</div>

Thank you for the explanation. Is this something that will generally not be possible or was it just not implemented yet?

---

<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:** [October 18, 2013, 2:43pm UTC](https://discuss.kotlinlang.org/t/syntax-to-pass-a-function-as-parameter/724/5 "2013-10-18T14:43:56Z")

</div>

It will be possile.

---

<div class="post-metadata">

**Author:** ![ahmadalsharif.dev](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@ahmadalsharif.dev](https://discuss.kotlinlang.org/u/ahmadalsharif.dev)\
**Post date:** [July 15, 2017, 12:06pm UTC](https://discuss.kotlinlang.org/t/syntax-to-pass-a-function-as-parameter/724/6 "2017-07-15T12:06:02Z")

</div>

Yes please have such a workaround, perhaps C# like sytax.

```
void Fun1(String s) 
{
}
void Fun2(Action<String> action)
{
    action("Hello");
}
void Fun3()
{
    Fun2(Fun1);
}

```
