What is the difference between :
fun one () {
two ()
}
fun two () {
println ( 2 )
}
fun main () {
one ()
}
And
fun main () {
fun one () {
two ()
}
fun two () {
println ( 2 ) }
one ()
}
And why we get an error when we put the one fun and two fun inside the main function
In the first case one()
and two()
are top-level functions, which are available anywhere (or only within the file if they were private
).
In the second case one()
and two()
are local functions, which are only available within the function they are declared in, main()
in your case.
As for the error you are getting it is caused by how local functions are implemented in the JVM, but to sum up you have to declare a local function before using it. Something like this:
fun main() {
fun two() {
println(2)
}
fun one() {
two()
}
one()
}
@bahaabauomyus
It would be nice if you could format your code properly. You can use 3 tick marsk ` in a row at the beginning and end to format it. Here is a link with more options: Extended Syntax | Markdown Guide
1 Like