I think 'a function' is actually 'a method.'

They say Kotlin can use a function.
But strictly speaking, ‘function’ is ‘AnyClass’ method,’ isn’t it?

It might be the same question that is “Is a ‘literal’ an ‘Object?’”

No, in fact it’s the other way around. A method is always a function but not every function is a method.
A function is defined as a piece of code that you can call. Some definitions say that each function needs to return a value, but this is true for Kotlin anyways since each function at least returns Unit.

Then there are methods. A method is just a function that is part of a class.

fun thisIsAFunctionReturningUnit() = println("function")
fun thisIsAFunctionReturningAValue(a: Int, b: Int) = a + b

class Foo {
    fun thisIsAMethod() = println("method")  // and also a function
}

Some programing languages like java don’t allow for normal functions. In java you can only create methods, this is not true for kotlin.

2 Likes

Thanks that you explain me ‘function.’
I feel I should know about ‘scope.’
I may resolve that a function is not a method, though I don’t know.

I will continue to think about it.

A method is just a function that is part of a class.

A method doesn’t really have to be a part of a class, for example Common Lisp has methods that aren’t class members.

In the OOP model, there are objects that exchange messages and each object has a way, a method, of dealing with a message it received. So a method is a piece of code that is dynamically selected to handle an incoming message.

Java kind of degraded this original sense with static methods, which are really functions that have nothing to do with the class they find themselves in (except for the technicalities of name resolution and access control).

1 Like

The OP’s question is not very specific. He could be talking about the Kotlin language. He could be talking about the JVM. He could talking about programming languages in general.

Any discussion in this thread will inevitably derail into heated arguments because everyone is interpreting the OP in a different way.

1 Like

To be honest, everyone’s explanation is too difficult for me.
I will continue to learn Kotlin.

By the way, I don’t know if the question I asked is important in making applications
Anyway, I want to know about it.

Thanks.
To be honest, everyone’s explanation is too difficult for me.
I will continue to learn Kotlin.

By the way, I don’t know if the question I asked is important in making applications
Anyway, I want to know about it.

I think the main problem is that you used these very basic terms like “function” and “method” without realizing their true meaning, or the variation in the meaning they can have in different context. So basically, nobody got what you are really asking.

I don’t get at all what you meant by “is a ‘literal’ an ‘Object’”. The concept “literal” is most definitely not a concept subordinate to the concept “Object”.

1 Like

I mean I want to know Kotlin’s concept in order to develop an excellent blueprint.
I thought I needed to know Kotlin’s philosophy that is Kotlin’s object-orientation.
Well, anyway, I need time to understand basic things on Kotlin.
And then I should decide if I could have understood how to make a blueprint on Kotlin.

Don’t be stuck with terminology.
In Kotlin there is no methods. People call different things what they use to from other languages.
The term the “thing” called in the language may help with communication, but won’t affect the quality of your code.

BTW the literal is not a class. In Java the String literal is an instance of the class and all other literals are primitives.

My understanding -
They have some common things, like

  • both can be called/invoked
  • both can take some/none args and return some/none/void/unit values
    But they are different
  • methods are associated with objects/classes
  • functions are objects

There is no need to compare functions/methods in kotlin and java, as Kotlin code will be converted to Java. You can consider it as some syntax suger, like

  • lambda function as an object of java Function interface
  • fun as methods

You can do it that way. Or, you can think of Kotlin as an independent language, and don’t care about how it is implemented under the hood.

The internal implementation will differ on various target platforms anyway. For example, on Javascript target a Kotlin lambda will be translated to an anonymous Javascript function.

I believe, ultimately it will be beneficial to understand how it works under the hood on a particular platform, in order to find the best solutions on that platform. However, always keep in mind that Kotlin is a multi platform language, and some assumption might not hold true on all of those platforms.

All that being said, now I’m curious… Kotlin code will turn into Java under the hood won’t it?

What would a loose Kotlin function not inside any class look like after the conversion? Will a class be created with the .kt file name?

On JVM platform yes. On others, like Javascript, no.

It will be added as a member function to an implicit singleton class. This behavior can be adjusted with the @JvmStatic annotation.

1 Like

Yeah, I should have made explicit that I was referring to JVM, which is most likely the focus in OP’s rationalisation

Thanks, @fatjoe79. That solves my curiosity)) and I hope it also helps the OP’s understanding of how Kotlin works in the JVM

function is stateless, it’s not for behavior of a stateful object. I’m right?

I believe what you are referring to are “pure” functions. In Kotlin a function does not need to be pure. It can modify the state of an object.