第二十一节 函数二

实参与形参。

值传递:发生了实参形参的数据传递。程序首先为形参,x,y分配内存空间并把实参a, b的值复制一份给形参x,y.

实参a,b和形参x,y为两组变量,在内存中占据不同的空间,当值传递完成后,他们便是互不相关的量,形参的变化不会影响实参。他们便是互不相关的量,形参的变化不会影

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HanShu
{
    class Program
    {
        static double Bigger(double x, double y)//x,y为形参
        {
            double MaxValue = (x > y) ? x : y;
            return MaxValue;
        }
        static void Main(string[] args)
        {
            double a = Convert.ToDouble(Console.ReadLine());
            double b = Convert.ToDouble(Console.ReadLine());
            double result = Bigger(a,b);//a,b为实参
            Console.WriteLine("最大值为:{0}",result);
        }
    }
}

  地址传递:将实参的地址传给形参即实参和形参指向同一块内存空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Zhi
{
    class Program
    {
        static void Dou(int[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = 2 * array[i];
            }
        }
        static void Main(string[] args)
        {
            int[] number = new int[] {1,2,3,4,5};
            Console.Write("Before");
            foreach (int elment in number)
            {
                Console.Write("\t"+elment);
            }
            Dou(number);
            Console.Write("\nAfter");
            foreach (int elment in number)
            {
                Console.Write("\t" + elment);
            }
        }
    }
}

  当参数类型为int,double等类型时,实参和形参之间进行传递。形参的改变不会影响实参。

参数类型为数组等引用类型时,实参和形参指向同一块内存空间,实参随着形参的变化而变化。

引用类型参数。用ref

static void Swap(ref int x,ref int y)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YinYong
{
    class Program
    {
        static void Swap(ref int x, ref int y)
        {
            int temp;
            temp = x;
            x = y;
            temp = y;
        }
        static void Main(string[] args)
        {
            int a = 40;
            int b = 80;
            Console.WriteLine("交换前a={0} b={1}",a,b);
            Swap(ref a,ref b);
            Console.WriteLine("交换后a={0} b={1}", a, b);
        }
    }
}

输出型参数ref 型参数传入函数前必须赋值。

out 型参数传入前不需要赋值,即使赋值了值也会被忽略,所以out型参数只能用来从函数返回结果,而不能用来向函数传递参数。在函数结束前,必须为out型参数赋值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Outshuc
{
    class Program
    {
        static double Yuan(double r,out double c)
        {
            double s = 2 * Math.PI * r;
            c = Math.PI * r * r;
            return s;
        }
        static void Main(string[] args)
        {
            double r = 156;
            double zhouchang;
            double area = Yuan(r,out zhouchang);
            Console.WriteLine("周长为"+zhouchang);
            Console.WriteLine("面积为"+area);
        }
    }
}

  参数匹配:调用函数时,实参和形参的类型应当匹配,如果不匹配,编译器将尝试进行隐式转化,把实参提升到形参的类型。

如果实参不能转化为形参类型,编译器将报错。或者将实参显示转化为形参。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CanShu
{
    class Program
    {
        static double Ti(double r)
        {
            double v = (4/3) * Math.PI * r * r * r;
            return v;
        }
        static void Main(string[] args)
        {
            double s = Ti(16.51);//参数类型匹配
            Console.WriteLine(s);
            int s1 = 1001;
            Ti(s1);//隐式转化为形参类型
            Console.WriteLine(s1);
            string s2 = "100";
            Console.WriteLine(Convert.ToDouble(s2));//显示转化为形参类型
        }
    }
}

  

posted on 2013-05-02 22:15  杨柳清枫2012  阅读(150)  评论(0)    收藏  举报

导航