Skip to content

Functions inside functions!

Julius Paffrath edited this page Dec 8, 2016 · 3 revisions

jask supports a very cool feature called inside function defines. This allows you to define a function inside a function! See the following code:

function myFunction()

    function insideFunction()
        printLine("I am a function defined inside!")
    end

    insideFunction()
end

The defined function insideFunction lives only inside myFunction and can't be called outside. In addition insideFunction can't access variables through the access operator outside of the wrapping function. To avoid this, have a look at the following code:

store 100 in outsideVar

function myFunction()
    store !outsideVar in insideVar

    function insideFunction()
        printLine(!insideVar)
    end

    insideFunction()
end

This looks a little bit puzzling but let me explain it:

  • first, the variable outsideVar is stored with the value 100
  • in myFunction a new variable is stored called insideVar and get's the value from outsideVar
  • in the end insideFunction access insideVar through the access operator