Define a new function

define  name (symbol)

Allows you to group a bunch of code and give it your own name for future re-use. Functions are very useful for structuring your code. They are also the gateway into live coding as you may redefine a function whilst a thread is calling it, and the next time the thread calls your function, it will use the latest definition.

Note, it is not recommended to start a function name with a capital letter if it takes no parameters.

Introduced in v2.0

Examples

# Example 1


  define :foo do
    play 50
    sleep 1
  end

 
  foo

 
 
  3.times do
    foo
  end


# Define a new function called foo
 
 
 
 
 
# Call foo on its own
 
 
# You can use foo anywhere you would use normal code.
# For example, in a block:
 
 
 



# Example 2


  define :play2 do |x|
    play x, release: 2
  end

 
  play2 42


# Define a new function called play2, taking one parameter
 
 
 
 
# Call play2, passing in a value for the parameter