# How to pass a function as parameter to another?

**URL:** <https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848>\
**Category:** Uncategorized\
**Created:** [April 20, 2013, 1:31pm UTC](https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848 "2013-04-20T13:31:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![m.h.shams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/m.h.shams/32/1482_2.png) [@m.h.shams](https://discuss.kotlinlang.org/u/m.h.shams)\
**Post date:** [April 20, 2013, 1:31pm UTC](https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848/1 "2013-04-20T13:31:25Z")

</div>

given funciton foo :

```kotlin
fun foo(m: String, bar: (m: String) -> Unit) {
    bar(m)
}

```

we can do:

``

```kotlin
        foo(“a meesage”, { println(“this is a message: $it”) } )
  //or
  foo(“a meesage”)  { println(“this is a message: $it”) }
  
```

  
now lets say, we have following function:

```kotlin
fun buz(m: String) {
   println("another message: $m")
}
```

is there a way, i can pass "buz" as a parameter to "foo" ? something like:

```kotlin
foo("a message", buz)
```

---

<div class="post-metadata">

**Author:** ![steamstreet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/steamstreet/32/1529_2.png) [@steamstreet](https://discuss.kotlinlang.org/u/steamstreet)\
**Post date:** [April 20, 2013, 9:05pm UTC](https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848/2 "2013-04-20T21:05:27Z")

</div>

I believe the only current way to do this is to change your definition of buz and set it as a property:

> val buz: (String)-\>Unit = { m-\> &nbsp;&nbsp;println("another message: $m") }

However, there has been some discussions about this here, and I think supporting passing named functions like that makes sense. I'd also like to be able to do this:

> class Zoo { &nbsp;&nbsp;fun buz(m: String) { } }
> 
> val z = Zoo()  
> foo(“a message”, z.buz)

  
This would allow the callback to have the context of an object. You can currently do this by wrapping it:

> val z = Zoo() foo("a message", {m-\> z.buz(m)})

---

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [April 21, 2013, 2:46am UTC](https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848/3 "2013-04-21T02:46:23Z")

</div>

There's an old issue filed about this:&nbsp;&nbsp;[http://youtrack.jetbrains.com/issue/KT-1183?projectKey=KT](http://youtrack.jetbrains.com/issue/KT-1183?projectKey=KT)

Vote!

---

<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:** [April 22, 2013, 3:10pm UTC](https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848/4 "2013-04-22T15:10:59Z")

</div>

We are currently working on it. The syntax will be the following:

``

```kotlin
foo(“a message”, myPackage::buz) // myPackage is a name of the package
foo(“a message”, ::buz) // buz will be looked up in current scope, works if “buz” is declared in current package or imported
```

---

<div class="post-metadata">

**Author:** ![m.h.shams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/m.h.shams/32/1482_2.png) [@m.h.shams](https://discuss.kotlinlang.org/u/m.h.shams)\
**Post date:** [April 22, 2013, 3:14pm UTC](https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848/5 "2013-04-22T15:14:58Z")

</div>

great. how about this case ?

```kotlin
myobject::buz  // buz belong to a class and myobject is an instance of such class.
```

would that be supported too ?

---

<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:** [April 22, 2013, 3:47pm UTC](https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848/6 "2013-04-22T15:47:53Z")

</div>

In the nearest future, we'll support only syntax with class or package before "::" symbols. Mentioned use case will be covered by standard library function taking object and member function, returning function with "captured" receiver object, used like this:

```kotlin
bind(myobject, buz)
```

Later, we are planning to support syntax you offer (with expression before “::”), but exact syntax may appear a bit different.

---

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [April 22, 2013, 5:55pm UTC](https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848/7 "2013-04-22T17:55:45Z")

</div>

Fantastic, this would open the door for some limited but potentially very useful dependent typing.

---

<div class="post-metadata">

**Author:** ![MarioArias](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/marioarias/32/1488_2.png) [@MarioArias](https://discuss.kotlinlang.org/u/MarioArias)\
**Post date:** [April 24, 2013, 1:33pm UTC](https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848/8 "2013-04-24T13:33:10Z")

</div>

Evgeny:

This syntax will let us just pass the member function as a parameter to other function or will return it as a function?. I mean, this will be possible?:

```kotlin
val f:(Foo) -> Bar = myPakage::buz
```

Thanks

---

<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:** [April 24, 2013, 1:38pm UTC](https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848/9 "2013-04-24T13:38:00Z")

</div>

This will be a syntactic sugar for trivial function literal which passes all its parameters to referred function. Answering your questions, yes it will return function expression, and your sample will work.

---

<div class="post-metadata">

**Author:** ![MarioArias](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/marioarias/32/1488_2.png) [@MarioArias](https://discuss.kotlinlang.org/u/MarioArias)\
**Post date:** [April 24, 2013, 1:47pm UTC](https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848/10 "2013-04-24T13:47:46Z")

</div>

Cool, if you combine it with [Function composition provided by funKTionale](https://github.com/MarioAriasC/funKTionale/wiki/Function-composition) you could do nice things like this:

```kotlin
val f:(Foo) -> Bar = myPackage::buz forwardCompose anotherPackage::otherBuz
```

---

<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:** [April 24, 2013, 2:10pm UTC](https://discuss.kotlinlang.org/t/how-to-pass-a-function-as-parameter-to-another/848/11 "2013-04-24T14:10:23Z")

</div>

Yes.
