2012年10月29日 星期一

函數的介紹

今天來跟大家簡單介紹一下函數,函數的用途是什麼?通常我用它來做兩件事情。
1、會被重複使用的功能,獨立出來,做成函數。
2、當各種功能的程式碼都擠在同一個區塊的時候的時候,用函數將程式碼包起來,容易閱讀也比較好維護。

在開始寫程式碼之前,先講一下怎麼用函數。
函數與變數一樣,要使用前要先宣告,怎麼宣告?
基本的樣子就像這樣:【可見程度】【回傳型別】【變數名稱】小括弧中放【傳入的變數】後面加上大括弧放程式碼。
【可見程度】可以是【public】、【private】、【protected】。
【回傳型別】可以是任何一種【變數型態】,特別的是【void】,代表不回傳任何東西。
【傳入的變數】可以是任何一種變數型態,也可以不傳入參數。
【變數名稱】可以隨意取。

先來寫一個比數字大小的範例。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UseFunction
{
    class Program
    {
        static void Main(string[] args)
        {
            string result = "";
            int[] SerialNumber = { 1, 2, 3 };
            int Max = 0;

            for (int i = 0; i < SerialNumber.Length; i++)
            {
                if (SerialNumber[i] > Max)
                    Max = SerialNumber[i];
            }
            result = "最大值:" + Max + "\r\n";
            Console.Write(result);
            Console.ReadKey();
        }
    }
}

上面的程式碼我們宣告了一個整數陣列,裡面放了一連串的數字,我想要得到這個數列的最大值,於是我另外宣告了一個整數變數【Max】來存放最大值,接著寫一個迴圈敘述來檢查每一個陣列裡面的元素,如果陣列裡面的元素比【Max】大,那麼就把【Max】的值換成陣列中的元素。

這邊有一個要注意的地方是整數變數【Max】的初始值,【Max】的初始值一定要比所有的陣列元素小,要不然這個判斷邏輯就失效了。

這時候你可能會問,又不一定每次都會知道所有元素的值,怎麼知道【Max】要設多少?不知道元素的話,保險的做法,就是把【Max】設為該型態的最小值,怎麼做?把【Max】的初始值換一下。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UseFunction
{
    class Program
    {
        static void Main(string[] args)
        {
            string result = "";
            int[] SerialNumber = { 1, 2, 3 };
            int Max = int.MinValue;

            for (int i = 0; i < SerialNumber.Length; i++)
            {
                if (SerialNumber[i] > Max)
                    Max = SerialNumber[i];
            }
            result = "最大值:" + Max + "\r\n";
            Console.Write(result);
            Console.ReadKey();
        }
    }
}

好了,接下來我們再增加一個整數陣列,一樣要得到這個數列中的最大值,我們的程式碼可能會變成這樣。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UseFunction
{
    class Program
    {
        static void Main(string[] args)
        {
            string result = "";
            int[] SerialNumber = { 1, 2, 3 };
            int[] SerialNumber_1 = { 4, 5, 6, 7 };

            int Max = int.MinValue;
            for (int i = 0; i < SerialNumber.Length; i++)
            {
                if (SerialNumber[i] > Max)
                    Max = SerialNumber[i];
            }
            result = "最大值:" + Max + "\r\n";
            Console.Write(result);

            Max = int.MinValue;
            for (int i = 0; i < SerialNumber_1.Length; i++)
            {
                if (SerialNumber_1[i] > Max)
                    Max = SerialNumber_1[i];
            }
            result = "最大值:" + Max + "\r\n";
            Console.Write(result);
            
            Console.ReadKey();
        }
    }
}

大家有沒有發現,上面的程式碼有一部分是重複的,就是取得陣列元素中最大值的那段邏輯,只有陣列名稱換成【SerialNumber_1】,其他的都一樣,我們來把這段程式變成函數。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UseFunction
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] SerialNumber = { 1, 2, 3 };
            int[] SerialNumber_1 = { 4, 5, 6, 7 };

            GetMax(SerialNumber);
            GetMax(SerialNumber_1);
            Console.ReadKey();
        }

        private static void GetMax(int[] InputSerialNumber)
        {            
            string result = "";
            int Max = int.MinValue;
            for (int i = 0; i < InputSerialNumber.Length; i++)
            {
                if (InputSerialNumber[i] > Max)
                    Max = InputSerialNumber[i];
            }
            result = "最大值:" + Max + "\r\n";
            Console.Write(result);
        }
    }
}

我們宣告了一個函數,可見層級是【private】,【static】這個先照抄,這邊不解釋,回傳型態是【void】 表示這個函數不會回傳任何東西,接著是函數名稱我們取名為【GetMax】,小括弧裡面放 【int[] InputSerialNumber】,代表我們要傳遞一個整數陣列進去給函數使用,傳遞進去的陣列的名稱為【InputSerialNumber】,大括弧裡裡面放的程式碼就是如何取得數列最大值的邏輯。

我們將取得數列最大值的邏輯獨立出來變成函數,要取得的最大值的數列只要呼叫【GetMax】函數,並把數列傳遞進去,函數會處理有關取得最大值的邏輯,看起來是不是簡潔許多,程式碼也比較方便閱讀與維護。

我們上面的範例改一下,示範一下有回傳資料的函數怎麼使用。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UseFunction
{
    class Program
    {
        static void Main(string[] args)
        {         
            int[] SerialNumber = { 1, 2, 3 };
            int[] SerialNumber_1 = { 4, 5, 6, 7 };

            string result = "";
            result = "最大值:" + GetMax(SerialNumber) + "\r\n";
            Console.Write(result);

            result = "最大值:" + GetMax(SerialNumber_1) +"\r\n";
            Console.Write(result);

            Console.ReadKey();
        }

        private static int GetMax(int[] InputSerialNumber)
        { 
            int Max = int.MinValue;
            for (int i = 0; i < InputSerialNumber.Length; i++)
            {
                if (InputSerialNumber[i] > Max)
                    Max = InputSerialNumber[i];
            }
            return Max;        
        }
    }
}

這邊我們修改了一點程式碼,本來回傳型態【void】改為【int】,並且在函數區塊的最後加上【return】代表要回傳【Max】這個變數的值。

提醒各位,函數可以選擇要回傳會者是不回傳,要回傳也只能回傳一個變數,但是總有些辦法變通的辦法。

如果你去水果行買東西,只剩下一隻手可以拿東西,老闆只能拿一個東西給你,如果你要買同種類的水果很多,老闆可能給你水果禮盒或一整箱未拆封水果,如果你要買梨子蘋果柳丁等都不一樣的水果,老闆會拿塑膠袋全部裝一起給你。

同一種水果很多個的水果禮盒就是List,而裝著各種不同水果的塑膠袋就是【類別】或【結構】,List前面文章介紹過了,這兩個東西說以後會介紹,今天就先到這,改天見。

沒有留言:

張貼留言