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 function of a subroutine is composed of a main function and a number of functions. Other functions are called by the main function, others
Functions can also be called to 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 placed in the library for public
Selection. Be good at using functions to reduce the amount of repetitive programming segments.
Void is not a return value, in parentheses is a parameter
Private only in the current class, public, can be used throughout the namespace
function type
The first type: no reference, no return
public void Leijia ()
{
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
Console.WriteLine (sum);
Console.ReadLine ();
}
The second kind, there is no return
public void Leijia (int a)
{
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
Console.WriteLine (sum);
Console.ReadLine ();
}
The third kind, has the participation to return
public int Leijia1 (int b)
{
int sum = 0;
for (int i = 1; I <= b; i++)
{
sum + = i;
}
return sum;
}
The fourth kind, has the return without the reference
public int Leijia2 ()
{
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
return sum;
}
C # Basic functions