Cơ sở dữ liệu - Hàm

pdf 53 trang vanle 1940
Bạn đang xem 20 trang mẫu của tài liệu "Cơ sở dữ liệu - Hàm", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfco_so_du_lieu_ham.pdf

Nội dung text: Cơ sở dữ liệu - Hàm

  1. Hàm v 1.0 - 10/2012 Lê Viết Mẫn - lvman@hce.edu.vn 1 Hàm Monday, September 9, 13
  2. chúng ta đã học Tuần tự Rẽ nhánh Lặp Câu lệnh if, switch Câu lệnh for, while, do Lê Viết Mẫn - lvman@hce.edu.vn 2 Hàm Monday, September 9, 13
  3. nhưng using System; namespace ConsoleApplication { class Program { static void Main(string[] args) { } } } Toàn bộ code của chúng ta được viết tại đây • Khó để xử lý các bài toán lớn, phức tạp Lê Viết Mẫn - lvman@hce.edu.vn 3 Hàm Monday, September 9, 13
  4. Nội dung 1. Hàm 1.1. Cơ bản về hàm 1.2. Truyền tham số 1.3. Các đặc tính mới về hàm trong C# 1.4. Các dạng hàm Main 2. Đệ qui Lê Viết Mẫn - lvman@hce.edu.vn 4 Hàm Monday, September 9, 13
  5. Cơ bản về Hàm Function Lê Viết Mẫn - lvman@hce.edu.vn 5 Hàm Monday, September 9, 13
  6. Hàm (1/3) • Kỹ thuật cho phép chia nhỏ một chương trình lớn ra nhiều chương trình nhỏ • Module hóa chương trình - mỗi hàm đảm nhiệm một vài chức năng nào đó của chương trình • Ví dụ : tiền tệ của Mỹ • Input • Output • Xử lý • Lợi ích : • Giảm nhẹ công việc cho người lập trình • viết chương trình nhỏ và gỡ lỗi chúng, sau đó lắp các chương trình đó thành một chương trình • Giảm những đoạn mã nguồn lặp lại • Sử dụng lại mã nguồn trong nhiều chương trình Lê Viết Mẫn - lvman@hce.edu.vn 6 Hàm Monday, September 9, 13
  7. Hàm (2/3) • Khi được sử dụng hay được gọi, int a = 100; hàm có thể được xử lý như một int b = 25; hộp đen • Người sử dụng chỉ quan tâm nó làm gì, chứ không quan tâm nó làm thế nào (hiệu năng, lỗi, ) • Console.WriteLine • Nguyên mẫu hàm mô tả cách kết nối với nó hoặc gọi nó • Các tham số của hàm có thể nhận • Hằng • Biến • Biểu thức Lê Viết Mẫn - lvman@hce.edu.vn 7 Hàm Monday, September 9, 13
  8. Hàm (3/3) Lời gọi hàm Lời gọi hàm phải tương như một câu ứng với định lệnh tuần tự nghĩa hàm Việc thực thi chương trình sẽ tiếp tục tại câu lệnh theo sau lời gọi hàm Lê Viết Mẫn - lvman@hce.edu.vn 8 Hàm Monday, September 9, 13
  9. Định nghĩa hàm • type - kiểu của giá trị trả về • không trả về dữ liệu thì sử dụng void name - tên của hàm • static type name(arguments) • tuân thủ cách đặt tên định danh { statements • arguments - danh sách tham số } • danh sách tên các tham số và kiểu dữ liệu của tham số, cách nhau dấu phẩy static int addition(int a, int b) • các tham số có tên và có thể đặt tên { theo quy tắc đặt tên biến return a + b; } • statements - thân hàm • Biến địa phương Các câu lệnh Định nghĩa hàm trong phạm vi lớp • Program, ngoài phạm vi hàm Main Lê Viết Mẫn - lvman@hce.edu.vn 9 Hàm Monday, September 9, 13
  10. Cách đặt tên hàm • Sử dụng ký hiệu Pascal để đặt tên hàm • Viết hoa ký tự đầu tiên của mỗi từ Add, IndexOf, RemoveAll • Nên dùng động từ hoặc cụm động từ để đặt tên cho hàm • Đặc biệt, hàm có • kiểu trả về là void nên bắt đầu bằng một động từ PrintResults • kiểu trả về là bool thì nên là bắt đầu bằng từ Is IsActivated • tên bắt đầu bằng từ Get để lấy dữ liệu (từ người sử dụng) GetDataFormFile • tên bắt đầu bằng từ Set để gán dữ liệu SetName Lê Viết Mẫn - lvman@hce.edu.vn 10 Hàm Monday, September 9, 13
  11. Lời gọi hàm • Hàm được gọi thông qua tên hàm theo sau là danh sách tham số • kiểu dữ liệu của các tham số không cần phải chỉ ra trong lời gọi hàm • không có tham số thì vẫn cần dùng hai dấu mở và đóng ngoặc • có thể bỏ qua giá trị trả về static int addition(int a, int b) z = addition( 5 , 3 ); • Ví dụ double payment = calcPayment(P, R, N); double payment = calcPayment(100000, 0.08/12, 360); double pseudo_random = random( ); calcPayment(100000, 0.08/12, 360); Lê Viết Mẫn - lvman@hce.edu.vn 11 Hàm Monday, September 9, 13
  12. Ví dụ - Ants go marching The ants go marching one by one hoorah, hoorah The ants go marching seven by seven hoorah, hoorah The ants go marching two by two hoorah, hoorah The ants go marching eight by eight hoorah, The ants go marching three by three. The little hoorah one stops to climb a tree The ants go marching nine by nine. The little one And they all go marching down to the ground to stops to check the time go out of the rain. And they all go marching down to the ground to Boom. Boom. Boom. Boom. Boom. Boom. Boom. go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching four by four hoorah, Boom. hoorah The ants go marching ten by ten. The little one The ants go marching five by five hoorah, hoorah stops to say the ends The ants go marching six by six. The little one And they all go marching down to the ground to stops to pick up sticks go out of the rain. And they all go marching down to the ground to Boom. Boom. Boom. Boom. Boom. Boom. Boom. go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Lê Viết Mẫn - lvman@hce.edu.vn 12 Hàm Monday, September 9, 13
  13. Ví dụ - Các đoạn The ants go marching one by one hoorah, hoorah The ants go marching seven by seven hoorah, hoorah The ants go marching two by two hoorah, hoorah The ants go marching eight by eight hoorah, The ants go marching three by three. The little hoorah one stops to climb a tree The ants go marching nine by nine. The little one And they all go marching down to the ground to stops to check the time go out of the rain. And they all go marching down to the ground to Boom. Boom. Boom. Boom. Boom. Boom. Boom. go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching four by four hoorah, Boom. hoorah The ants go marching ten by ten. The little one The ants go marching five by five hoorah, hoorah stops to say the ends The ants go marching six by six. The little one And they all go marching down to the ground to stops to pick up sticks go out of the rain. And they all go marching down to the ground to Boom. Boom. Boom. Boom. Boom. Boom. Boom. go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Lê Viết Mẫn - lvman@hce.edu.vn 13 Hàm Monday, September 9, 13
  14. Ví dụ - Các đoạn The ants go marching one by one hoorah, hoorah The ants go marching seven by seven hoorah, hoorah The ants go marching two by two hoorah, hoorah The ants go marching eight by eight hoorah, The ants go marching three by three. The little hoorah one stops to climb a tree The ants go marching nine by nine. The little one And they all go marching down to the ground to stops to check the time go out of the rain. And they all go marching down to the ground to Boom. Boom. Boom. Boom. Boom. Boom. Boom. go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching four by four hoorah, Boom. hoorah The ants go marching ten by ten. The little one The ants go marching five by five hoorah, hoorah stops to say the ends The ants go marching six by six. The little one And they all go marching down to the ground to stops to pick up sticks go out of the rain. And they all go marching down to the ground to Boom. Boom. Boom. Boom. Boom. Boom. Boom. go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Lê Viết Mẫn - lvman@hce.edu.vn 14 Hàm Monday, September 9, 13
  15. Ví dụ - Điệp khúc The ants go marching one by one hoorah, hoorah The ants go marching seven by seven hoorah, hoorah The ants go marching two by two hoorah, hoorah The ants go marching eight by eight hoorah, The ants go marching three by three. The little hoorah one stops to climb a tree The ants go marching nine by nine. The little one And they all go marching down to the ground to stops to check the time go out of the rain. And they all go marching down to the ground to Boom. Boom. Boom. Boom. Boom. Boom. Boom. go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching four by four hoorah, Boom. hoorah The ants go marching ten by ten. The little one The ants go marching five by five hoorah, hoorah stops to say the ends The ants go marching six by six. The little one And they all go marching down to the ground to stops to pick up sticks go out of the rain. And they all go marching down to the ground to Boom. Boom. Boom. Boom. Boom. Boom. Boom. go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Lê Viết Mẫn - lvman@hce.edu.vn 15 Hàm Monday, September 9, 13
  16. Ví dụ - Ants go marching • In đoạn thơ với một con số truyền vào • In dòng thay đổi ở cuối mỗi đoạn thơ • Chú ý với số 10 • In điệp khúc • Một hàm in đoạn thơ • Một hàm in điệp khúc Lê Viết Mẫn - lvman@hce.edu.vn 16 Hàm Monday, September 9, 13
  17. // Print the lyric of song Ants go marching using System; namespace AntMarching { class Program { static void PrintVerse(int n) { // In kho tho cua bai hat "Ants go marching" if (n == 10) { Console.Write("The ants go marching {0} by {0} ", n); Console.Write("The little one stops to"); } else { Console.Write("The ants go marching {0} by {0}", n); Console.WriteLine(" hoorah, hoorah"); !! Console.Write("The ants go marching {0} by {0}", n+1); Console.WriteLine(" hoorah, hoorah"); !! Console.Write("The ants go marching {0} by {0}", n+2); Console.WriteLine(" The little one stops to "); } } static void PrintChorus() { // In diep khuc Console.Write("And they all go marching down "); Console.WriteLine("to the ground to go out of the rain."); ! Console.WriteLine("Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom."); } static void Main(string[] args) { PrintVerse(1); Console.WriteLine("climb a tree"); PrintChorus(); PrintVerse(4); Console.WriteLine("pick up sticks"); PrintChorus(); PrintVerse(7); Console.WriteLine("check the time"); PrintChorus(); PrintVerse(10); Console.WriteLine("say the end"); PrintChorus(); Console.ReadKey(); } } } Lê Viết Mẫn - lvman@hce.edu.vn 17 Hàm Monday, September 9, 13
  18. // Print the lyric of song Ants go marching using System; namespace AntMarching { class Program { static void PrintVerse(int n, string end) { // In kho tho cua bai hat "Ants go marching" if (n == 10) { Console.Write("The ants go marching {0} by {0} ", n); } else { Console.Write("The ants go marching {0} by {0}", n); Console.WriteLine(" hoorah, hoorah"); !! Console.Write("The ants go marching {0} by {0}", n+1); Console.WriteLine(" hoorah, hoorah"); !! Console.Write("The ants go marching {0} by {0}", n+2); } Console.Write("The little one stops to {0}", end); } static void PrintChorus() { // In diep khuc Console.Write("And they all go marching down "); Console.WriteLine("to the ground to go out of the rain."); ! Console.WriteLine("Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom."); } static void Main(string[] args) { PrintVerse(1, "climb a tree"); PrintChorus(); PrintVerse(4, "pick up sticks"); PrintChorus(); PrintVerse(7, "check the time"); PrintChorus(); PrintVerse(10, "say the end"); PrintChorus(); Console.ReadKey(); } } } Lê Viết Mẫn - lvman@hce.edu.vn 18 Hàm Monday, September 9, 13
  19. Giá trị trả về static int addition(int a, int b) 8 z = addition( 5 , 3 ); • Giá trị trả về mặc định của hàm là int (nhưng vẫn phải ghi tường minh) • kiểu trả về void được sử dụng cho các hàm không trả lại một giá trị nào • Hàm có thể thuộc vào các dạng sau • Tính toán và trả ra một giá trị (kết quả) dựa trên các tham số đầu vào • Thay đổi giá trị của các tham số đầu vào và trả ra một giá trị (thông báo tình trạng) • Không trả ra giá trị nào : thay đổi các tham số đầu vào, thay đổi giá trị toàn cục hay thực hiện một vài hành vi nào đó (in dữ liệu ra màn hình) • Hàm kết thúc sau khi thực hiện hết các lệnh trong hàm hoặc gặp câu lệnh return • return có thể được gọi bất kỳ đâu trong thân hàm Lê Viết Mẫn - lvman@hce.edu.vn 19 Hàm Monday, September 9, 13
  20. // Print the lyric of song Ants go marching using System; namespace AntMarching { class Program { static int PrintVerse(int n, string end) { // In kho tho cua bai hat "Ants go marching" if (n == 10) { Console.Write("The ants go marching {0} by {0} ", n); } else { Console.Write("The ants go marching {0} by {0}", n); Console.WriteLine(" hoorah, hoorah"); !! Console.Write("The ants go marching {0} by {0}", ++n); Console.WriteLine(" hoorah, hoorah"); !! Console.Write("The ants go marching {0} by {0}", ++n); } Console.Write("The little one stops to {0}", end); return n; } static void PrintChorus() { // In diep khuc Console.Write("And they all go marching down "); Console.WriteLine("to the ground to go out of the rain."); ! Console.WriteLine("Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom."); } static void Main(string[] args) { int count = 1; count = PrintVerse(count, "climb a tree"); PrintChorus(); count = PrintVerse(++count, "pick up sticks"); PrintChorus(); count = PrintVerse(++count, "check the time"); PrintChorus(); count = PrintVerse(++count, "say the end"); PrintChorus(); Console.ReadKey(); } } } Lê Viết Mẫn - lvman@hce.edu.vn 20 Hàm Monday, September 9, 13
  21. Ví dụ - tiền tệ của Mỹ • Input : số tiền - money (pennies) • Output : 1 dollars 3 quarters 2 dimes 1 nickels 4 pennies • Xử lý : • dollars = money / 100 • quarters = phần còn lại của money / 25 • dimes = phần còn lại của money / 10 • nickels = phần còn lại của money / 5 • pennies = phần còn lại của money Lê Viết Mẫn - lvman@hce.edu.vn 21 Hàm Monday, September 9, 13
  22. // a program that converts a number of pennies into the equivalant // value expressed in dollars, quarters, dimes, nickels, and pennies. using System; namespace Penny { class Program { static void Main(string[] args) { int!money; Console.Write("Enter the number of pennies: "); ! money = int.Parse(Console.ReadLine()); Console.WriteLine("{0} pennies =", money); ! int!dollars = money / 100; ! money = money % 100; ! int!quarters = money / 25; ! money %= 25; ! int!dimes = money / 10; ! money %= 10; Module hóa chương trình này ? ! int!nickels = money / 5; ! money %= 5; • Tách các bước tính số ! int!pennies = money; dollar, số quarter, số dimes Console.WriteLine("{0} dollars", dollars); Console.WriteLine("{0} quarters", quarters); Console.WriteLine("{0} dimes", dimes); và số nikel Console.WriteLine("{0} nickels", nickels); Console.WriteLine("{0} pennies", pennies); Console.ReadKey(); • Các hàm này sẽ nhận một } } số money và trả về kết quả } tính được Lê Viết Mẫn - lvman@hce.edu.vn 22 Hàm Monday, September 9, 13
  23. // a program that converts a number of pennies into the equivalant // value expressed in dollars, quarters, dimes, nickels, and pennies. using System; static void Main(string[] args) { namespace Penny int money; { class Program Console.Write("Enter the number of pennies: "); { money = int.Parse(Console.ReadLine()); static int CalcDollars(int money) { Console.WriteLine("{0} pennies =", money); return money / 100; } int dollars = CalcDollars(money); money = money % 100; static int CalcQuarters(int money) { int quarters = CalcQuarters(money); return money / 25; money = money % 25; } int dimes = CalcDimes(money); static int CalcDimes(int money) money %= 10; { return money / 10; int nickels = CalcNickels(money); } money %= 5; static int CalcNickels(int money) int pennies = money; { return money / 5; Console.WriteLine("{0} dollars", dollars); } Console.WriteLine("{0} quarters", quarters); Console.WriteLine("{0} dimes", dimes); Console.WriteLine("{0} nickels", nickels); Console.WriteLine("{0} pennies", pennies); Console.ReadKey(); } } } Lê Viết Mẫn - lvman@hce.edu.vn 23 Hàm Monday, September 9, 13
  24. Truyền tham số Lê Viết Mẫn - lvman@hce.edu.vn 24 Hàm Monday, September 9, 13
  25. Phạm vi của biến • .NET cấp phát cho mỗi hàm một heap vùng nhớ có kích thước cố định myobj = new Object() trong bộ nhớ stack • Các biến được tạo ra trong hàm nào có phạm vi hoạt động trong stack hàm đó main() • Khi hết phạm vi của hàm thì biến đó bị int y square(y) huỷ square(y) • Giá trị của biến của hàm này thì hàm s = y * y khác không thể sử dụng hoặc thay đổi dữ liệu, trừ khi static data code Lê Viết Mẫn - lvman@hce.edu.vn 25 Hàm Monday, September 9, 13
  26. Kiểu trị & kiểu tham chiếu Biến kiểu Kiểu trị Kiểu tham chiếu Chứa Giá trị Tham chiếu Lưu trữ Stack Heap Giá trị khởi tạo 0, false, ‘\0’ null Phép gán sao chép giá trị sao chép tham chiếu int i = 17; string s = “Hello”; Ví dụ int j = i; string s1 = s; i 17 s 0x0a10 Hello j 17 s1 0x0a10 stack stack heap Lê Viết Mẫn - lvman@hce.edu.vn 26 Hàm Monday, September 9, 13
  27. Tham trị 0xff10 0xef00 5 static void Main() a 5 5 i { ! int a = 5; ! Func(a); 0xff10 0xef00 } a 5 6 i static void Func(int i) thời gian { ! i = i + 1; 0xff10 } a 5 Lê Viết Mẫn - lvman@hce.edu.vn 27 Hàm Monday, September 9, 13
  28. Tham chiếu - ref 0xff10 0xef00 0xff10 static int Main() a 5 0xff10 i { ! int a = 5; ! Func(ref a); 0xff10 0xef00 } a 6 0xff10 i static void Func(ref int i) { thời gian ! i = i + 1; 0xff10 } a 6 Lê Viết Mẫn - lvman@hce.edu.vn 28 Hàm Monday, September 9, 13
  29. Ví dụ Hoán đổi giá trị hai biến static void Swap (ref int v1, ref int v2) a 10 b 20 { ! int temp = v1; ! v1 = v2; a 10 b 20 temp 10 ! v2 = temp; } a 20 b 20 temp 10 static int Main () thời gian { a 20 b 10 temp 10 ! int a = 10, b = 20; ! ! Swap(ref a, ref b); a 20 b 10 } Lê Viết Mẫn - lvman@hce.edu.vn 29 Hàm Monday, September 9, 13
  30. // Print the lyric of song Ants go marching using System; namespace AntMarching { class Program { static void PrintVerse(int n, string end) { // In kho tho cua bai hat "Ants go marching" if (n == 10) { Console.Write("The ants go marching {0} by {0} ", n); } else { Console.Write("The ants go marching {0} by {0}", n); Console.WriteLine(" hoorah, hoorah"); !! Console.Write("The ants go marching {0} by {0}", n+1); Console.WriteLine(" hoorah, hoorah"); !! Console.Write("The ants go marching {0} by {0}", n+2); } Console.Write("The little one stops to {0}", end); } static void PrintChorus() { // In diep khuc Console.Write("And they all go marching down "); Console.WriteLine("to the ground to go out of the rain."); ! Console.WriteLine("Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom."); } static void Main(string[] args) { PrintVerse(1, "climb a tree"); PrintChorus(); PrintVerse(4, "pick up sticks"); PrintChorus(); PrintVerse(7, "check the time"); PrintChorus(); PrintVerse(10, "say the end"); PrintChorus(); Console.ReadKey(); } Yêu cầu : Sử dụng tham chiếu } } Lê Viết Mẫn - lvman@hce.edu.vn 30 Hàm Monday, September 9, 13
  31. // Print the lyric of song Ants go marching using System; namespace AntMarching { class Program { static void PrintVerse(ref int n, string end) { // In kho tho cua bai hat "Ants go marching" if (n == 10) { Console.Write("The ants go marching {0} by {0} ", n); } else { Console.Write("The ants go marching {0} by {0}", n++); Console.WriteLine(" hoorah, hoorah"); Console.Write("The ants go marching {0} by {0}", n++); Console.WriteLine(" hoorah, hoorah"); Console.Write("The ants go marching {0} by {0}", n++); } Console.Write("The little one stops to {0}", end); } static void PrintChorus() { // In diep khuc Console.Write("And they all go marching down "); Console.WriteLine("to the ground to go out of the rain."); Console.WriteLine("Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom."); } static void Main(string[] args) { int i = 1; PrintVerse(ref i, "climb a tree"); PrintChorus(); PrintVerse(ref i, "pick up sticks"); PrintChorus(); PrintVerse(ref i, "check the time"); PrintChorus(); PrintVerse(ref i, "say the end"); PrintChorus(); Console.ReadKey(); } } } Lê Viết Mẫn - lvman@hce.edu.vn 31 Hàm Monday, September 9, 13
  32. // a program that converts a number of pennies into the equivalant // value expressed in dollars, quarters, dimes, nickels, and pennies. using System; static void Main(string[] args) { namespace Penny int money; { class Program Console.Write("Enter the number of pennies: "); { money = int.Parse(Console.ReadLine()); static int CalcDollars(int money) { Console.WriteLine("{0} pennies =", money); return money / 100; } int dollars = CalcDollars(money); money = money % 100; static int CalcQuarters(int money) { int quarters = CalcQuarters(money); return money / 25; money = money % 25; } int dimes = CalcDimes(money); static int CalcDimes(int money) money %= 10; { return money / 10; int nickels = CalcNickels(money); } money %= 5; static int CalcNickels(int money) int pennies = money; { return money / 5; Console.WriteLine("{0} dollars", dollars); } Console.WriteLine("{0} quarters", quarters); Console.WriteLine("{0} dimes", dimes); Console.WriteLine("{0} nickels", nickels); Console.WriteLine("{0} pennies", pennies); Console.ReadKey(); } } Yêu cầu : Sử dụng tham chiếu } Lê Viết Mẫn - lvman@hce.edu.vn 32 Hàm Monday, September 9, 13
  33. // a program that converts a number of pennies into the equivalant // value expressed in dollars, quarters, dimes, nickels, and pennies. using System; namespace Penny { class Program { static int calcDollars(ref int money) { int dollars = money / 100; static void Main(string[] args) money = money % 100; { return dollars; int money; } Console.Write("Enter the number of pennies: "); static int calcQuarters(ref int money) money = int.Parse(Console.ReadLine()); { int quarters = money / 25; Console.WriteLine("{0} pennies =", money); money = money % 25; return quarters; int dollars = calcDollars(ref money); } int quarters = calcQuarters(ref money); int dimes = calcDimes(ref money); static int calcDimes(ref int money) int nickels = calcNickels(ref money); { int pennies = money; int dimes = money / 10; money = money % 10; Console.WriteLine("{0} dollars", dollars); return dimes; Console.WriteLine("{0} quarters", quarters); } Console.WriteLine("{0} dimes", dimes); Console.WriteLine("{0} nickels", nickels); static int calcNickels(ref int money) Console.WriteLine("{0} pennies", pennies); { Console.ReadKey(); int nickels = money / 5; } money = money % 5; } return nickels; } } Lê Viết Mẫn - lvman@hce.edu.vn 33 Hàm Monday, September 9, 13
  34. Tham chiếu - out 0xff10 0xef00 0xff10 static int Main() a un 0xff10 i { ! int a; ! Func(out a); 0xff10 0xef00 } a 10 0xff10 i static void Func(out int i) { thời gian ! i = 10; 0xff10 } a 10 Lê Viết Mẫn - lvman@hce.edu.vn 34 Hàm Monday, September 9, 13
  35. // a program that converts a number of pennies into the equivalant // value expressed in dollars, quarters, dimes, nickels, and pennies. using System; namespace Penny { class Program { static int calcDollars(ref int money) { static void Main(string[] args) int dollars = money / 100; { money = money % 100; int money; return dollars; } Console.Write("Enter the number of pennies: "); money = int.Parse(Console.ReadLine()); static int calcQuarters(ref int money) { Console.WriteLine("{0} pennies =", money); int quarters = money / 25; money = money % 25; int dollars = calcDollars(ref money); return quarters; int quarters = calcQuarters(ref money); } int dimes = calcDimes(ref money); int nickels = calcNickels(ref money); static int calcDimes(ref int money) int pennies = money; { int dimes = money / 10; Console.WriteLine("{0} dollars", dollars); money = money % 10; Console.WriteLine("{0} quarters", quarters); return dimes; Console.WriteLine("{0} dimes", dimes); } Console.WriteLine("{0} nickels", nickels); Console.WriteLine("{0} pennies", pennies); static int calcNickels(ref int money) Console.ReadKey(); { } int nickels = money / 5; } money = money % 5; } return nickels; } Yêu cầu : Sử dụng tham chiếu out Lê Viết Mẫn - lvman@hce.edu.vn 35 Hàm Monday, September 9, 13
  36. // a program that converts a number of pennies into the equivalant // value expressed in dollars, quarters, dimes, nickels, and pennies. using System; namespace Penny { class Program { static void GetMoney(out int money) { Console.Write("Enter the number of pennies: "); static void Main(string[] args) money = int.Parse(Console.ReadLine()); { } int money; GetMoney(out money); static int CalcDollars(ref int money) { Console.WriteLine("{0} pennies =", money); int dollars = money / 100; money = money % 100; int dollars = CalcDollars(ref money); return dollars; int quarters = CalcQuarters(ref money); } int dimes = CalcDimes(ref money); int nickels = CalcNickels(ref money); static int CalcQuarters(ref int money) int pennies = money; { int quarters = money / 25; Console.WriteLine("{0} dollars", dollars); money = money % 25; Console.WriteLine("{0} quarters", quarters); return quarters; Console.WriteLine("{0} dimes", dimes); } Console.WriteLine("{0} nickels", nickels); Console.WriteLine("{0} pennies", pennies); static int CalcDimes(ref int money) Console.ReadKey(); { } int dimes = money / 10; } money = money % 10; } return dimes; } static int CalcNickels(ref int money) { int nickels = money / 5; money = money % 5; return nickels; } Lê Viết Mẫn - lvman@hce.edu.vn 36 Hàm Monday, September 9, 13
  37. Các đặc tính khác của hàm trong C# Lê Viết Mẫn - lvman@hce.edu.vn 37 Hàm Monday, September 9, 13
  38. Tham số mặc định • Các giá trị mặc định được sử dụng khi người sử dụng không cung cấp một giá trị cho tham số của hàm • Phải được đặt sau các tham số không được xác định giá trị mặc định • Ví dụ static void Size(int s = 0) Size(5)!! // no default, s = 5 Size()!! // s default to 0 static void F(int a, int b = 0, int c = 10) F(1, 2, 3); F(1, 2);! // c defaults to 10 F(1);!! // defaults taken : b = 0, c = 10 • Không thể vi phạm các luật về tham số hàm static void F(int i, int j = 0) static void F(int i)!! ! ! // conflicts with default above Lê Viết Mẫn - lvman@hce.edu.vn 38 Hàm Monday, September 9, 13
  39. Chồng hàm • Các hàm có cùng tên (do người sử dụng cung cấp) • Phải có danh sách tham số riêng • Khác nhau về số lượng tham số F(void), F(int i), F(int i, int j) • Khác nhau về kiểu của các tham số F(int i), F(string s), F(double d) • Khác nhau về kiểu của các tham số tại vị trí tương ứng G(int i, string s), G(string s, int i) • Nên có cách sử dụng tương tự nhau • Không nên (không thể) chồng hàm dựa trên kiểu trả về Lê Viết Mẫn - lvman@hce.edu.vn 39 Hàm Monday, September 9, 13
  40. Hàm Main Lê Viết Mẫn - lvman@hce.edu.vn 40 Hàm Monday, September 9, 13
  41. Các dạng hàm Main static void Main(string[] args) { } static void Main() { } static int Main(string[] args) { return 0; } static int Main() { return 0; } Lê Viết Mẫn - lvman@hce.edu.vn 41 Hàm Monday, September 9, 13
  42. Đệ qui Lê Viết Mẫn - lvman@hce.edu.vn 42 Hàm Monday, September 9, 13
  43. Hàm đệ qui • Đệ qui là kỹ thuật mà một hàm có thể gọi lại chính nó • Đệ qui trực tiếp : một hàm gọi lại chính nó • Đệ qui gián tiếp : A gọi B, B gọi C, C gọi A • Phải có 3 đặc tính : • Một nhánh để thực hiện lời gọi đệ quy (thường trong lệnh if) • Một nhánh để kết thúc đệ quy (thường trong lệnh if) • Đầu vào phải thay đổi với mỗi lời gọi • Về mặt lý thuyết, đệ qui có thể được viết bằng vòng lặp Lê Viết Mẫn - lvman@hce.edu.vn 43 Hàm Monday, September 9, 13
  44. Ví dụ - in số static void Forward(int number) static void Reverse(int number) { { ! if (number != 0) ! if (number != 0) !{ !{ !!Forward(number / 10); !!Console.Write(number % 10); !!Console.Write(number % 10); !!Reverse(number / 10); !} !} } } Lê Viết Mẫn - lvman@hce.edu.vn 44 Hàm Monday, September 9, 13
  45. Ví dụ - in số number == 0 number == 0 Gọi lần 4 number == 1 number == 1 Gọi lần 3 3 Forward(0) 5 Write(1) 4 Write(1) 6 Reverse(0) number == 12 number == 12 Gọi lần 2 2 Forward(1) 3 Write(2) 5 Write(2) 4 Reverse(1) number == 123 number == 123 Gọi lần 1 1 Forward(12) 1 Write(3) 6 Write(3) 2 Reverse(12) Forward Reverse Lê Viết Mẫn - lvman@hce.edu.vn 45 Hàm Monday, September 9, 13
  46. Ví dụ - tính giai thừa Viết chương trình tính n! (n giai thừa) ⎧ 1 n = 0 n! = ⎨ ⎩ 1* 2 * * n n ≥ 1 Lê Viết Mẫn - lvman@hce.edu.vn 46 Hàm Monday, September 9, 13
  47. using System; namespace Factorial { class Program { static void Main(string[] args) { int product = 1; int n; ! Console.Write("CALCULATE FACTORIAL OF A NUMBER\n\n"); ! Console.Write("Enter a number : "); n = int.Parse(Console.ReadLine()); ! for (int i = 2; i <= n; i++) // Tai thoi diem nay product = 1 * 2 * * (i-1) = (i-1)! product = product * i; ! Console.WriteLine("Factorial of {0} is {1}", n, product); Console.ReadKey(); } } } Lê Viết Mẫn - lvman@hce.edu.vn 47 Hàm Monday, September 9, 13
  48. Ví dụ - tính giai thừa Viết chương trình tính n! (n giai thừa) ⎧ 1 n = 0 n! = ⎨ ⎩ 1* 2 * * n n ≥ 1 ⎪⎧ 1 n = 0 n! = ⎨ n *(n − 1)! n ≥ 1 ⎩⎪ Lê Viết Mẫn - lvman@hce.edu.vn 48 Hàm Monday, September 9, 13
  49. static int Factorial(int n) { ! if (n == 2) !! return 2; ! else !! return n * Factorial(n-1); } Lê Viết Mẫn - lvman@hce.edu.vn 49 Hàm Monday, September 9, 13
  50. Ví dụ - USCLN Viết chương trình tìm ước số chung lớn nhất của hai số u và v Lê Viết Mẫn - lvman@hce.edu.vn 50 Hàm Monday, September 9, 13
  51. Ví dụ - USCLN Viết chương trình tìm ước số chung lớn nhất của hai số u và v ⎪⎧ u ,v = 0 USCLN(u,v) = ⎨ USCLN(v,u%v) ,v ≠ 0 ⎩⎪ Lê Viết Mẫn - lvman@hce.edu.vn 51 Hàm Monday, September 9, 13
  52. static int USCLN(int u, int v) { ! if (u == v) !! return u; ! else !! return USCLN(v, u % v); } Lê Viết Mẫn - lvman@hce.edu.vn 52 Hàm Monday, September 9, 13
  53. Cảm ơn sự chú ý Câu hỏi ? Lê Viết Mẫn - lvman@hce.edu.vn 53 Hàm Monday, September 9, 13