# Global functions

**URL:** <https://discuss.kotlinlang.org/t/global-functions/14299>\
**Category:** Support\
**Created:** [September 17, 2019, 2:03pm UTC](https://discuss.kotlinlang.org/t/global-functions/14299 "2019-09-17T14:03:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bahaabauomyus](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@bahaabauomyus](https://discuss.kotlinlang.org/u/bahaabauomyus)\
**Post date:** [September 17, 2019, 2:03pm UTC](https://discuss.kotlinlang.org/t/global-functions/14299/1 "2019-09-17T14:03:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [September 17, 2019, 8:57pm UTC](https://discuss.kotlinlang.org/t/global-functions/14299/2 "2019-09-17T20:57:29Z")

</div>

In the first case `one()` and `two()` are [top-level functions](https://kotlinlang.org/docs/reference/functions.html#function-scope), which are available anywhere (or only within the file if they were `private`).

In the second case `one()` and `two()` are [local functions](https://kotlinlang.org/docs/reference/functions.html#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:

```run-kotlin
fun main() {
    fun two() {
        println(2)
    }

    fun one() {
        two()
    }

    one()
}

```

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [September 17, 2019, 11:24pm UTC](https://discuss.kotlinlang.org/t/global-functions/14299/3 "2019-09-17T23:24:53Z")

</div>

@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](https://www.markdownguide.org/extended-syntax/#fenced-code-blocks)
