fitMedia()
, setTempo()
, init()
, play()
, and range()
are all examples of functions. These are functions provided and defined by EarSketch. But you can also create your own functions. User-defined functions allow you to write some code and execute it anywhere in a script without having to write it all over again. To use the code in your own function, you call the function with its name — and any parameters inside of parentheses — just as you would call any other function.
For instance, calling setTempo()
executes some code that is useful to define the tempo of the music for a song. But since this is useful every time you write an EarSketch script, we created a function called setTempo()
to simplify the task. That way, instead of having to write a bunch of code every time, you only need to call the function:
setTempo(120)
Functions also make code clearer. They reduce duplication of lines and small details that get in the way of a clear understanding of a script’s goals. Instead of a mass of undifferentiated functions, the backbone of a script becomes a list of function calls that build towards a clear purpose. Functions provide a script with structure, making it more readable. Finally, creating your own functions allows you to reuse code that you’ve written. So once you’ve written some code and defined it within a function, you can reuse it as many times as you like by simply calling the function again and again. To take advantage of these things, you will learn how to make your own functions.