Bài giảng Cơ sở lập trình - Chương 6 cung cấp các kiến thức cơ bản về hàm và đệ quy. Nội dung cụ thể được trình bày trong chương này gồm có: Cơ bản về hàm, truyền tham số, các đặc tính mới về hàm trong C#, các dạng hàm Main, đệ quy. Mời các bạn cùng tham khảo.
Trang 1v 1.0 - 10/2012
Hàm
Trang 2chúng ta đã học
Câu lệnh if, switch Câu lệnh for, while, do
Trang 3using System;
namespace ConsoleApplication {
class Program {
Trang 4Nộ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
Trang 5Cơ bản về Hàm
Function
Trang 6• Kỹ thuật cho phép chia nhỏ một chương trình lớn ra nhiều
• 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
Hàm (1/3)
Trang 7• 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ó
Trang 8Việ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
Trang 9• type - kiểu của giá trị trả về
• không trả về dữ liệu thì sử dụng void
• 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
Trang 10Cách đặt tên hàm
• Viết hoa ký tự đầu tiên của mỗi từ
• 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
Add, IndexOf, RemoveAll
Print Results
Is Activated Get DataFormFile
Set Name
Trang 11• 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
calcPayment(100000, 0.08/12, 360);
Trang 12The 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
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.
Ví dụ - Ants go marching
Trang 13The 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
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.
Ví dụ - Các đoạn
Trang 14The 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
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.
Ví dụ - Các đoạn
Trang 15The 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
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.
Ví dụ - Điệp khúc
Trang 16• 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ơ
Trang 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();
} }
Trang 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();
} }
}
Trang 19• 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
• 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)
z = addition( 5 , 3 );
8
Trang 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);
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();
} }}
Trang 21Ví dụ - tiền tệ của Mỹ
Trang 22// a program that converts a number of pennies into the equivalant
// value expressed in dollars, quarters, dimes, nickels, and pennies.
Console Write( "Enter the number of pennies: " );
! money = int Parse( Console ReadLine());
Console WriteLine( "{0} pennies =" , money);
! int ! dollars = money / 100;
! 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);
Trang 23// a program that converts a number of pennies into the
Console.WriteLine("{0} pennies =", money);
int dollars = CalcDollars(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();
} }}
Trang 24Truyền tham số
Trang 25Phạm vi của biến
vùng nhớ có kích thước cố định
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
stack
code static data
heap
main() int y
myobj = new Object()
square(y) square(y)
s = y * y
Trang 26Kiểu trị & kiểu tham chiếu
i 17
j 17
s s1 0x0a10
0x0a10
Hello
stack
Trang 28Tham chiếu - ref
static int Main()
Trang 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
Trang 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();
} }
Trang 32// a program that converts a number of pennies into the
Console.WriteLine("{0} pennies =", money);
int dollars = CalcDollars(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();
} }}
Yêu cầu : Sử dụng tham chiếu