C # language basics-functions,
Function
A large program is generally divided into several program blocks, each of which is used to implement a specific function. Subprograms are used in all advanced languages to implement the functions of modules. In C #, subprograms are composed of a main function and several functions. Other functions can be called by the main function. The same function can be called multiple times by one or more functions.
In program design, some common functional modules are often written into functions and put in the function library for public selection. Be good at using functions to reduce the workload of repeatedly writing program segments.
1. Functions: a module that can independently complete a function.
Function four elements: input, output, function body, function name
Function Definition:
(Static/public) return type function name (parameter type parameter name, parameter type parameter name)
{
Function body
}
Function call:
Return variable type variable name = function (real parameter value)
Ii. Case study function types
Use the Function Method to Solve the factorial sum, that is, 1! + 2! + 3! +... + N! -- Four function types
Namespace Function
{
Class Program
{
/// <Summary>
/// Method 1: no response and no Parameter
/// </Summary>
Public void jiehe ()
{
Console. Write ("enter a positive integer :");
Int a = int. Parse (Console. ReadLine ());
Int sum = 0;
For (int I = 1; I <= a; I ++)
{
Int jie = 1;
For (int j = 1; j <= I; j ++)
{
Jie * = j;
}
Sum + = jie;
}
Console. WriteLine (sum );
Console. ReadLine ();
}
/// <Summary>
/// Second: No Response Parameter
/// </Summary>
/// <Param name = "a"> </param>
Public void jiehe (int)
{
Int sum = 0;
For (int I = 1; I <= a; I ++)
{
Int jie = 1;
For (int j = 1; j <= I; j ++)
{
Jie * = j;
}
Sum + = jie;
}
Console. WriteLine (sum );
Console. ReadLine ();
}
/// <Summary>
/// The third type: Response parameters
/// </Summary>
/// <Param name = "a"> </param>
/// <Returns> </returns>
Public int jiehe1 (int)
{
Int sum = 0;
For (int I = 1; I <= a; I ++)
{
Int jie = 1;
For (int j = 1; j <= I; j ++)
{
Jie * = j;
}
Sum + = jie;
}
Return sum;
}
/// <Summary>
// Type 4: return without Parameters
/// </Summary>
/// <Returns> </returns>
Public int jiehe2 ()
{
Console. Write ("enter a positive integer :");
Int a = int. Parse (Console. ReadLine ());
Int sum = 0;
For (int I = 1; I <= a; I ++)
{
Int jie = 1;
For (int j = 1; j <= I; j ++)
{
Jie * = j;
}
Sum + = jie;
}
Return sum;
}
Static void Main (string [] args)
{
// Corresponds to the first to four types of functions above, and is written in the main function.
// Type 1: no response and no parameters:
Program hanshu = new Program ();
Hanshu. jiehe ();
// Type 2: No response parameter:
Console. Write ("enter a positive integer :");
Int a = int. Parse (Console. ReadLine ());
Program hanshu = new Program ();
Hanshu. jiehe ();
// Type 3: Response parameters:
Console. Write ("enter a positive integer :");
Int a = int. Parse (Console. ReadLine ());
Program hanshu = new Program ();
Int sum = hanshu. jiehe1 ();
Console. WriteLine (sum );
Console. ReadLine ();
// Type 4: return without parameters:
Program hanshu = new Program ();
Int sum = hanshu. jiehe2 ();
Console. WriteLine (sum );
Console. ReadLine ();
}
}
}
Iii. Examples
1. namespace Hanshu
{
Class Program
{
/// <Summary>
/// Input two numbers to compare the values and return a large value
/// </Summary>
/// <Param name = "a"> </param>
/// <Param name = "B"> </param>
/// <Returns> </returns>
Public double Max (double a, double B)
{
If (a> B)
{
Return;
}
Else
{
Return B;
}
}
Static void Main (string [] args)
{
// Write a function to compare the size
// Compare the size of three numbers
Double a = 3;
Double B = 5, c = 2;
Program hanshu = new Program ();
Double max = hanshu. Max (hanshu. Max (a, B), c );
Console. WriteLine (max );
Console. ReadLine ();
}
}
}
2. Use the function to create a rabbit.