First, function:
A larger program should generally be divided into a number of blocks, each of which is used to implement a specific function. All high-level languages have the concept of subroutines, which implements the function of modules with subroutines. In the C # language, the role of a subroutine is composed of a main function and a number of functions. Other functions are called by the main function, and other functions can be called each other. The same function can be called any number of times by one or more functions.
In the program design, some common function modules are often written into functions, which are put in the library for public selection. Be good at using functions to reduce the amount of repetitive programming segments.
Namespace_ May 11 _ Function {classProgram {//no return value, no parameters Public voidAA () {Console.WriteLine ("Hello world!"); } //no return value, no parameters Public voidLeijia () {//Accumulate sumConsole.Write ("Please enter an integer:"); intA =int. Parse (Console.ReadLine ()); intsum =0; for(inti =1; I <= A; i++) {sum+=i; } Console.WriteLine (sum); } //no return value, parameter Public voidLeijia (intz) {intsum =0; for(intI=1; i<=z;i++) {sum+=i; } Console.WriteLine (sum); } //has a return value, and has parameters Public intLeijia1 (intz) {intsum =0; for(intI=1; i<=z;i++) {sum+=i; } returnSum//return is the meaning of returning } //There are return values, no parameters Public intLeijia2 () {Console.WriteLine ("Please enter an integer:"); intA =int. Parse (Console.ReadLine ()); intsum =0; for(inti =1; I <= A; i++) {sum+=i; } returnsum; } /// <summary> ///two values to compare, return a larger value, or return any value if equal/// </summary> /// <param name= "a" ></param> /// <param name= "B" ></param> /// <returns></returns> Public DoubleMax (DoubleADoubleb) {//Three number comparison size, output a larger value if(A >b) {returnA; } Else//includes a<b and A=b { returnb; } } Static voidMain (string[] args) { //This class is initialized to call the function inside the class.Program Hanshu =NewProgram (); //no return value, no parameters//Hanshu.aa (); //no return value, no parameters//Hanshu. Leijia (); //Random Value//random ran = new random (); //int aa = ran. Next (10); //Accumulate sum//no return value, parameter//Console.WriteLine ("Please enter an integer:"); //int a = Int. Parse (Console.ReadLine ()); ////hanshu. Leijia (a); //There are return values, parameters//int sum = Hanshu. Leijia1 (a);//There is a return value that needs to be received, return sum, return sum value//Console.WriteLine (sum); //There are return values, no parameters//int sum = Hanshu. LEIJIA2 (); //Console.WriteLine (sum); //Three number comparison size, output a larger value//Double A = 4, B = 7, c = 2; //Console.WriteLine (Hanshu. Max (Hanshu. Max (A, B), c));Console.ReadLine (); } }}
Second, exercise: factorial summation sum
namespacefunction Exercise _ factorial summation sum {classProgram {//1, no return value, no parameters//Public void Leijia1 ()//{ //Console.Write ("Please enter a positive integer:"); //int a = Int. Parse (Console.ReadLine ()); //int jie = 1; //int sum = 0; //for (int i = 1; i <= A; i++)//{ //Jie *= i; //sum + = Jie; //} //Console.WriteLine (sum); //} //2, no return value, parameter//Public void leijia2 (int x)//{ //int jie = 1; //int sum = 0; //for (int i = 1; i <= x; i++)//{ //Jie *= i; //sum + = Jie; //} //Console.WriteLine (sum); //} //3, there is a return value, no parameters//public int Leijia3 ()//{ //Console.Write ("Please enter a positive integer:"); //int a = Int. Parse (Console.ReadLine ()); //int jie = 1; //int sum = 0; //for (int i = 1; i <= A; i++)// { //Jie *= i; //sum + = Jie; // } //return sum; //} //4, has the return value, has the parameter//public int leijia4 (int y)//{ //int jie = 1; //int sum = 0; //for (int i = 1; I <= y; i++)// { //Jie *= i; //sum + = Jie; // } //return sum; //} Static voidMain (string[] args) { //Initialize this classProgram Hanshu =NewProgram (); //1, no return value, no parameters//hanshu.leijia1 (); //2, no return value, parameter//Console.Write ("Please enter a positive integer:"); //int a = Int. Parse (Console.ReadLine ()); //Hanshu.leijia2 (a);//you need to pass a value to the X in "public void leijia2 (int x)" above the parameter.//The Hanshu received below is printed directly with the initialization of the//3, there is a return value, no parameters//int sum = HANSHU.LEIJIA3 (); //Console.WriteLine (sum); //4, has the return value, has the parameter//Console.Write ("Please enter a positive integer:"); //int a = Int. Parse (Console.ReadLine ()); //int sum = HANSHU.LEIJIA4 (a); //Console.WriteLine (sum);Console.ReadLine (); } }}
May 11 function, exercise: factorial summation sum