基本概念
函数写在class语句块或者struct语句块中
static 返回类型 函数名(参数类型 参数名1,参数类型,参数名2)
{
函数的代码逻辑
return 返回值,有返回类型才写 可以任意类型包括类 结构体 枚举
}
关于函数名 使用帕斯卡命名发命名,参数名用驼峰。myName(驼峰命名) MyName(帕斯卡命名发)
参数不是必须的 可以有0到n个参数 参数类型也是任意类型 多个参数 逗号隔开
返回值类型不为void时,必须通过return返回对应类型的内容。
函数的实际应用
1无参数无返回值
cs
static void SayHello()
{
Console.WriteLine("hello");
}
2有参数无返回值函数
cs
static void SayName(string name)
{
Console.WriteLine("your name is {0}" ,name);
}
3 无参有返回值函数
cs
static string Whatname()
{
return "fly";
}
4 有参有返回值函数
cs
static int Sum(int a,int b)
{
return a + b;
}
5 有参有多返回值函数
cs
static int[] Calc(int a ,int b)
{
int sum=a+b;
int avg = sum / 2;
return new int []{ sum,avg};
}