Function behavior with passed arguments


#1

1) Give a description of the problem
Just an observation. I dug a little deeper after a discussion with @eibyer here. After a bonehead variable mistake, I began to wonder about functions with passed arguments. My findings may be expected (common knowledge) or just syntax errors. But just basic adding/subtracting passed arguments did not give me the results I had expected. I had to add functions to the addition for correct results. Otherwise, the passed arguments were treated as strings. Subtraction worked correctly.

2) What is the expected behaviour?
I expected the addition and subtraction to work without additional functions.

3) What is happening/not happening?
Addition treated the passed arguments as strings unless a function was used.
Subtraction did not require functions for correct results.

**4) Post a Green Snapshot of the piston![image|45x37]


#2

I believe there is extra code in place for the plus symbol, that does not exist for the minus dash…


I tend to use data more than once, so I usually dump incoming data into variables first, and then refer to those in the future. Using this method, there is no need for the extra code.

IE, the second piston can do this:

pic

… to yield this:

pic


Note: Parenthesis are required around this formula…


#3

Yes, I found the same with local variables…

It just seems odd that passed arguments, when added together (without the integer/decimal function), are treated as string variables. But subtracting passed arguments is not treated that way.

I guess the take-away from my little experiment is that I will assign passed variables to a locally declared variable. That just seems like extra overhead.


#4

This is one of the major reasons why I use specific variable types, and avoid using “dynamic” variables… (the later is open to interpretation… I assume, similarly with $args)


If you are not re-using the dataPoints, you could cheat a bit by having the first piston do the math, and then only pass one Integer between pistons.


#5

It depends how you look at it. It seems pretty clear that the passed arguments ARE strings.

webCoRE is doing its own interpretation of e.g. "20" + "2". It sees there are two strings and it recognises the + as commonly meaning string concatenation. So that is what it does, and gives the answer "202".

However, when it comes to "20" - "2" it doesn’t recognise the - as being meaningful. So it casts the string to numbers and does 20 - 2 instead.

Actually in Groovy "20" - "2" would give "0". But that isn’t so widespread.


#6

Sounds like a good explanation! So I guess it would be wise to assign passed arguments to a local variable before use. Or use extra coding with functions.


#7

Supposing you called the piston by URL rather than ‘execute piston’. You’d write your argument handling code exactly the same. You’d actually have to make a particular effort to know how it was called. In a URL you can’t tell from the query string ?Integer1=20 whether Integer1 was an integer or a string to start with. It is just a string now. Same with executing a piston. The arguments are converted to strings. If you want to treat them as something else it is down to you to make webCoRE do what you want.

You also have to remember that webCoRE is starting from scratch in evaluating expressions. Just like any other programming language, it decides how it wants to interpret e.g. "20" + "2". It could have decided that "20" . "2" meant concatenation and "20" + "2" should be treated as integer addition, but it didn’t.


#8

That makes sense. Thanks for the help.