Declaring variables ,explicit order

First example works ,when first declaring variables and give it a value , the second example doesn’t declare variable ,give value ; declare another variable , give value

(

{
var mother;
var father;
mother = 400;
father=0.1;

SinOsc.ar(freq:mother)*father}.play;)

//something //

(
{
var mother;
mother = 400;
var father;
father=0.1;

SinOsc.ar(freq:mother)*father}.play;)

You’re correct that this doesn’t work. All variable declarations (var foo) must be grouped together at the beginning of the block that the variables are scoped to. In your example, the variables father and mother are scoped to the Function, so you can do any of the following:

{
    var mother;
    var father;
    mother = 400;
    father = 0.1;
    ...
}
{
    var mother, father;
    mother = 400;
    father = 0.1;
    ...
}
{
    var mother = 400, father = 0.1;
    ...
}

…but NOT:

{
    var mother;
    mother = 400;
    var father;
    ...
}

Does it mater if I declare a variable or argument that have the same value
See example , bothdo exactly the same thing ( until I dig deeper that is )

 ({
arg money;
money=777;

(SinOsc.ar(money,mul:1))}.play);

//commnent//
({
var money;
money=666;

(SinOsc.ar(money,mul:1))}.play);

I am still trying to wrap my head around it , sure I understand the arguments of the objects , and these can be replaced with variables .
But what is the use of declaring arguments , are these not the same as variables ( in the example given ) ?

Strange whenver I have more then 2 declared arguments , the code won’t work anymore

({
arg money; arg horse///doesn’t work
money=777;
horse=0.3
(SinOsc.ar(money,mul:1))*horse}.play);
//commnent//
({
var money;var timber;///still works
money=666;
timber=0.2;
(SinOsc.ar(money,mul:1))*timber}.play);
///

Yes, it does matter, and you’re right both that the two examples seem to do the same thing and that they are not actually the same in practice.

Variables are used internally by the Synth, but arguments are also exposed to the “outside world” as Controls.

(
    x = {
        var money = 666;
        SinOsc.ar(money)
    }.play
)

This creates a Synth bound to interpreter variable x, but you have no way of modulating the frequency of the SinOsc once the Synth has been created. That’s where args come in.

(
    x = {
        arg money = 777;
        SinOsc.ar(money)
    }.play
)

x.set(\money, 666)

In this case the arg creates a control which can be set from outside the Synth.

Yes, I believe all your args need to be declared in one statement like arg x, y, z.... It’s more common these days to use pipes to enclose your arg list instead of the arg keyword ({|x, y, z| ... }), but the two forms are equivalent. In general, Functions are structured like:

{|x, y, z| // arguments
    var abc; // variable declarations
    /* body of the Function, where you use the args and vars to do things */
    /* final line of the Function, which determines what it returns */
}
1 Like

@gentleclockdivider please make sure to enclose your code samples in triple-backticks (```) so that they’re easier for others to read.

since you’re new to SC, i’d like to recommend the “getting started with SC” tutorial series: https://doc.sccode.org/Tutorials/Getting-Started/00-Getting-Started-With-SC.html. It will probably answer a lot of these kinds of questions for you, and each section has a list of documentation pages you can read for a deeper dive.

Thanks catnipwinz and virtualwoef :slight_smile: , I am reading lot’s of supercollider manuals at the same time
I still have a long way to go that’s for sure , but I am already hooked

2 Likes