Sushil
Sushil

Reputation: 5531

Order of Functions Kotlin

I have been learning android development with kotlin for the last 2 weeks by downloading open source projects from github and modifying the source code. This was the project that I was studying for the past few days. When I was going through the MainActivity.kt file, I came across this function (Line 69 in MainActivity.kt):

fun appendVal(string: String, isClear: Boolean) {
        if (isClear) {
            placeholder.text = ""
            answer.text = ""
//            placeholder.append(string)
        } else {
            placeholder.append(string)
        }
    }

Surprisingly, this function was below the onCreate function. But if I try the same with python (placing my custom function below main()), it would return an error. Here is a demo:

if __name__ == '__main__':
    print(add(1,2))

def add(a,b):
    return a+b

Output:

NameError: name 'add' is not defined

Why doesn't the same occur in kotlin? Does the order of functions matter in kotlin? Any help would be appreciated. Thanks!

Upvotes: 0

Views: 603

Answers (2)

ycannot
ycannot

Reputation: 1947

Order of functions doesn't matter in Kotlin or Java, but matters in Python. I advise you to locate your python functions to the top of your python script.

Upvotes: 3

Martin Zeitler
Martin Zeitler

Reputation: 76669

Theses are methods and not just some loose functions. And Kotlin is object-orientated and not sequential code. So the order of methods doesn't matter the least, rather the global/local scope.

Upvotes: 3

Related Questions