📏 Parameterized Functions | myBetabox

📏 Parameterized Functions

Keyboard Guide

Parameterized Functions

The Code Explained

This is obviously a simple example but if you run this code you will notice that the number is passed into the function using the variable n and is then played.

🌟 Parameters are variables that are passed through functions where they do something with those given variables.

In the video above, the function :chord_player is called multiple times using different parameters (:E4, 2, :G4, 5) to create different tunes using minimal code.

To declare a parameter, you must put its name inside of the two vertical lines that we call goal posts. This will then allow you to put a value after calling your function that will then be passed into the function. Let’s take a look at something a little more complicated now:

define :chord_player do |root, repeats|
    repeats.times do
        play chord(root, :minor), release: 0.3
        sleep 0.5

chord_player :e3, 2
sleep 0.5
chord_player :a3, 3

As you can see, this function takes two parameters. One is a note while the other is a note.

🌟 |root, repeats| is the order of your parameters. When you type :E4, 2 you are saying |:E4, 2| as :E4 lines up with root and 2 lines up with repeats.

This particular function will play a chord based around that note and play it a certain number of times. Don’t worry if you don’t understand the play chord code yet, we will learn about it tomorrow! Now you should know the basics of functions and variables!