# Strange behaviour of a function in feature definition

3 replies to this topic

### #1 Mr. Klaus Straka

Mr. Klaus Straka

Newbie

• Members
• 9 posts

Posted 09 June 2016 - 11:03 AM

Hi,

i am working on a feature that actually draw some helical curves and circles. Therefor i frequently have to calulate an angle in the x-y-plane. Ok, thats easy. To get the right actual angle in the range of 0-360 degrees it wrote a function

```function quadrantCorrection(FPoint startp, FDouble phi):FDouble
if (AND(startp.getY()>=0,startp.getX()<0)) // 2th. Q.
phi = 180-phi
elseif (AND(startp.getY()>=0,startp.getX()>=0)) // 1st Q.
phi = phi
elseif (AND(startp.getY()<0,startp.getX()>=0)) // 4nd. Q.
echo("Q4, phi " + phi)
phi = 360-phi
echo("Q4, phiCorr " + phi)
elseif (AND(startp.getY()<0,startp.getX()<0)) // 3rd. Q.
phi = 180+phi
endif
return(phi)
endfunction
```

I use this function for a great number of times and it seems to work fine. However in the attached project in the feature definition "MixerBasemesh" at line 340 it fails for whatever reason.

Below some debug output is given, the x and y coordinate show that the point is located in 4th quadrant. The function above detects this correctly (indicated by the debug output "Q4, phi 7.44226") but it does not compensate (phi=360-phi) the angle as shown by "Q4, phiCorr 7.44226".

```phiE: 7.44226
x: 19.9307
y: -2.60349
Q4, phi 7.44226
Q4, phiCorr 7.44226
phiECorr: 7.44226
phi: -37.5577
phi: -37.5577
-----
```

This is a somwhat strange behaviour, i have no idea what i am doing wrong. Any comments on this.

I use V4.1.0 on Ubuntu.

Thank you for your help an best regards,

Klaus

• 0

### #2 Mr. Arne Bergmann

Mr. Arne Bergmann

Expert

• Members
• 156 posts
• LocationBerlin

Posted 09 June 2016 - 11:32 AM

Hi Klaus,

Please try to change the code of the function to the following:

```function quadrantCorrection(FPoint startp, FDouble phi):FDouble
if (AND(startp.getY()>=0,startp.getX()<0)) // 2th. Q.
phi = 180-#phi
elseif (AND(startp.getY()>=0,startp.getX()>=0)) // 1st Q.
phi = phi
elseif (AND(startp.getY()<0,startp.getX()>=0)) // 4nd. Q.
echo("Q4, phi " + phi)
phi = 360-#phi
echo("Q4, phiCorr " + phi)
elseif (AND(startp.getY()<0,startp.getX()<0)) // 3rd. Q.
phi = 180+#phi
endif
return(phi)
endfunction
```

It works for me like that. Apparently, there is a problem when trying to assign an expression to an argument inside a function that includes the argument itself. To be honest, I am not quite sure at the moment, why that is the case, but the '#' (detach) helps here.

As an addition: Note that your function does not really need to return anything, since it just returns the argument. As opposed to other places, even basic types are passed to functions as references. So, when calling the function above like this:

```double d()
point p()
// .... initialize....

```

the variable d might have a different value after the call (except if the 1st Q case occurred). If you want to make sure that this does not happen, introduce a temporary variable (which, by the way, also takes away the need to use the detach):

```function quadrantCorrection(FPoint startp, FDouble phi):FDouble
double result(phi)
if (AND(startp.getY()>=0,startp.getX()<0)) // 2th. Q.
result= 180-result
elseif (AND(startp.getY()>=0,startp.getX()>=0)) // 1st Q.
result = result
elseif (AND(startp.getY()<0,startp.getX()>=0)) // 4nd. Q.
echo("Q4, phi " + result)
result= 360-result
echo("Q4, phiCorr " + result)
elseif (AND(startp.getY()<0,startp.getX()<0)) // 3rd. Q.
phi = 180+result
endif
return(result)
endfunction
```

I hope this helps.

Cheers,

Arne

• 0
Arne Bergmann
Software Engineer

www.FRIENDSHIP-SYSTEMS.com
TEL. +49 (0)331 967 66 0

a.bergmann at friendship-systems.com

My Developer blog

### #3 Mr. Arne Bergmann

Mr. Arne Bergmann

Expert

• Members
• 156 posts
• LocationBerlin

Posted 09 June 2016 - 11:35 AM

The document that is attached there includes some more information on references and functions.

• 0
Arne Bergmann
Software Engineer

www.FRIENDSHIP-SYSTEMS.com
TEL. +49 (0)331 967 66 0

a.bergmann at friendship-systems.com

My Developer blog

### #4 Mr. Klaus Straka

Mr. Klaus Straka

Newbie

• Members
• 9 posts

Posted 09 June 2016 - 02:03 PM

Hi Arne,