
Problems with mutiple returns

Mike German
I have a number of functions where Iwant to return mutiple varaibles. Not being a proper programmer I can do this by converting numbers to formatted strings, concatenated them and return them a string list. This is a bit clunky to say the least. I'm using Igor 9
I am now try to improve with the multiple return syntax. I find the structure approach diffecult and again a bit awkward where I have a number of functions to write. I therefore wrote this example code, which to me is using the syntax according to the Manual
Function [X_mid, Y_mid, Z_mid] midpoint(X1, Y1, Z1, X2, Y2, Z2) variable X1, Y1, Z1, X2, Y2, Z2 variable X_mid = (X1 + X2) / 2 variable Y_mid = (Y1 + Y2) / 2 variable Z_mid = (Z1 + Z2) / 2 return [X_mid, Y_mid, Z_mid] end
but when compiling, X_mid is highlighted and an error "paramater undeclared" is given. How do I declare them?
Hi,
You need to declare the variable type in the first line. I got this to compile.
What is interesting is that this version doesn't compile complaining about the X1 variable.
It seems if start declaring inline you must declare them all inline.
Andy
October 26, 2024 at 09:09 am - Permalink
Interesting - I'm using Version: 9.0.6.1 (Build 56660) and similar to you this version complains at the your second snippet .
The first line with all the declarations is even more clunky that my string approach :-) .. but thanks that answers my query. There must be a better way for this to work.
October 26, 2024 at 10:00 am - Permalink
Andy is right, multiple return syntax requires IP7 inline style parameter declarations. The function header then also get's a bit too long as you already noticed.
IIRC you can break the function declaration line in multiple lines with having a trailing \.
October 26, 2024 at 11:05 am - Permalink
Somewhere, the manual says that all definitions must be in line using multiple return functions.
I'd prefer using a WAVE function for this case.
October 26, 2024 at 11:12 am - Permalink
I would NOT recommend it, but you could also use pass-by-reference in a pinch:
This invites difficult-to-find bugs, so you need to be careful. I personally use this approach only for pure output functions with no input.
October 26, 2024 at 09:17 pm - Permalink
Mike,
When declaring parameters inline, I like the following use of whitespace as it is clearer to me than typing it out in a linear fashion.
October 28, 2024 at 08:56 am - Permalink