<p>In this article, you will learn about infix notation to call a function in Kotlin with the help of examples.</p>
<p>Kotlin supports function calls of a special kind, called <span style="color: #008000;"><strong>infix</strong></span> calls.</p>
<p>In Kotlin, functions marked with <span style="color: #008000;"><strong>infix</strong></span> keyword can also be called using <span style="color: #008000;"><strong>infix notation</strong></span> means calling without using parenthesis and dot.</p>
<p>Before you learn how to create a function having infix notation, let&#8217;s explore some of the commonly used built-in infix functions.</p>
<p>Following are a few common examples of infix notations in Kotlin:</p>
<h4><span style="color: #000080;"><strong>Infix Notation Example &#8211; Creating a Map</strong></span></h4>
<pre>fun main(args: Array) {
<strong><span style="color: #008000;">
 //to() is an infix function</span></strong>
<strong><span style="color: #008000;"> //used while working with Map</span></strong>
 val myMap = mapOf(1 <span style="color: #0000ff;"><strong>to</strong></span> "one", 2 <strong><span style="color: #0000ff;">to</span></strong> "two", 3 <span style="color: #0000ff;"><strong>to</strong></span> "three")
 for (value in myMap.values) {
 println(value)
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>one
two
three</pre>
<h4><span style="color: #000080;"><strong>Infix Notation Example &#8211; Boolean Operators ( and, or )</strong></span></h4>
<pre>fun main(args: Array) {

 val a = true
 val b = false
 var result: Boolean

<span style="color: #008000;"><strong> //or, and are infix functions
</strong><strong> //performs logical 'and','or' operations between the two values</strong></span>
 result = a <strong><span style="color: #0000ff;">or</span></strong> b <strong><span style="color: #008000;">// a.or(b)</span></strong>
 println("result = $result")

 result = a <strong><span style="color: #0000ff;">and</span></strong> b <strong><span style="color: #008000;">// a.and(b)</span></strong>
 println("result = $result")
 
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>result = true
result = false</pre>
<h4><span style="color: #000080;"><strong>Infix Notation Example &#8211; String.matches()</strong></span></h4>
<pre>fun main(args: Array) {
 val regex = Regex("[tT]rue|[yY]es")
 val str = "Yes"

<span style="color: #008000;"><strong> //matches is an infix function</strong></span>
<span style="color: #008000;"><strong> //used while working with String</strong></span>
<span style="color: #008000;"><strong> //returns `true` if the string matches the given regular expression.</strong></span>
 val result = str matches regex <strong><span style="color: #008000;">//str.matches(regex)</span></strong>
 print("result = $result")
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>result = true</pre>
<h4><span style="color: #000080;"><strong>Infix Notation Example &#8211; Range Operators (until, downTo, step)</strong></span></h4>
<pre>fun main(args: Array) {

<span style="color: #008000;"><strong> //until, downTo, step are infix functions </strong></span>
<span style="color: #008000;"><strong> //used while working with Range</strong></span>
 print("for (i in 1 until 5) print(i) = ")
 for (i <span style="color: #000000;">in</span> 1 <span style="color: #0000ff;"><strong>until</strong></span> 5) print("$i ")

 println()
 
 print("for (i in 10 <span style="color: #000000;">downTo</span> 1 <span style="color: #000000;">step</span> 3) print(i) = ")
 for (i <span style="color: #000000;">in</span> 10 <span style="color: #0000ff;"><strong>downTo</strong></span> 1 <strong><span style="color: #0000ff;">step</span></strong> 3) print("$i ")
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>for (i in 1 until 5) print(i) = 1 2 3 4 
for (i in 10 downTo 1 step 3) print(i) = 10 7 4 1</pre>
<h5></h5>
<h4><span style="color: #000080;"><strong>Creating a function with infix notation</strong></span></h4>
<p>You can make a function call in Kotlin using infix notation if the function</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<ul>
<li>is a member function or extension function.</li>
<li>has a single parameter.</li>
<li>marked with <span style="color: #008000;"><strong>infix</strong></span> keyword.</li>
<li><span style="color: var(--color-text);">The parameter must not accept <span style="color: #008000;"><strong>variable number of arguments</strong></span> and must have no <span style="color: #008000;"><strong>default value</strong></span>.</span></li>
</ul>
<h5></h5>
<h4><span style="color: #000080;"><strong>Example: User-defined function with infix notation</strong></span></h4>
<pre>class Math {
 <span style="color: #008000;"><strong>//user defined infix member function</strong></span>
 infix fun cube(n: Int): Int {
 val num = n * n * n
 return num
 }
}

fun main(args: Array) {
 val math = Math()
 <strong><span style="color: #008000;">//calling cube() without using infix notation
 //math.cube(5)

 //calling cube() using infix notation</span></strong>
 val result = math cube 5 
 print("The cube of number 5 is: " + result)
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>The cube of number 5 is: 125</pre>
<h5><span style="color: #0000ff;"><strong>Example explained:</strong></span></h5>
<ul>
<li><span style="color: #0000ff;"><strong>cube()</strong></span> is a member function of class <span style="color: #0000ff;"><strong>Math</strong></span>, accepts only one parameter, and marked with <span style="color: #008000;"><strong>infix</strong></span> keyword.</li>
<li>Now cube() is an infix function we can call it by using infix notation like this: <strong style="color: var(--color-text);"><span style="color: #008000;">math cube 5 </span></strong><span style="color: var(--color-text);">which is equivalent to</span><span style="color: #008000;"><strong> math.cube(5)</strong></span><span style="color: var(--color-text);">.</span></li>
</ul>


