Spacing and Parentheses

Csound code is written with one statement (an instruction to the compiler) per line. The carriage-return character ends a statement. Other than that, Csound is fairly tolerant of how you enter your code. You don’t need to indent the lines within an if/then/endif block. You can use tab characters to align columns in order to make them easier to read, but this is purely a convenience; to Csound, any string of one or more space or tab characters is the same.

When entering formulas, you can use or not use spaces around the symbols—again, it’s up to you. The three lines below are entirely equivalent as far as Csound is concerned:


  if(ivalue=5)then
  ;... is functionally the same as:
  if (ivalue = 5) then
  ;... and, for that matter, the same as:
  if ivalue=5 then

Parentheses are required only when you need to change the order of precedence of two or more operators. For example, the multiplication operator (*) has higher precedence than the addition operator (+). So these two statements are identical:


  ivalueX = ivalue1 + ivalue2 * ivalue3
  ivalueX = ivalue1 + (ivalue2 * ivalue3)

…but they’re not the same as this:


  ivalueX = (ivalue1 + ivalue2) * ivalue3

In the first two lines, ivalue2 will be multiplied by ivalue3, and then ivalue1 will be added to the result. In the third line, the parentheses ensure that ivalue1 and ivalue2 will be added first, and then the result of the addition will be multiplied by ivalue3.

If you’re not sure of the precedence of two operators, using extra parentheses is an easy way to make sure your code does what you want it to. Adding superfluous parentheses also helps group complex expressions so as to make them easier to read.

Parentheses are required around the arguments to a few opcodes, such as sqrt, even when the expression being used as an argument is simple. These opcodes are often referred to as functions, because their syntax is identical to the syntax used in other programming languages to call functions:


  ivalueX = sqrt(ivalue)    ; this compiles...
  ivalueX sqrt ivalue     ; ... but this doesn’t

Spaces are required only in a few places, such as around the names of opcodes and between numerical values (p-fields) in the score.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.172.38