Newbie question: using nested functions

For readability, easy navigation and decreasing of repeated pieces of code I would like to use nested functions without argument, for example like this

function onNote(event)
tonal_playback_block()
noise_playback_block()
end 

function tonal_playback_block()
piece of code #1
end 

function noise_playback_block()
piece of code #2
end 

The problem is that tonal_playback_block() doesn’t see global variable “event” inside itself and can’t work with it. Is there any workaround rather than relaying event via a local auxiliary variable?

Well, event isn’t global variable. Unless you assign it to one. But the easiest way would be to pass the event as argument to your functions. And probably also safer. To avoid overwriting global variables by mistake.

function onNote(e)
    tonal_playback_block(e)
    noise_playback_block(e)
end

function tonal_playback_block(e)
    print(e)
end 

function noise_playback_block(e)
    print(e)
end

So there is no way to copy and paste pieces of script independently of any argument? For example in Kontakt “call my_function” uses function “locally” without awareness of outer system/global variables. However implementation of my_function() works with global variables and servers as a mere copying of script blocks.

You could assign the event to global variable first.

function onNote(e)
    event = e
    tonal_playback_block()
    noise_playback_block()
end

function tonal_playback_block()
    print(event)
end 

function noise_playback_block()
    print(event)
end

But I wouldn’t do this. If you use wait() then by the time you do something with the event (global variable) it may already be overwritten by new note.