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
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.
X_mid = (X1 + X2) / 2
Y_mid = (Y1 + Y2) / 2
Z_mid = (Z1 + Z2) / 2
return [X_mid, Y_mid, Z_mid]
end
What is interesting is that this version doesn't compile complaining about the X1 variable.
variable X1,X2,Y1,Y2,Z1,Z2
X_mid = (X1 + X2) / 2
Y_mid = (Y1 + Y2) / 2
Z_mid = (Z1 + Z2) / 2
return [X_mid, Y_mid, Z_mid]
end
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.
// assign variables
x1 = ...
...
// move to waves
make/FREE/D/N=3 w1, w2
w1 = {x1, y1, z1}
w2 = {x2, y2, z2}
// get mid point
wmp = find_midpoint(w1, w2)
// OR
variable xmp, ymp, zmp
xmp = find_midpoint(w1, w2)[0]
Function/WAVE find_midpoint(wave w1, wave w2)
make/FREE/N=3/D wmp
wmp = (w1 + w2)/2
return wmp
end
October 26, 2024 at 11:12 am - Permalink
I would NOT recommend it, but you could also use pass-by-reference in a pinch:
variable X_mid, Y_mid, Z_mid, X1, Y1, Z1, X2, Y2, Z2
X_mid = (X1 + X2) / 2
Y_mid = (Y1 + Y2) / 2
Z_mid = (Z1 + Z2) / 2
end
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.
variable X_mid,
variable Y_mid,
variable Z_mid
] midpoint(
variable X1,
variable Y1,
variable Z1,
variable X2,
variable Y2,
variable Z2
)
X_mid = (X1 + X2) / 2
Y_mid = (Y1 + Y2) / 2
Z_mid = (Z1 + Z2) / 2
return [X_mid, Y_mid, Z_mid]
end
October 28, 2024 at 08:56 am - Permalink