# Lesson 16 # Functions Lesson. In this lesson, # you will learn how to define and # use functions with parameters. # General Instructions: # # This lesson contains two examples and # two exercises (A, B). # # To listen to an example, change "comment" # to "uncomment" on the "comment do" line, # then click the "Run" menu command at the # top of the Sonic Pi editor window. # When you are done with the example, turn # it off by changing "uncomment" back to # "comment," and move on to the next section # of the lesson. # # In the previous lesson you learned how # to define a function by giving a block # of code a name and calling it from elsewhere # in the program. In this lesson, we will # see that functions gain a lot of power # and flexibility when we give them parameters. # Example 1: Function with parameters. comment do define :twinkle_a do | offset | melody_a = [:C4, :C4, :G4, :G4, :A4, :A4, :G4] index = 0 count = melody_a.length count.times do play melody_a[index] + offset sleep 1 index += 1 end sleep 1 end define :twinkle_b do | offset | melody_b = [:F4, :F4, :E4, :E4, :D4, :D4, :C4] index = 0 count = melody_b.length count.times do play melody_b[index] + offset sleep 1 index += 1 end sleep 1 end define :twinkle_c do | offset | melody_c = [:G4, :G4, :F4, :F4, :E4, :E4, :D4] index = 0 count = melody_c.length count.times do play melody_c[index] + offset sleep 1 index += 1 end sleep 1 end use_bpm 120 twinkle_a 2 twinkle_b 2 twinkle_c 2 twinkle_c 2 twinkle_a 2 twinkle_b 2 end # Exercise A: # Define a new function named "twinkle_play" # that wraps the calls to twinkle_a, # twinkle_b, and twinkle_c in a single function. # Add a single parameter, "offset," to the # twinkle_play function that passes the offset # to each function inside the new function. # Call twinkle_play from inside a # thread, and then call it outside (below) # the thread after a delay of four beats. Then # do a version with a delay of eight beats. # Example 2: A Metronome function with parameters comment do define :metronome do | beats, strong, weak, bpm | use_bpm bpm loop do sample strong sleep 1 count = beats - 1 count.times do sample weak sleep 1 end end end metronome 4, :perc_snap2, :perc_snap, 120 end # Exercise B: # Define a new function for Example 2 which # will run at the same time as the metronome # function. Create a melody which plays twice # as fast as the metronome ticks (that is, # sleep 0.5 or sleep 1.0/2.0 for each note in # the melody). Call the function on the line # directly below the call to the metronome # with no sleep between the two calls. # Tip #1: Give the melody function a single # parameter for the bpm, and set the bpm # inside the melody function. (Otherwise # Sonic Pi will use the default bpm; the # metronome bpm is set within the scope of # the metronome function only). # Tip #2: Since the metronome is in a loop # that will not stop until "Stop" is clicked, # the melody function will never be called # unless you place the call to the metronome # function inside a thread.