Why `fun`?

Methods are declared with the fun keyword in Kotlin, whereas most other languages use def (e. g. Scala, Python) some use fn (e. g. Rust) or function (JavaScript). I wonder whether it was intended or at least a welcome sideeffect to have the word “fun” in the language :wink: Was it?

1 Like

It took me a while to get used to fun, and I would have preferred the straight-forward function. (If override and companion object aren’t considered too long, function shouldn’t either, even though it’s used more often.) One nice aspect (if you will) is that fun, val, and var all have the same length, but I don’t know if that played a role in the decision.

PS: JavaScript uses function.

1 Like

I don’t think there’s any word for defining functions that “most” languages use. We use “fun” because we like it - and yes, we do know what the word means in English.


I think there is some masterful psychological manipulation going on here. Every time you write a function in Kotlin they want you to believe you’re having fun.

I mean, think about it… all the awesome stuff they’ve done with the language; it’s like they want you to enjoy yourself while programming. Having fun while programming, absurd.


Should have used function, func or fn, just saying, it would have been a better larger audience choice.

1 Like

SML is using fun. The audience coming from there has no quantitative relevance… Quality over quantity in this case :joy:

1 Like

I heard in the recording of some conference that fun is used because it’s 3 letters long like var and val. :smiley:

1 Like

Ok, what about packages that use the word fun? I’m running into a problem right now since my Android app contains the word fun, as in package fun.domain.myApp. How do we get around this obvious problem?

What is your problem exactly? As long as you use IntelliJ/Android Studio, you should not even notice this problem, because IDE solves it for you automatically - by enclosing a reserved word in backticks:

package `fun`.domain.myApp
import `fun`.domain.myApp.MyClass

ML and Erlang both use “fun” keyword (Erlang for lambdas), Go uses “func”. So it is not something new, and superior to alternatives you mentioned.

  • Fn - cryptic
  • Def - unspecific to functions (define what?)
  • Function - overly verbose

Am I the only one they should have called it methods? By why not leave it out altogether like java does?

Why do this:

fun sum(a: Int, b: Int): Int {
    return a + b

when you can do this:

 sum(a: Int, b: Int): Int {
    return a + b

It is more explicit. Same for val/var. i personally never liked that syntax introduced I believe by C, where we declare fields and methods and we don’t even say what we do.


That would cause ambiguity. Take a look at the following:

foo() {}

Are we defining a method with no parameters and return type Unit? Or are we invoking a method that takes a trailing lambda?


C/C++ sort of does away with fun or any named keyword.

That leads to this Most vexing parse - Wikipedia

fun() {}

Would be a constructor in that case…

Well, for one thing, not all funs are methods (i.e. members of a class or object); some are top-level functions, some are local functions, and some are anonymous functions.

(And since you need syntax for those, it makes sense to reuse it for methods too, rather than adding new syntax for that case.)

The other way around is the same… That’s why I find it confusing (personally) that a fun acts like a method.
For me that is a clear indication if a method has access to the objects variable or not. All the function extensions in that care are functions, and the other are methods. This stays closer to OOP.

A function doesn’t need any object and is independent, while the method is a function, which is linked with any object

A method is a function — a member function, which is a function that belongs to a class or object, and operates on (an instance of) that. The Kotlin docs specifically call them ‘member functions’; that’s also the official name for them in languages such as C++.

(I’m not sure where your quote is from, but its terminology clearly isn’t applicable to languages like Kotlin.)

1 Like

What about Java? There we have instance methods and static methods. So yet another way around where even functions are methods. I don’t think there is a single, correct naming convention. I usually read “method” and “function” as pretty much the same thing and only if I see “instance/member function/method”, “static/global/top-level function/method” or e.g. “pure function”, I assume this is more specific.

1 Like