title: Passing a function that requires parameters as a callback in Kotlin
body: <p>I am using Kotlin (for Android development) and I'm trying to pass a function to another function which I'd like to use as a callback. The code is very basic as this is just a test for now.</p>
  
  <p>Please note that, although you will probably wonder why I'm using a callback like this, it's just for test purposes. In my actual application I would want to assign the callback to a value and call it later on once an asynchronous method has completed. </p>
  
  <p>I cannot use co-routines etc... since this code will be used for a multi-platform solution, hence my interest in making a function callback.</p>
  
  <p><strong>My Kotlin Class that will receive the function (callback)</strong></p>
  
  <pre><code>class SampleApi {
      private var counter: Int = 0
  
      fun startCounting(initialValue: Int, counterCallBack: (resultVal: Int) -&gt; Unit) {
          counter = initialValue
  
          counter++
  
          counterCallBack(counter)
      }
  }
  </code></pre>
  
  <p>The above is a basic class that has a function <em>startCounting</em> which will receive an integer and a function. It will then call that function and pass in a value.</p>
  
  <p><strong>The calling code</strong></p>
  
  <pre><code>class MainActivity : AppCompatActivity() {
  
      private val sampleApi: SampleApi = SampleApi()
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          sampleApi.startCounting(5, {counterCallBack()})
      }
  
      private fun counterCallBack(counter: Int) {
          Toast.makeText(this, counter.toString(), Toast.LENGTH_SHORT).show()
      }
  }
  </code></pre>
  
  <p>The Sample code shown above contains the callback method (which is expecting to receive an integer), and contains the call to the startCounting method from the SampleApi class that is expecting to receive a function.</p>
  
  <p>The problem I have is this line:</p>
  
  <pre><code>sampleApi.startCounting(5, {counterCallBack()})
  </code></pre>
  
  <p>The error within Android Studio is due to the fact that a value is that the function is expecting an integer and hence I receive the error:</p>
  
  <pre><code>No value passed for parameter 'counter'
  </code></pre>
  
  <p>I tried to look at lambdas but didn't think that was the issue. I have searched to see if an answer to this already existed and, whilst helpful they didn't seem to consider the same use case as mine.</p>
  
  <p>Any help with this would be very much appreciated.</p>