218 Methods Chapter 6 26 // main entry
218 Methods Chapter 6 26 // main entry point for the application 27 [STAThread] 28 static void Main() 29 { 30 Application.Run( new FactorialTest()); 31 } 32 33 private void showFactorialsButton_Click( object sender, 34 System.EventArgs e ) 35 { 36 outputLabel.Text = “”; 37 38 for ( long i =0; i <= 10; i++ ) 39 outputLabel.Text += i + "! = " + 40 Factorial( i ) + "n"; 41 } 42 43 } // end of class FactorialTest Fig. 6.15 Calculating factorials with a recursive method. (Part 2 of 2.) Method Factorial receives a parameter of type long and returns a result of type long. As seen in Fig. 6.15, factorial values become large quickly. We choose data type long so the program can calculate factorials greater than 20!. Unfortunately, the Factorial method produces large values so quickly, even long does not help us print many more factorial values before the size of even the long variable is exceeded. Factorials of larger numbers require the program to use float and double variables. This points to a weakness in most programming languages, namely, that the languages are not easily extended to handle the unique requirements of various applications. As we will see in our treatment of object-oriented programming beginning in Chapter 8, C# is an extensible language programmers with unique requirements can extend the language with new data types (called classes). A programmer could create a HugeInteger class, for example, that would enable a program to calculate the factorials of arbitrarily large numbers. Common Programming Error 6.14 Forgetting to return a value from a recursive method can result in syntax and/or logic errors.
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.