C#函数的定义与调⽤
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
*c#函数定义与调⽤
*函数(⽅法)作⽤:
*功能分块处理,使⽤函数可以更好的管理代码。(例如:在多个地⽅出现重复功能的时候)
*函数定义的⽅法名称命名规则⼀般遵循帕斯卡命名法
*函数的分类:
*(1)参数分类:
*⽆参函数
*public void Study()
*有参函数
*public void Study(int num/参数类型,参数名。。。)
*(2)返回值分类:
*有返回值函数:return
*⽆返回值函数
*函数的调⽤:
*⽆参函数的调⽤:Study()
*有参函数的调⽤:Study(符合函数参数类型的具体值)
*注意:调⽤函数时名称必须与具有该功能的函数名称⼀致
*函数的形参与实参:
*形参:
*形参就是函数定义⾥的各种运算。例如:int Fun(int a);
*实参:
*实参是形参被具体赋值之后的值,参加实际的运算,实际作⽤。int a = 5;
*区别:
*1.实参可以是常量,变量,表达式,函数等,但是必须有具体的值,先赋值再⽤。
*2.形参只有在被调⽤的时候才分配内存,调⽤结束释放内存(类似局部变量),因此只有在函数内有效; *3.实参对形参的数据传送是单向的
*4.实参形参占⽤不同的内存空间,所以相互不受影响
*
*
*/
namespace ConsoleApplication1
{
class Program
{
#region
//static void Main(string[] args)
//{
//    XingMing("⼩王","⼩赵");
/
/    Run();
//    Runing();
//    Jump(10);
//    Fly();
//    float score = 3.3f;
//    Num(score);
//    Console.ReadKey();
//}
有参函数
//public static void XingMing(string name1, string name2)//传递多个参数,⽤逗号隔开        //{
//    Console.WriteLine(name1 + "第⼀" + name2 + "第⼆");
/
/}
⽆参函数
//public static void Run()
//{
//    Console.WriteLine("我在跑");
//}
⽆参有返回值函数
//public static string Runing()
//{
//    Console.WriteLine("我正在跑");
//    return "我正在跑";
/
/}
有参有返回值函数
//public static int Jump(int num)//int是有返回值的,void没有返回值
//{
//    Console.WriteLine(num);
//    return num;
//}
string返回string类型的
//public static string Fly()
//{
//    return "A";//string也可返回null,return null;
/
/}
形参与实参
//public static float Num(float score)
//{
//    score = 2.2f;
//    Console.WriteLine("score" + score);
//    return score;
//}
#endregion
static void Main(string[] args)
{
int[] nums = new int[5] { 2,1,5,9,7};
Sort(nums);
for (int i = 0; i < nums.Length; i++)
{
Console.WriteLine(nums[i]);
}
Console.WriteLine("请输⼊第⼀个数");
int numOne = int.Parse(Console.ReadLine());
Console.WriteLine("请输⼊第⼆个数");
int numTwo = int.Parse(Console.ReadLine());
MaxNum(numOne,numTwo);
MinNum(numOne, numTwo);
MaxOrMinNum(numOne,numTwo);
int maxNum = MaxNumTwo(numOne, numTwo);
int minNum = MaxNumTwo(numOne, numTwo);
Console.WriteLine("最⼤值=" + MaxNumTwo(numOne, numTwo));            Console.WriteLine("最⼩值=" + MinNumTwo(numOne, numTwo));            Console.ReadKey();
}
//冒泡排序
public static void Sort(int[] a)
{
for (int i = 0; i < a.Length - 1; i++)
{
for (int j = 0; j < a.Length - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
/
/函数的嵌套
public static void MaxOrMinNum(int a, int b)
{
MaxNum(a,b);
MinNum(a,b);
}
//求最⼤值  函数⼀
public static void MaxNum(int a, int b)
{
if (a > b)
{
Console.WriteLine("最⼤值=" + a);
}
else
{
Console.WriteLine("最⼤值=" + b);
}
}
//求最⼩值  函数⼀
public static void MinNum(int a,int b)
{
if (a < b)
writeline函数{
Console.WriteLine("最⼩值=" + a);
}
else
{
Console.WriteLine("最⼩值=" + b);            }
}
//求最⼤值  函数⼆
public static int MaxNumTwo(int a, int b)        {
if (a > b)
{
return a;
}
else
{
return b;
}
}
//求最⼩值  函数⼆
public static int MinNumTwo(int a, int b)        {
if (a < b)
{
return a;
}
else
{
return b;
}
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。