Defining Functions, Syntax?

I’ve been using the syntax for defining functions in the tutorial e.g.

define :foo do |x,y|
  x+y
end

Looking at the Ruby, the syntax is more what I would have expected…

def foo (x,y)
  x+y
end

Which also works in SPi. I just wanted to understand the thinking behind this - is it to present a unified SPi syntax for education for instance? Or are they functionally different?

I can stick to the documented SPi syntax for this one. Sam has said ‘Sonic Pi is not Ruby’ so fair enough but I’m getting the point where using Ruby constructs is important. What’s the team view on this kind of thing?

I was looking at this because I wondered if the different ways of call a function where SPi or Ruby…

a=foo(1,2)

vs the more Rubyesque

a=(foo 1,2)
1 Like

Sam or others may correct me if I am wrong, but from my understanding of it, your first point was correct - the idea being to keep a specific Sonic Pi function in order to keep the functions in the DSL distinctly separate from the underlying plain Ruby.

There is nothing wrong with using plain old Ruby in your compositions per se. The only caveat is that such use is not officially supported, and things may not work as you expect in the future if implementation details in the server or Sonic Pi language change and conflict somehow, etc.

Re your examples of calling a function - either works, and again there is no ‘right’ or ‘wrong’. There are various times in the Sonic Pi language where one or the other is used more frequently - for example, (ring ...) vs something like sample(...).
I think things like (ring ...) were a way to bring a slightly more functional style syntax into Sonic Pi - though again Sam would likely have more to say on that :slightly_smiling_face:

2 Likes