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)
            {
                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;
            }
        }
    }
}

 

posted @ 2022-03-04 13:09  电子_精灵  阅读(502)  评论(0编辑  收藏  举报