C#基础4:函数+ref和out参数

 

PS:注释和讲解全在代码中

1. 简单函数

文档注释作用图:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C4_程序设计
{
    class 函数
    {
        static void Main()
        {
            int p;
            long[] a;       //长整数类型,和C++中的long long一样
            long ans;
            a = new long[60];
            for (int i = 0; i < a.Length; i++)
            {
                if (i <= 2)
                    a[i] = 1;
                else
                    a[i] = a[i - 1] + a[i - 2];
            }
            PrintFib(a);
            p = Convert.ToInt32(Console.ReadLine());
            ans = TheSum(a, p);
            Console.WriteLine(ans);

            int c, d;
            string C, D;
            c = 1; d = 2; C = "1"; D = "2";
            Console.WriteLine(Add(c, d));           //输出结果:3
            Console.WriteLine(Add(C, D));           //输出结果:12
        }
        static void PrintFib(long[] temp)          //Pascal命名法:所有单词首字母大写,其它小写,主要用于函数和类名的定义,其实就是驼峰命名法把第一个字母改成大写
        {
            for (int i = 0; i < temp.Length; i++)
                Console.Write(temp[i]+" ");
            Console.Write("\n");
            return;
        }
        //三个斜杠为文档注释,主要用于对函数功能性的描述
        /// <summary>
        /// 求斐波那契数列的前p项和,保证p∈[0, 60]
        /// </summary>
        /// <param name="temp">数组</param>
        /// <param name="p">上述p</param>
        /// <returns>前缀和</returns>
        static long TheSum(long[] temp, int p)
        {
            long ans;
            ans = 0;
            if (p <= -1 || p >= 61)
            {
                Console.WriteLine("输入错误");
                return -1;
            }
            for (int i = 0; i <= p - 1; i++)
                ans += temp[i];
            return ans;
        }

        //函数重载:和C++一样,没什么可讲的
        static int Add(int a, int b)
        {
            return a + b;
        }
        static string Add(string a, string b)
        {
            return a + b;
        }
    }
}

 

2. ref和out参数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C4_程序设计
{
    class 函数的高级参数
    {
        static void Main()
        {
            //求半径为p的圆的面积
            double p;
            p = Convert.ToDouble(Console.ReadLine());
            Aera(ref p);
            Console.WriteLine(p);

            //求a和b的最大值和平均数
            double ave;
            int a, b, max, c;
            //Console.WriteLine("max = {0}, ave = {1}", max, ave);      错误:使用了未赋值的局部变量“max”和“ave”
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            c = Jud(a, b, out max, out ave);
            Console.WriteLine("max = {0}, ave = {1}", max, ave);
            Console.WriteLine("c = {0}", c);
        }
        static void Aera(ref double p)          //ref参数:引用传递,和C++中的double &p作用一致!
        {
            p = p * p * 3.1415926;
        }

        static int Jud(int a, int b, out int max, out double ave)       //out参数:若函数需要返回多个值,就要用到out参数来返回多余的值
        {
            if (a >= b)
                max = a;
            else
                max = b;
            ave = (1.0 * a + b) / 2;
            return 1;
        }
    }
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值