函数

函数

一个较大的程序一般应分为若干个程序块,每一个模块用来实现一个特定的功能。所有的高级语言中都有子程序这个概念,用子程序实现模块的功能。在C#语言中,子程序的作用是由一个主函数和若干个函数构成。由主函数调用其它函数,其它函数也可以互相调用。同一个函数可以被一个或多个函数调用任意多次。

     在程序设计中,常将一些常用的功能模块编写成函数,放在函数库中供公共选用。要善于利用函数,以减少重复编写程序段的工作量

函数:能够独立完成某项功能的模块。

函数四要素:输入、输出、函数体、函数名

函数定义:
static/public) 返回类型 函数名(参数类型 参数

分为四种类型:无参数无返回、有参数无返回、有返回无参数、有参数有返回。

函数在program类当中书写。

    class Program

    {

第一种无参数无返还;       

 public void ww()

        {

            Console.Write("请输入一个正整数:");

            int a = int.Parse(Console.ReadLine());

            for (int i = 1; i <= a; i++)

            {

                int ji = 1;

                for (int j = 1; j <= i; j++)

                {            int sum = 0;            

                    ji *= j;

                }

                sum += ji;

            }

            Console.WriteLine(sum);

            Console.ReadLine();

        }

        //2有参数无返回

        public void yw (int b)

    {

       

        int sum = 0;

        for (int i = 1; i <=b;i++)

        {

            int ji = 1;

            for (int j = 1; j <= i; j++)

            {

                ji *= j;

            }

            sum += ji;

        }

        Console.WriteLine(sum);

        }

       //3有参数有返回

         public int yy(int b)

         {

             int sum = 0;

             

        for (int i = 1; i <=b;i++)

        {

            int ji = 1;

            for (int j = 1; j <= i; j++)

            {

                ji *= j;

            }

            sum += ji;

        }

        return sum;

        

         }

          //4有返回无参

         public int wy()

         {

             Console.Write("请输入一个正整数:");

             int a = int.Parse(Console.ReadLine());

             int sum = 0;

             for (int i = 1; i <= a; i++)

             {

                 int ji = 1;

                 for (int j = 1; j <= i; j++)

                 {

                     ji *= j;

                 }

                 sum += ji;

             }

             return sum;

         }  

        static void Main(string[] args)

        {

            不使用函数的情况:

            //Console.Write("请输入一个正整数:");

            //int a = int.Parse(Console.ReadLine());

            //int sum = 0;

            //for (int i = 1; i <=a; i++)

            //{

            //     int ji = 1;  

            //    for (int j = 1; j <= i; j++)

            //    {

            //        ji *= j;

            //    }

            //    sum += ji;

            //}

            //Console.WriteLine(sum);

            //Console.ReadLine();

第一种无参数无返回

            //Program hanshu = new Program();

            //hanshu.ww();

            //Console.ReadLine();

   第二种有参无返

            //Program hanshu1 = new Program();

            //Console.Write("请输入一个正整数:");

            //int a = int.Parse(Console.ReadLine());

 第三种,有参数有返回           

            // hanshu1.yw(a);

            //Console.WriteLine();

            //Console.ReadLine();

 第四中,无参数有返回           

            //Program hanshu2 = new Program();

            //hanshu2.wy();

            //Console.WriteLine();

            //Console.ReadLine();

原文地址:https://www.cnblogs.com/light3857/p/5628822.html