Variable amp not defined

A simple example using two sine osc’s (using the phase parameter for phase modulation ) and some variables defining carr and mod frequency

However , in the second example I assigned the (amp ) envelope to variable ‘amp’ , and the console givs me an error it is not defined

(
{
var p;l;c;m;

c=[110,111];
m= c*3;////modulator frequency 
l=XLine.ar(1,0.001,2);
p = SinOsc.ar(m);
SinOsc.ar(c,p*l)*0.5*Env([0,1,0],[0.01,0.500],[0,-5]).ar}.play)

//
(
{
var p;l;c;m;amp;
amp=Env([0,1,0],[0.01,0.500],[0,-5]).ar;
c=[110,111];
m= c3;////modulator frequency
l=XLine.ar(1,0.001,2);
p = SinOsc.ar(m);
SinOsc.ar(c,p
l)0.5amp}.play)

c3 is not defined in your second example

https://soundcloud.com/all-n4tural/sets/spotlight
https://alln4tural.bandcamp.com

When declaring multiple variables, instead of:

use commas to separate variables being declared:

var p, l, c, m, amp;

Then amp won’t give an error.
Best,
Paul

Hi @gentleclockdivider, please remember to place triple backticks (```) around your code to make it easier to read and avoid Discourse from misformatting it.

Btw, the syntax for defining functions is described in the “Getting Started” tutorial here: https://doc.sccode.org/Tutorials/Getting-Started/04-Functions-and-Other-Functionality.html. I’d highly recommend giving it a read for more information.

You’re correct , I 've corrected it , but the problem is still present
Here I assigned all variables ( including amp ) in a column fashion and it works , the first example still doesn’t work >
Just the ordering of the variables is different .
First example doesn’t work , the second does

//
(
{
var p;l;c;m;amp;
amp=Env([0,1,0],[0.01,0.500],[0,-5]).ar;
c=[110,111];
m= c3;////modulator frequency
l=XLine.ar(1,0.001,2);
p = SinOsc.ar(m);
SinOsc.ar(c,p
l)0.5amp}.play);
//
(
{
var p;
var l;
var c;
var m;
var amp;
amp=Env([0,1,0],[0.01,0.500],[0,-5]).ar;
c=[110,111];
m= c3;////modulator frequency
l=XLine.ar(1,0.001,2);
p = SinOsc.ar(m);
SinOsc.ar(c,p
l)0.5amp}.play);
///

Edit there has to be some kind of bug , in the code c*3 pasted here (mod.frequency ) the * sign is omitted , deleted

This was already explained. It should be var p, l, c, m, amp;.

But the example you posted still has semicolons, instead of commas, between the variable names. Hence the last example isn’t actually corrected.

hjh

I thought that was only for arguments and not variables
My excuses

There is no bug; this happens because you didn’t place your code inside triple-backticks. As an example, here’s the same line of code with and without them:

cd5

c*d*5

The *'s are read as italics formatting in the markup used on Discourse.

Thanks all but I think there is a bug
Go back to my verry first example which worked and had the variables written on one line with semibrackets in between ( as jamshark pointed out these should be comma’s only and not semibrackets ) , still it works with semibrackets
Now delete this line and rewrite it (exactly ) again with semibrackets, now it doesn’t work anymore

Bug or not ?
Hence my original start of the topic why it stopped working after I added another variable

Then why did the first example work when amp was not assigned yet ?
The line containing all variables also had semibrackets
The problem occured once amp was added as a variable

Yes, your code has errors. I thought you were referring to the “bug” that your pasted code was misformatted.

Single letters used as identifiers are effectively global variables. This means that if you don’t declare x as a variable within the local scope, the name x refers to that global variable. This is covered in the tutorial page I just linked above, toward the end:

The letters a to z are what are called interpreter variables. These are pre-declared when you start up SC, and have an unlimited, or ‘global’, scope. This makes them useful for quick tests or examples.

https://doc.sccode.org/Tutorials/Getting-Started/04-Functions-and-Other-Functionality.html

Most programming languages are designed so that the purpose of every character in a code string can be determined unambiguously by scanning forward, without backtracking.

Your guess about arg and var separators being semicolons (btw they are not called semibrackets in English – they are semicolons) breaks this principle, by supposing that a ; may serve as both a list separator and a list terminator. That’s ambiguous. Computer languages tend to avoid this kind of ambiguity.

var abc; def; abc + def;

var abc;var signals the compiler to enter a “variable declaration” state and the ; signals to exit that state.

Next is def. There’s no var so the compiler will not re-enter variable declaration mode. That means it must be an expression. Here it will fail because def is undefined.

If instead it’s var abc, def; abc + def; then the comma signals the compiler to continue with variable declaration, so both variables are defined and the line can compile. (It will fail to execute because the variables have no values, but that’s a different issue.)

var a; b is in the same format as var abc; def – a variable declaration followed by an expression – but: def is invalid as an expression here because the variable is undefined. b is valid as an expression because, as Brian said, b is an interpreter variable that always exists, so it can be used without being declared.

The principle to take away is, then, that usually a character will have one and only one meaning in a given context. It might have a different meaning in a different context, though. (There’s even one case in SC where a | character could be a terminator or a binary operator, both in the context of an argument list – but SC forces the use of parentheses in this case to tell them apart – so the syntax can still meet the requirement of non-ambiguity.)

hjh

1 Like