concept global variable in category chuck

This is an excerpt from Manning's book Programming for Musicians and Digital Artists: Creating music with ChucK.
The swell function has a return type of void, which means it doesn’t return a value at all, because it doesn’t need to. Remember in chapter 1 when we introduced data types, and we promised you that we’d use void later on? Well, here we are. You can also make functions with void arguments, like you used for s.gain() in listing 5.6, because they don’t need data input to do their thing. Some functions like this return a value, others do useful work without either needing or yielding data. These might work on global variables, advance time, or do other things.
Let’s explore how functions and global variables can work together by writing a program that walks up and down a little scale pattern, rising in pitch forever. In listing 5.10, you’ll use a new sound-making UGen, a Mandolin, and connect it to the dac
. The Mandolin is a complete “instrument” that you can play with simple commands like .freq (like SndBuf) and .noteOn (which essentially plucks the Mandolin). We’ll talk more about Mandolin and many other UGens starting in the next chapter. Continuing with listing 5.10, you also define a global variable called note and initialize it to 60 (middle C)
. Both mand and note are global, because you declare them at the top of your program, outside any braces. Next, you define two functions: noteUp()
and noteDown()
, which have no input arguments, because they operate only on global variables. Also notice that the output types are set to void (denoting no return value). If you now look more closely at noteUp()
, you’ll see that it updates the global variable note, by adding one
, and prints out the new note value
. noteDown()
has similar functionality but this time subtracts 1 from note
You define one more function called play()
, which sets the pitch of your mand
, plucks it with noteOn
, and advances time by 1 second
. The two functions noteUp() and noteDown() call
the play() function
to cause the mandolin to play.