I'm a novice coder who seeks help w/ creating a function and possible clean up of code.

Brief history: I'm currently working on a project that requires the use of a C++ DLL to talk to a PLC machine running in a real time production environment. My background is mathematics, not coding. I'm struggling to make this work!

Currently the parameters for this machine require evaluating data by instantaneous radius of curvature standards. (Recall that the smaller the radius of curvature, the more curvy or less straight a curve becomes. This is not difficult to find by way of calculus, but I'm nowhere near ready to code derivatives and second derivatives! My current curve needs to be adjusted to fit the minimum radius parameters. I've found a way of backdooring this calculus requirement with a little geometry and iterating along the curve, looking at three adjacent points each time. (Points are close enough to be considered instantaneous)

Currently this code works. I had to do some strange internal error handling because it was coming up with strange results when I got close to infinity, 1/0 or something close to it. Maybe this could be a corrected suggestion instead of my if, else stmts.

Please help, comment, or suggest on the following three points of concern:

1) Need to reuse this method of finding the radius several more times than currently using it, so need it to be a function instead of part of the main body of code! Not sure how the variables inside of the function need to be set up to conserve memory

2) Recently read an article warning against function use in DLLs because of inability to handle errors. (An error would stop production Not a good thing! Should I be worried about this?) This DLL called is an exported function---Does this mean I can't call a function within a function? I know nothing

3) Can you suggest any restructuring or changes to be made

Here's the code & structure that's currently used:

Changes made to the program will require the radius of curvature algorithm to be repeated in a binary search fashion. Repeating that block of code would be extremely unfriendly to the reader.Code:DLLMain() //stuff to make the PLC machine & DLL talk to each other extern "C" __declspec(dllexport) int calculate( EXT_ROUTINE_CONTROL* pERCtrl, int npar[] , float fpar[], float xcl[], float xdia[], float ycl[], float ydia[], float zlen[], int nresults[], float fresults[]) { //code //code //more code! //radius of curvature algorithm formatted differently //code }

Here's my proposed changes after reading the threads posted on your forum :

Code://function prototype float FINDRADIUS( float& y1, float& z1, float& y2, float& z2, float& y3, float& z3); DLLMain() //stuff to make the PLC machine & DLL talk to each other extern "C" __declspec(dllexport) int calculate( EXT_ROUTINE_CONTROL* pERCtrl, int npar[] , float fpar[], float xcl[], float xdia[], float ycl[], float ydia[], float zlen[], int nresults[], float fresults[]) { //code //code //more code! //call function several times!!!!!! FINDRADIUS(Parameters. . . ) //code } //???????????????????How & where should I declare variables float FINDRADIUS( float& y1, float& z1, float& y2, float& z2, float& y3, float& z3) { yDiff1=y2-y1; yDiff2=y3-y2; absYDiff1 = abs(yDiff1); absYDiff2 = abs(yDiff2); /*NOTE:the following if, else if tests are important because division by zero will be a problem since the two lines constructed have negative reciprocal slopes relative to the ydiff's*/ if (abs(yDiff2-yDiff1)<=.0002) { /*log is straight along this interval (i.e--do NOTHING jump code set radius[j] really high [not important] and return*/ radius[j]=10000; goto Straight_Log; }//end if straight interval if (absYDiff1<=.00015) { /*To avoid dividing by zero, set absolute value of m2 higher than 20000 (upper limit check ok, jf 7/2004)*/ if (yDiff1>0) m1=20000; else m1=-20000; m2= -(z3-z2)/yDiff2; }//end if yDiff1 is close to 0 else if (absYDiff2<=.00015) { /*To avoid dividing by zero, set absolute value of m2 higher than 20000. (identical upper limit as above, jf)*/ if (yDiff2>0) m2=20000; else m2=-20000; m1= -(z2-z1)/yDiff1; }//end else if yDiff2 is close to 0 if (absYDiff1 > .00015 && absYDiff2 > .00015) {//finds the perpendicular slope of the two intervals m1= -(z2-z1)/(y2-y1); m2= -(z3-z2)/(y3-y2); }//end if perpendicular slopes else if (m1==0 && m2==0) {//error check (should never hit this) m1= -(z2-z1)/(y2-y1); m2= -(z3-z2)/(y3-y2); }//end else if error check Z1Mdpt = (z2+z1)/2; Y1Mdpt = (y2+y1)/2; Z2Mdpt = (z2+z3)/2; Y2Mdpt = (y2+y3)/2; /*We know the slope of the perpendicular bisecting lines and a point on each of the lines, the midpoints. Since the two lines intersect at the radius, solving the system of equations yields the pt w/ coordinates (Zradius,Yradius). Try w/ a compass if you don't believe me ;) */ Zradius = (m1*Z1Mdpt - Y1Mdpt - m2*Z2Mdpt + Y2Mdpt)/(m1-m2); Yradius = m1*(Zradius-Z1Mdpt) + Y1Mdpt; /*The coordinate of the radius is known. By using the distance formula we can find the length of the radius passing through the three points*/ radius = sqrt(pow((z1-Zradius),2)+pow((y1-Yradius),2)); return(radius); }//end find radius function