Call the problem (a) method / function
Function is invoked want to get the value of the caller:
- Incoming parameters;
- Use static fields to simulate global variables;
C # is not the concept of global variables, but the use of static fields to simulate global variables;
Between the classes and functions:
//字段, 属于类的字段
public static int _number = 10;
The caller gets the value of the callee
- return value;
- Out parameters (see below)
- An array of exceptions
Parameters of the called function is parameter, is passed by the caller argument; but all open space in memory;
note:
- Functional approach must be single;
- The method is the most taboo word prompt the user; because this thing can only be used in the console;
Using (B) out, ref parameters and Params
Using (1) out of the parameters
One approach, a plurality of the same type of return value, the array can be used; when a plurality of different types of values need to be returned, given out parameters;
public static void Main(string[] args)
{
int[] nums = {1, 2, 3, 78, 56, 45, 85};
int max = 0; // 可以先不赋值
int min = 0; // 可以与被调用函数不同
int sum = 0;
int avg = 0;
Test(nums, out max, out min, out sum, out avg);
Console.WriteLine(max, min, sum, avg);
}
// 带out的参数是多余返回的值
public static viod Test(int[], out int max, out int min ,out int sum, out int avg)
{
//out 参数要求在方法的内部必须为其赋值
max = nums[0];
min = nums[0];
sum = 0;
for (int i = 0; i < nums.Length; i++)
{
if(nums[i]>max) max = nums[i];
if(nums[i]<min) min = nums[i];
sum += nums[i];
}
avg = sum/nums/Length;//不需要return
}
Using (2) ref parameters
ref parameter in C ++ to achieve a similar effect to take the address, so that, in operation of the called function, the arguments are the operations performed, no return value, changes may occur in the arguments;
The method of claim outside must be assigned, the assignment may not be the method;
static void Main(string[] args)
{
int x = 0, y = 0;
Console.WriteLine("请输入两个数字:");
try
{
x = Convert.ToInt32(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("输入错误!");
return;
}
Exchange(ref x, ref y);
Console.WriteLine(x);
Console.WriteLine(y);
}
/// <summary>
/// 这个函数实现交换两个int型的变量
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
public static void Exchange(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
Using (3) params parameters
params is a variable parameter; the argument list, array type parameters consistent with the variable elements are treated as elements of the array;
static void Main(string[] args)
{
Test("zhangSan", 99, 88, 56);
}
// 下面的参数,既可以传数组,也可以传入多个int
// 可变参数数组必须是形参列表中的最后一个;
public static void Test(string name, params int[] score)
{
int sum = 0;
for (int i = 0; i < score.Length; i++)
{
sum += score[i];
}
Console.WriteLine("{0}的成绩是{1}", name, sum);
}
- The variable parameter array must be the last parameter in the list;
- The variable parameters can not use a plurality of arrays;
Overload (iii) Method
Overloaded method refers to the method of the same name, but different parameters; so you can easily call; at the time of the call, you can call in different ways according to different arguments passed;
Indeed Console.WritelLine () This overloaded to use, which can print different types of data;
note:
- If the number of parameters the same, is not the same type of the argument;
- The same parameter types, then the number of parameters can not be the same;
- Overloading and method return value does not matter;
Recursive (d) method
That is the method calls itself
note:
- Have recursion body;
- Have jumped recursive condition; not endless calls;