home/ atoms/ sonic-pi-define-function

define :name do ... end creates reusable code blocks that persist and can be redefined between runs

define :name do … end creates a named function callable anywhere by writing its name. Unlike variables, functions defined with define persist across runs — the previous definition remains until overwritten. Parameterised functions use |param| pipes. This enables live coding by separating sound definition from the loop structure: redefine :my_sound while a named thread keeps calling it, and the loop picks up the new behaviour transparently.

Examples

define :beat do sample :bd_haus sleep 0.5 end

live_loop :main do beat end

Assessment

Define a function :riff that plays a 4-note pattern, call it from a live_loop, then redefine it mid-run. Explain why the loop picks up the new definition without stopping.

“teach your functions to accept arguments”
corpus · sonic-pi-built-in-tutorial · chunk 13