Kĩ thuật lập trình - Hàm

• 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 Tham số mặc định

pdf53 trang | Chia sẻ: huyhoang44 | Lượt xem: 720 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Kĩ thuật lập trình - Hàm, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Lê Viết Mẫn - lvman@hce.edu.vn Hàm v 1.0 - 10/2012 Hàm 1 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm2 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 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm3 nhưng... using System; namespace ConsoleApplication { class Program { static void Main(string[] args) { ... } } } • Khó để xử lý các bài toán lớn, phức tạp Toàn bộ code của chúng ta được viết tại đây Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm Nội dung 4 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 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm Cơ bản về Hàm Function 5 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm • 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 6 Hàm (1/3) Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm • Khi được sử dụng hay được gọi, hàm có thể được xử lý như một 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 7 Hàm (2/3) int a = 100; int b = 25; Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm Lời gọi hàm như một câu lệnh tuần tự 8 Hàm (3/3) Lời gọi hàm phải tương ứng với định 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 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn 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 • tuân thủ cách đặt tên định danh • 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 • các tham số có tên và có thể đặt tên theo quy tắc đặt tên biến • statements - thân hàm • Biến địa phương • Các câu lệnh 9 Định nghĩa hàm static type name(arguments) { statements } static int addition(int a, int b) { return a + b; } Định nghĩa hàm trong phạm vi lớp Program, ngoài phạm vi hàm Main Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm 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ừ • 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ừ • kiểu trả về là bool thì nên là bắt đầu bằng từ Is • tên bắt đầu bằng từ Get để lấy dữ liệu (từ người sử dụng) • tên bắt đầu bằng từ Set để gán dữ liệu 10 Add, IndexOf, RemoveAll PrintResults IsActivated GetDataFormFile SetName Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn 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ề • Ví dụ 11 Lời gọi hàm static int addition(int a, int b) z = addition( 5 , 3 ); double payment = calcPayment(P, R, N); double payment = calcPayment(100000, 0.08/12, 360); double pseudo_random = random( ); calcPayment(100000, 0.08/12, 360); Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm The ants go marching one by one hoorah, hoorah The ants go marching two by two hoorah, hoorah The ants go marching three by three. The little one stops to climb a tree And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching four by four hoorah, hoorah The ants go marching five by five hoorah, hoorah The ants go marching six by six. The little one stops to pick up sticks And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching seven by seven hoorah, hoorah The ants go marching eight by eight hoorah, hoorah The ants go marching nine by nine. The little one stops to check the time And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching ten by ten. The little one stops to say the ends And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. 12 Ví dụ - Ants go marching Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm The ants go marching one by one hoorah, hoorah The ants go marching two by two hoorah, hoorah The ants go marching three by three. The little one stops to climb a tree And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching four by four hoorah, hoorah The ants go marching five by five hoorah, hoorah The ants go marching six by six. The little one stops to pick up sticks And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching seven by seven hoorah, hoorah The ants go marching eight by eight hoorah, hoorah The ants go marching nine by nine. The little one stops to check the time And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching ten by ten. The little one stops to say the ends And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. 13 Ví dụ - Các đoạn Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm The ants go marching one by one hoorah, hoorah The ants go marching two by two hoorah, hoorah The ants go marching three by three. The little one stops to climb a tree And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching four by four hoorah, hoorah The ants go marching five by five hoorah, hoorah The ants go marching six by six. The little one stops to pick up sticks And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching seven by seven hoorah, hoorah The ants go marching eight by eight hoorah, hoorah The ants go marching nine by nine. The little one stops to check the time And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching ten by ten. The little one stops to say the ends And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. 14 Ví dụ - Các đoạn Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm The ants go marching one by one hoorah, hoorah The ants go marching two by two hoorah, hoorah The ants go marching three by three. The little one stops to climb a tree And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching four by four hoorah, hoorah The ants go marching five by five hoorah, hoorah The ants go marching six by six. The little one stops to pick up sticks And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching seven by seven hoorah, hoorah The ants go marching eight by eight hoorah, hoorah The ants go marching nine by nine. The little one stops to check the time And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. The ants go marching ten by ten. The little one stops to say the ends And they all go marching down to the ground to go out of the rain. Boom. Boom. Boom. Boom. Boom. Boom. Boom. Boom. 15 Ví dụ - Điệp khúc Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm • 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 16 Ví dụ - Ants go marching Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm17 // 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(); } } } Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm18 // 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(); } } } Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm • 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 19 Giá trị trả về static int addition(int a, int b) z = addition( 5 , 3 ); 8 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm20 // 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(); } } } Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm21 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 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm22 // 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; ! int! nickels = money / 5; ! money %= 5; ! int! pennies = money; 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(); } } } Module hóa chương trình này ? • Tách các bước tính số dollar, số quarter, số dimes và số nikel • Các hàm này sẽ nhận một số money và trả về kết quả tính được Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm23 // 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(int money) { return money / 100; } static int CalcQuarters(int money) { return money / 25; } static int CalcDimes(int money) { return money / 10; } static int CalcNickels(int money) { return money / 5; } 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 = CalcDollars(money); money = money % 100; int quarters = CalcQuarters(money); money = money % 25; int dimes = CalcDimes(money); money %= 10; int nickels = CalcNickels(money); money %= 5; int pennies = money; 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(); } } } Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm Truyền tham số 24 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm Phạm vi của biến • .NET cấp phát cho mỗi hàm một vùng nhớ có kích thước cố định 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 hàm đó • Khi hết phạm vi của hàm thì biến đó bị huỷ • Giá trị của biến của hàm này thì hàm khác không thể sử dụng hoặc thay đổi dữ liệu, trừ khi... 25 stack code static data heap main() int y myobj = new Object() square(y) square(y) s = y * y Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm Kiểu trị & kiểu tham chiếu 26 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 Ví dụ int i = 17;int j = i; string s = “Hello”; string s1 = s; i 17 j 17 s s1 0x0a10 0x0a10 Hello stack heapstack Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm27 Tham trị static void Main() { ! int a = 5; ! Func(a); } static void Func(int i) { ! i = i + 1; } 5 0xff10 a 5 0xef00 i 5 5 0xff10 6 0xef00 i 5 0xff10 a th ời g ia n a Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm28 Tham chiếu - ref static int Main() { ! int a = 5; ! Func(ref a); } static void Func(ref int i) { ! i = i + 1; } a 5 0xff10 a 0xff10 0xef00 i 0xff10 6 0xff10 0xef00 i 6 0xff10 th ời g ia n 0xff10a Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm29 Ví dụ static void Swap (ref int v1, ref int v2) { ! int temp = v1; ! v1 = v2; ! v2 = temp; } static int Main () { ! int a = 10, b = 20; ! ! Swap(ref a, ref b); } Hoán đổi giá trị hai biến a 10 th ời g ia n b 20 a 10 b 20 temp 10 a 20 b 20 temp 10 a 20 b 10 temp 10 a 20 b 10 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm30 // 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 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm31 // 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(); } } } Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn // 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(int money) { return money / 100; } static int CalcQuarters(int money) { return money / 25; } static int CalcDimes(int money) { return money / 10; } static int CalcNickels(int money) { return money / 5; } 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 = CalcDollars(money); money = money % 100; int quarters = CalcQuarters(money); money = money % 25; int dimes = CalcDimes(money); money %= 10; int nickels = CalcNickels(money); money %= 5; int pennies = money; 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(); } } } Hàm32 Yêu cầu : Sử dụng tham chiếu Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn // 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; money = money % 100; return dollars; } static int calcQuarters(ref int money) { int quarters = money / 25; money = money % 25; return quarters; } static int calcDimes(ref int money) { int dimes = money / 10; money = money % 10; return dimes; } static int calcNickels(ref int money) { int nickels = money / 5; money = money % 5; return nickels; } 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 = calcDollars(ref money); int quarters = calcQuarters(ref money); int dimes = calcDimes(ref money); int nickels = calcNickels(ref money); int pennies = money; 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(); } } } Hàm33 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm34 Tham chiếu - out static int Main() { ! int a; ! Func(out a); } static void Func(out int i) { ! i = 10; } a a a un 0xff10 0xff10 0xef00 i 0xff10 10 0xff10 0xef00 i 10 0xff10 th ời g ia n 0xff10 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn // 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; money = money % 100; return dollars; } static int calcQuarters(ref int money) { int quarters = money / 25; money = money % 25; return quarters; } static int calcDimes(ref int money) { int dimes = money / 10; money = money % 10; return dimes; } static int calcNickels(ref int money) { int nickels = money / 5; money = money % 5; return nickels; } 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 = calcDollars(ref money); int quarters = calcQuarters(ref money); int dimes = calcDimes(ref money); int nickels = calcNickels(ref money); int pennies = money; 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(); } } } Hàm35 Yêu cầu : Sử dụng tham chiếu out Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn // 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: "); money = int.Parse(Console.ReadLine()); } static int CalcDollars(ref int money) { int dollars = money / 100; money = money % 100; return dollars; } static int CalcQuarters(ref int money) { int quarters = money / 25; money = money % 25; return quarters; } static int CalcDimes(ref int money) { int dimes = money / 10; money = money % 10; return dimes; } static int CalcNickels(ref int money) { int nickels = money / 5; money = money % 5; return nickels; } static void Main(string[] args) { int money; GetMoney(out money); Console.WriteLine("{0} pennies =", money); int dollars = CalcDollars(ref money); int quarters = CalcQuarters(ref money); int dimes = CalcDimes(ref money); int nickels = CalcNickels(ref money); int pennies = money; 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(); } } } Hàm36 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm Các đặc tính khác của hàm trong C# 37 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm • 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ụ • Không thể vi phạm các luật về tham số hàm 38 Tham số mặc định 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 static void F(int i, int j = 0) static void F(int i)!! ! ! // conflicts with default above Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn 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ố • Khác nhau về kiểu của các tham số • Khác nhau về kiểu của các tham số tại vị trí tương ứng • 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ề 39 Chồng hàm F(void), F(int i), F(int i, int j) F(int i), F(string s), F(double d) G(int i, string s), G(string s, int i) Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm Hàm Main 40 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm Các dạng hàm Main 41 static void Main(string[] args) { } static int Main(string[] args) { return 0; } static int Main() { return 0; } static void Main() { } Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm Đệ qui 42 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm • Đệ 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 43 Hàm đệ qui Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm44 Ví dụ - in số static void Forward(int number) { ! if (number != 0) ! { ! ! Forward(number / 10); ! ! Console.Write(number % 10); ! } } static void Reverse(int number) { ! if (number != 0) ! { ! ! Console.Write(number % 10); ! ! Reverse(number / 10); ! } } Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm45 Ví dụ - in số number == 123 1 Forward(12) 6 Write(3) number == 12 2 Forward(1) 5 Write(2) number == 1 3 Forward(0) 4 Write(1) number == 0 number == 123 1 Write(3) 2 Reverse(12) number == 12 3 Write(2) 4 Reverse(1) number == 1 5 Write(1) 6 Reverse(0) number == 0 Forward Reverse Gọi lần 1 Gọi lần 2 Gọi lần 3 Gọi lần 4 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm46 Ví dụ - tính giai thừa Viết chương trình tính n! (n giai thừa) n!= 1 n = 01*2 *..*n n ≥ 1 ⎧ ⎨ ⎩ Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm47 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(); } } } Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm48 Ví dụ - tính giai thừa Viết chương trình tính n! (n giai thừa) n!= 1 n = 01*2 *..*n n ≥ 1 ⎧ ⎨ ⎩ n!= 1 n = 0n * (n −1)! n ≥ 1 ⎧ ⎨ ⎪ ⎩⎪ Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm49 static int Factorial(int n) { ! if (n == 2) ! ! return 2; ! else ! ! return n * Factorial(n-1); } Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm50 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 Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm51 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 USCLN(u,v) = u ,v = 0USCLN(v,u%v) ,v ≠ 0 ⎧ ⎨ ⎪ ⎩⎪ Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Hàm52 static int USCLN(int u, int v) { ! if (u == v) ! ! return u; ! else ! ! return USCLN(v, u % v); } Monday, September 9, 13 Lê Viết Mẫn - lvman@hce.edu.vn Cảm ơn sự chú ý Câu hỏi ? Hàm53 Monday, September 9, 13

Các file đính kèm theo tài liệu này:

  • pdf06_ham_3209.pdf