C# 函數1 (函數的定義)


復制代碼

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         // 函數的定義方法
11         //  修飾符  返回值 函數名 (參數)
12         // 定義函數. ↓
13         static void MyFuntion1(int i, int t)  //一個無返回值的函數, (Void代表沒有返回值) , MyFuntion是函數名 (int i, int j )是兩個整形參數
14         {
15             int a;
16             a = (i > t) ? i : t;
17             Console.WriteLine("我一個無返回值有參數的函數");
18         }
19 
20         static int myfuntion2(int i, int t) //返回值為整形的函數, 其它的與上面一樣
21         {
22             int a;
23             a = (i > t) ? i : t;
24             Console.WriteLine("我是一個有參數有返回值的函數");
25             return a;  // 用return 關鍵之來表示函數返回. 這句話就表示了.函數返回值為a的值 .  (函數一旦遇到return將立刻返回)
26         }
27 
28         static void MYfuntion3()  //一個無返回值,無參數的函數
29         {
30             Console.WriteLine("我是一個沒有返回值沒有參數的函數");
31         }
32 
33         static int MYFuntion4() // 一個有返回值,無參數的函數
34         {
35             Console.WriteLine("我是一個有返回值,但是無參數的函數");
36             return 40;  // 函數返回值為40
37         }
38 
39         static void Main(string[] args)
40         {
41             int a, b,c;
42             a = 20;
43             b = 50;
44           // 調用上面第一個函數 
45             MyFuntion1(a,b);
46             Console.WriteLine();
47          // 調用上面第二個函數
48             c = myfuntion2(a,b);
49             Console.WriteLine(c + "\n");
50         // 調用上面第三個函數
51             MYfuntion3();
52             Console.WriteLine();
53         // 調用上面第四個函數 
54             c = MYFuntion4();
55             Console.WriteLine(c);
56         }
57     }
58 }

//以上代碼輸出的結果為

    

我一個無返回值有參數的函數

我是一個有參數有返回值的函數
50

我是一個沒有返回值沒有參數的函數

我是一個有返回值,但是無參數的函數
40

 
        
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM