In lieu of an abstract, here is a brief excerpt of the content:

82 Chapter 7 Variable Assignment and Scoping This chapter introduces you to how to assign and reference variables in SAL (and Nyquist). Variables save values for reuse, avoiding the need to recompute them. Variables also offer a way to name values, making programs easier to understand. You will become familiar with several more SAL functions and the concept of the scope of a variable. 7.1 set Command In Chapter 4, we used the set command to assign a list (a Nyquist score) to the variable my-score. The association between a variable and its value is called a binding. In addition to the assignment operation denoted by set, bindings are created by parameter passing, with, and other commands. We say the scope of the variable my-score is global because the variable and its value are valid everywhere in the program. The scope of a variable is the region in which a variable’s value is known. The general set command template is set variable1 = value1, variable2 = value2, … variablen = valuen For example, set a = 1, b = 2, c = 3 will assign the variable a the value of 1, b the value of 2, and c the value of 3. Example 7.1.1 shows the variable a4 evaluates to 69. We define a function transp that adds an interval to a pitch number. The body of the function returns the sum of the pitch and the interval. When we call the function with an input of a4 and 5, a4 is evaluated to obtain 69, which is passed to transp along with 5. The sum, which is 74, is returned. Printing the value of a4 indicates that the global variable has not been reassigned. Printing the value of pitch indicates that the variable pitch has no value. What does this mean? This example demonstrates some of the differences between local and global variables. The parameter pitch in the function transp acts as a local variable. pitch is considered a local variable because its value is known only within the scope of its function. We demonstrate the scope of pitch by calling the function transp where pitch is bound to the value 69 and used to compute the result value 74. But 7.1 set Command 83 pitch is local to the function transp, demonstrated by trying to evaluate it outside of the transp function. Example 7.1.1: Local and global variables SAL> print a4 69 SAL> define function transp(pitch, interval) begin return pitch + interval end SAL> print transp(60, 3) 63 SAL> print transp(a4, 5) 74 SAL> print a4 69 SAL> print pitch error: unbound variable - PITCH Call traceback: SAL top-level command interpreter SAL> When we call the function transp with an input of a4, the function returns the transposed value of the global variable a4. A subsequent access of a4 indicates its value is unchanged. The global variable a4 is unchanged because it was not explicitly reassigned using set. It is the value of a4, not a4 itself, that is associated with the pitch parameter in transp. a4 is not affected by the function, and even pitch is unchanged from its initial value, because there is no set. Example 7.1.2 uses set in the body of the function definition to reassign the value of a4. A function call followed by a print demonstrates that set reassigns the value of the global variable a4. [3.145.151.141] Project MUSE (2024-04-19 08:53 GMT) 84 Chapter 7 ⋅ Variable Assignment and Scoping Example 7.1.2: Global variable SAL> define function transp-a4-with-set(interval) begin set a4 = a4 + interval return a4 end SAL> print transp-a4-with-set(0.1) 69.1 SAL> print a4 69.1 Local and global variables of the same name can exist at the same time without interference. In Example 7.1.3, the variable v is used as both a global and a local. When both are active (inside the function), v refers to the local variable, but outside of the function body, v refers to the global. Example 7.1.3: Local variable SAL> define function my-fn(v) begin display "inside my-fn", v end SAL> set v = "v is a global" SAL> exec my-fn("v is a local") inside my-fn : V = v is a local SAL> display "outside my-fn", v outside my-fn : V = v is a global SAL> 7.2 begin, end, and with Until now, we...

Share