C#四部曲基础——函数
一.函数基础
1.基本概念
函数(方法)
(1)本质
一块具有名称的代码块(可以使用函数的名称执行该代码块)
函数(方法)是封装代码进行重复使用的一种机制
(2)作用
①封装代码
②提升代码复用率(少写点代码)
③抽象行为
2.函数写(申明)在哪里
(1)class语句块中
比如static void Main(string[ ] args)
{
主函数就是在class语句块
}
(2)struct(结构体)语句块中
函数不能写在函数语句块内
也就是说Main函数中也不能写函数
3.基本语法
(1) (2) (3) (4)
static 返回类型 函数名(参数类型 参数名1,参数类型 参数名2,……)
{
函数的代码逻辑;
……;
(5)
return 返回值;
}
(1)static
不是必须的,但是在没有学校类(class)和结构体(struct)之前是必须写的
(2)返回类型
引出一个新的关键词 void(表示没有返回值)
如果有返回值
返回类型可以写任意的变量类型
14种变量类型和复杂数据类型(数组,枚举,结构体(struct),类(class))
(3)函数名
使用帕斯卡命名法
myName(驼峰命名法)
MyName(帕斯卡命名法)
(4)参数
参数不是必须的,可以有0~n个参数
参数类型也可以是任意类型
14种变量类型和复杂数据类型(数组,枚举,结构体(struct),类(class))
多个参数用逗号(,)隔开
参数名(驼峰命名法)
(5)返回值
当返回的类型不为void时,必须通过关键词 return 返回对应类型的
即使void 也可以选择性使用return
4.实际运用
(1)无参无返回值函数
static void SayHelloWorld()
{
Console.WriteLine("Hello World!");
}
(2)有参无返回值函数
static void SayYourName(string name)
{
Console.WriteLine("你的名字是:{0}",name);
}
(3)无参有返回值函数
static string WhatYourName()
{
return "念陈cbwindy";
}
(4)有参有返回值函数
static int Sum(int a,int b)
{
return a+b;
}
(5)有参有多返回值函数
static int[] Calc(int a,int b)
{
int sum = a+b;
int avg = sum/2;
int[] arr = {sum , avg};
return arr;
}
(6)运用函数
internal class 函数
{
static void SayHelloWorld()
{
Console.WriteLine("Hello World!");
}
static void SayYourName(string name)
{
Console.WriteLine("你的名字是:{0}", name);
}
static string WhatYourName()
{
return "念陈cbwindy";
}
static int Sum(int a, int b)
{
return a + b;
}
static int[] Calc(int a, int b)
{
int sum = a + b;
int avg = sum / 2;
int[] arr = { sum, avg };
return arr;
}
static void Main(string[] args)
{
SayHelloWorld();
SayYourName("念陈cbwindy");
SayYourName(WhatYourName());
Console.WriteLine(Sum(1,4));
Console.WriteLine("总和为:{0},平均数为:{1}",Calc(4, 8)[0],Calc(4, 8)[1]);
}
}
5.关于return
即使函数没有返回值,也可以用return,return返回后直接不执行之后的代码,直接返回到函数外部。
二.ref和out
1.学习ref和out的原因
可以解决 在函数内部改变外部传入的内容,里面变了,外面也要变
internal class program
{
static void ChangeValue(int value)
{
value = 3;
}
static void ChangeArrayValue(int[] array)
{
array[0] = 99;
}
static void ChangeArray(int[] array)
{
array = new int[]{10,20,30};
}
static void Main(string[] args)
{
int a = 1;
ChangeValue(a);
Console.WriteLine(a);
int[] arr = {1,2,3};
ChangeArrayValue(arr);
Console.WriteLine(arr[0]);
ChangeArray(arr);
Console.WriteLine(arr[0]);
}
}
可以用引用类型和值类型的区别来理解
2.ref和out的使用
其实是函数参数的修饰符
当传入的值类型参数在内部修改时 或者引用类型参数在内部重新申明时
外部的值会发生改变
(1)ref
internal class program
{
static void ChangeValue(ref int value)
{
value = 3;
}
static void ChangeArray(ref int[] array)
{
array = new int[]{10,20,30};
}
static void Main(string[] args)
{
int a = 1;
ChangeValue(ref a);
Console.WriteLine(a);
int[] arr = {1,2,3};
ChangeArray(ref arr);
Console.WriteLine(arr[0]);
}
}
(2)out
internal class program
{
static void ChangeValue(out int value)
{
value = 3;
}
static void ChangeArray(out int[] array)
{
array = new int[]{10,20,30};
}
static void Main(string[] args)
{
int a = 1;
ChangeValue(out a);
Console.WriteLine(a);
int[] arr = {1,2,3};
ChangeArray(out arr);
Console.WriteLine(arr[0]);
}
}
3.ref和out的区别
ref传入的变量必须初始化,out不用
out传入的变量必须在内部赋值,ref不用
ref传入的变量必须初始化,但是在内部,可改可不改
out传入的变量不用初始化,但是在内部,必修修改该值(必须赋值)
三.变长参数和参数默认值
1.变长参数关键词
举例:函数计算n个整数和
class program
{
static int Sum(params int[] arr)
{
int sum = 0;
for(int i = 0;i < arr.Length ; i++)
{
sum += arr[i];
}
return sum;
}
static void Main(string[] args)
{
int sum = Sum(1,2,3,4,5,6,7,8,9);
Console.WriteLine(sum);
}
}
关键字:params
注意:
1.params关键字后面必为数组
2.数组的类型可以是任意的类型
3.函数参数可以有 别的参数和 params关键字修饰的参数
4.函数参数中只能最多出现一个params关键字 并且一定是在最后一组参数 前面可以有n个其他参数
2.参数默认值
有参数默认值的参数 一般称为可选参数
作用:当调用函数时,可以不传入参数,不传就可以使用默认值作为参数的值
注意:
支持多参数默认值,每个参数都可以有默认值
如果要混用,可选参数必须写在普通参数后面
四.函数重载
1.基本概念
在同一个语句块(class或者struct)中,函数(方法)名相同,参数的数量不同或者参数的数量相同,但参数的类型或顺序不同
作用:
命名一组功能相似的函数减少函数名的数量,避免命名空间的污染
提升程序可读性
2.实例
注意:
重载和返回值类型无关,只和参数类型,个数,顺序有关
调用时 程序会自己根据传入的参数类型判断使用哪一个重载
static int Sum(int a,int b)
{
return a + b;
}
(1)参数数量不同
static int Sum(int a,int b,int c)
{
return a + b + c;
}
(2)参数数量相同,类型不同
static float Sum(float a,float b)
{
return a + b;
}
static float Sum(int a,float b)
{
return a + b;
}
(3)数量相同,顺序不同
static float Sum(float a,int b)
{
return a + b;
}
(4)ref 和 out
static float Sum(ref float a,int b)
{
return a + b;
}
(5)验证
internal class Program
{
static int Sum(int a, int b)
{
return a + b;
}
static int Sum(int a, int b, int c)
{
return a + b + c;
}
static float Sum(float a, float b)
{
return a + b;
}
static float Sum(int a, float b)
{
return a + b;
}
static float Sum(float a, int b)
{
return a + b;
}
static float Sum(ref float a, int b)
{
return a + b;
}
static void Main(string[] args)
{
float f = 1.1f;
Console.WriteLine(Sum(1, 2));
Console.WriteLine(Sum(1, 2, 3));
Console.WriteLine(Sum(1.3f, 2.3f));
Console.WriteLine(Sum(1.1f, 2));
Console.WriteLine(Sum(1, 2.3f));
Console.WriteLine(Sum(ref f, 2));
}
}
可选参数不能重载
五.递归函数
1.基本概念
递归函数就是让函数调用自己
一个正确的递归函数:
①必须有结束调用的条件
②用于条件判断的判断的这个条件 必须改变 能够达到停止的目的
2.实例
用递归函数打印出0到10
class Program
{
static void Func(int a)
{
Console.WriteLine(a);
++a;
if(a <= 10)
{
Func(a);
}
}
static void Main(string[] args)
{
Func(0);
}
}
更多推荐
所有评论(0)