Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Methods and member functions in Kotlin

Tags:

kotlin

Kotlin Reference section on Functions states that 'A member function is a function that is defined inside a class or object':

enter image description here

I have also seen references to methods in Kotlin. Are methods and member functions the same, different or misuse of terms?

like image 599
CW Holeman II Avatar asked Sep 18 '25 07:09

CW Holeman II


2 Answers

AFAIK yes, same.

Kotlin is a bit different from Java because there are for example top level functions, and functions in companion objects, functions within functions, extension functions, etc. Everything in Kotlin starts with fun is called a (some kind of) function.

In Java, there are less types of functions. There are static (class-level) functions and object-level methods. The latter expression comes from OOP world, and Java is an OOP language, while Kotlin is not just that.

like image 136
C-Shark Avatar answered Sep 21 '25 16:09

C-Shark


Methods and member functions are synonyms. Much like object attributes are called member fields. Synonyms, however, have often little differences or shades of meanings. For instance, the word old can be expressed with synonyms ancient, venerable, elderly or matured. Each of the words describes the same thing with some different shades of meanings.

Kotlin is a powerful functional programming language. In functional languages, the term function is very important. Kotlin functions are much powerful than Java or C# methods. If we called them methods, this would imply similar to C# and Java. The term member function reflects the power and flexibility of the Kotlin function better.

like image 43
Jan Bodnar Avatar answered Sep 21 '25 17:09

Jan Bodnar