How to use squirrel functions?

I am calling a simple function in my squirrel code, but this does not seem to work as expected.
Calling the a function with parameters does not affect the original variables. ‘counter1’ and ‘counter2’ just keep the same value. In javascript this would work, so why doesn’t this work in Squirrel?

`// declare test variables
counter1 <- 100;

function impLoop() {
// function call to update these variables
changeValue(counter1);
server.log("counter 1 is now " + counter1);

// schedule the loop to run again:
imp.wakeup(1, impLoop);
}

function changeValue(val1){
// shouldn’t this increase the value of counter1?
val1 = val1+25;
}`

which value do you want to change?

right now, you are only changing val1 the exactly same way every loop.

I am expecting the variable counter1 to be increased after calling “changeValue()”.
Inside changeValue, the val1 variable is updated.

Strangely, the log message just displays “100”, which is the original value before calling “changeValue()”

I’ve updated the above code to clarify my question.

counter1 is a global variable, set to 100. The variable val1 in the function changeValue() is local to that function, ie. only that function ‘knows’ about it.

Your code essentially copies the value in counter1 into val1 when you pass counter1 to the function changeValue(). You then change the value of val1 (to 125; at this point counter1 is still 100) but you don’t hand back the answer. There are at least two ways to do this, but given you’re using a function, the best is to change your functions thus:

function changeValue(val1) { // shouldn't this increase the value of counter1? NO!! val1 = val1+25; return val1 // return the sum's answer }

and

`function impLoop()
{
// function call to update these variables
counter1 = changeValue(counter1);
server.log("counter 1 is now " + counter1);

// schedule the loop to run again:
imp.wakeup(1, impLoop);
}`

Here, counter1 is updated with the value ‘returned’ by the function changeValue()

In Squirrel, single values (numbers, strings, booleans – known collectively as “scalars”) are “passed by value”. That means that “val1” in the function changeValue is a copy of the variable counter1, and changes made to it aren’t reflected in the original – it’s not copied back again.

By contrast, compound values (arrays, tables, class instances) are “passed by reference”, which means that the variable in the function is a reference to the original and changes made to it are visible from the outside.

Exactly the same is true in Javascript, BTW:
`var counter1 = 100;

function changeValue(val1) {
val1 = val1 + 25;
}

changeValue(counter1);
console.log(counter1);
`

Peter

Thanks for the clarifications! There’s some confusion on my part apparently.
I’m going to try and use an object instead of passing a straight number.
(I can’t use return values because ultimately I need to change several different parameters with one function).

This is fundamentally the difference between “pass by value” and “pass by reference” here’s one reasonable description of the concept: http://courses.washington.edu/css342/zander/css332/passby.html

(I can't use return values because ultimately I need to change several different parameters with one function).

If you need to return multiple values, you could look at returning a table:

function returnsMultipleValues(p1, p2, p3) { return { result1 = p1+1, result2 = p2*2, result3 = p1+p2+p3 }; }