Recursive function returns a nan (c++) <p>when i use a while loop, the function returns a correct value, but when I make the function recursive, it returns a nan. For debugging purposes, I cout-ed the value(x) just before returning it and it gives a correct answer, but after returning the value to the calling function, it's a nan. One other thing, the program doesn't take 0 for the coefficients of x. Any attempts result in nan. Below is my code (all of it just to be sure I didn't give insufficient information): </p> <pre><code>// This is my first useful program // to calculate the root (Solution) of exponential // functions up to the fourth degree using // Newton-Raphson method and applying a recursive function #include &lt;iostream&gt; #include &lt;cstdlib&gt; #include &lt;string&gt; using namespace std; // Below is the function fGetRoot's prototype declaration // c4, c3, etc are the coefficients of x in the 4th power, 3rd power // etc, while c is the constant float fGetRoot (float c4, float c3, float c2, float c1, float c); float fPowerFunc(float fPowered, int iPower); float x; // declaring this as global variables so they can be initialized in the calling function int i=10; // and be used in the recursive function without being reinitialized during each recursion int main() { float c4, c3, c2, c1, c; float fRoot; cout &lt;&lt; "Hello, I am a genie for calculating the root of your problems\ up to the fourth power" &lt;&lt; endl; cout &lt;&lt; "Please enter the values of the coefficients of x to the power 4\ , 3, 2, 1 and the constant respectively" &lt;&lt; endl; cout &lt;&lt; "x^4:" &lt;&lt; endl; cin &gt;&gt; c4; cout &lt;&lt; "\n x^3:" &lt;&lt; endl; cin &gt;&gt; c3; cout &lt;&lt; "x^2:" &lt;&lt; endl; cin &gt;&gt; c2; cout &lt;&lt; "\n x:" &lt;&lt; endl; cin &gt;&gt; c1; cout &lt;&lt; "Constant, C:" &lt;&lt; endl; cin &gt;&gt; c; cout &lt;&lt; "\nEnter the initial iteration. Any figure is fine. The closer to the answer, the better" &lt;&lt; endl; cin &gt;&gt; x; i=10; // gets the number of times to iterate. the larger, the more accurate the answer fRoot = fGetRoot(c4, c3, c3, c1, c); cout &lt;&lt;"\nAnd the root is: " &lt;&lt; fRoot &lt;&lt; "\n"; return 0; } // The fGetRoot function float fGetRoot(float c4, float c3, float c2, float c1, float c) { float fTemp1, fTemp2, fTemp3, fTemp4; // the series of lines below are equivalent to the one line below but clearer fTemp1 = c4*fPowerFunc(x,4); cout &lt;&lt; "This is the value of c4*x^4: "&lt;&lt; fTemp1 &lt;&lt; endl; // for debugging purposes fTemp2 = c3*fPowerFunc(x,3); fTemp3 = fTemp2 + fTemp1; fTemp1 = c2*fPowerFunc(x,2); cout &lt;&lt; "This is the value of c2*x^2: "&lt;&lt; fTemp1 &lt;&lt; endl; //for debugging purposes fTemp2 = c1*fPowerFunc(x,1); fTemp4 = fTemp1 + fTemp2 + c; fTemp1 = fTemp3 + fTemp4; fTemp2 = 4*c4*fPowerFunc(x,3); fTemp3 = 3*c3*fPowerFunc(x,2); fTemp4 = fTemp2 + fTemp3 + 2*c2*x; fTemp2 = fTemp4; fTemp3 = fTemp1/fTemp2; fTemp1 = fTemp3; fTemp2 = x - fTemp1; x = fTemp2; i--; // The line below is equivalent to the "fTemp" series of lines... just to be sure //x=x-(c4*fPowerFunc(x,4)+c3*fPowerFunc(x,3)+c2*fPowerFunc(x,2)+c1*fPowerFunc(x,1)+c)/(4*c4*fPowerFunc(x,3)+3*c3*fPowerFunc(x,2)+2*c2*x); cout &lt;&lt; "\nThis is x: " &lt;&lt; x &lt;&lt; endl; i--; if(i==0) return x; x=fGetRoot(c4, c3, c2, c1, c); // Let the recursion begin... } // A simpler approach to the fPowerFunc(). This gets two numbers and powers the left one by the right one. It works right float fPowerFunc(float fPowered, int iPower) { float fConstant=fPowered; while(iPower&gt;0) { fPowered *=fConstant; iPower--; } return fPowered; } </code></pre>