Recursive function : Function Definition « Function « JavaScript Tutorial






<HTML>
<HEAD>
   <SCRIPT>
   function fib (inNum) {
      if (inNum == 0) 
        var fibNum = 0;
      else{
        if (inNum == 1)
           fibNum = 1;
        else{
           fibNum = fib(inNum - 2) + fib(inNum - 1);
        }
     }
     return fibNum;
   }

   function writeFibs(topFib) {
      for (var i=0;  i <= topFib ; i++) {
        document.write ("Fib(" + i + ") = " + fib(i) + " <br>");
      }
   }
   
   </SCRIPT>
</HEAD>
<BODY>
   <FORM Name="theForm">
   <INPUT Type=Text Name="numFibs">
   <INPUT Type=Button Value="Show Fibs" onClick='writeFibs(numFibs.value);'>
</FORM>
</BODY>
</HTML>








7.1.Function Definition
7.1.1.Functions
7.1.2.Define the simplest function
7.1.3.Define a function
7.1.4.Calculation in function
7.1.5.Recursive function