Você está na página 1de 28

Chng 4

LP TRNH N TH
Ni dung

Khi nim v c php


Tm vc
Tham s v li gi hm
Hm quy

2
t vn

Vit chng trnh tnh S = a! + b! + c! vi a,


b, c l 3 s nguyn dng nhp t bn phm.
Chng trnh
chnh

Nhp Tnh Xut


a, b, c > 0 S = a! + b! + c! kt qu S

Nhp Nhp Nhp Tnh Tnh Tnh


a>0 b>0 c>0 s1=a! s2=b! s3=c!

3
t vn

3 on lnh nhp a, b, c > 0


do {
Console.Write(Nhap a(s nguyen duong):);
a = int.parse(Console.ReadLine());
} while (a <= 0);
do {
Console.Write(Nhap b(s nguyen duong):);
b = int.parse(Console.ReadLine());} while
(b <= 0);
do {
Console.Write(Nhap a(s nguyen duong):);
c = int.parse(Console.ReadLine());
} while (c <= 0);

4
t vn

3 on lnh tnh s1 = a!, s2 = b!, s3 = c!


{ Tnh s1 = a! = 1 * 2 * * a }
s1 = 1;
for (i = 2; i <= a ; i++)
s1 = s1 * i;
{ Tnh s2 = b! = 1 * 2 * * b }
s2 = 1;
for (i = 2; i <= b ; i++)
s2 = s2 * i;
{ Tnh s3 = c! = 1 * 2 * * c }
s3 = 1;
for (i = 2; i <= c ; i++)
s3 = s3 * i;
5
t vn

Gii php => Vit 1 ln v s dng nhiu ln


on lnh nhp tng qut, vi n = a, b, c
do {
Console.Write(Nhap s Nguyen duong):);
n = int.parse(Console.ReadLine());
} while (n <= 0);

on lnh tnh giai tha tng qut, n = a, b, c


{ Tnh s = n! = 1 * 2 * * n }
s = 1;
for (i = 2; i <= n ; i++)
s = s * i;

6
Hm

Khi nim
Mt on chng trnh c tn, u vo v u ra.
C chc nng gii quyt mt s vn chuyn
bit cho chng trnh chnh.
c gi nhiu ln vi cc tham s khc nhau.
c s dng khi c nhu cu:
Ti s dng.
Sa li v ci tin.

7
Hm

C php
[static]<kiu tr v> <tn hm>([<danh sch tham s>])
{
<cc cu lnh>
[return <gi tr>;]
}
Trong
<kiu tr v> : kiu bt k ca C# (char, int,
long, float,). Nu khng tr v th l void.
<tn hm>: theo quy tc t tn nh danh.
<danh sch tham s> : tham s hnh thc u
vo ging khai bo bin, cch nhau bng du ,
<gi tr> : tr v cho hm qua lnh return.
8
Cc bc vit hm

Cn xc nh cc thng tin sau y:


Tn hm.
Hm s thc hin cng vic g.
Cc u vo (nu c).
u ra (nu c).

u vo 1 Tn hm
u vo 2 u ra (nu c)
Cc cng vic
u vo n
s thc hin

9
Hm

V d 1
Tn hm: XuatTong
Cng vic: tnh v xut tng 2 s nguyn
u vo: hai s nguyn x v y
u ra: khng c
static void XuatTong(int x, int y){
int s;
s = x + y;
Console.WriteLine( {} + {1} = {2},
x, y, s);
}

10
Hm

V d 2
Tn hm: TinhTong
Cng vic: tnh v tr v tng 2 s nguyn
u vo: hai s nguyn x v y
u ra: mt s nguyn c gi tr x + y
static int TinhTong(int x, int y)
{
int s;
s = x + y;
return s;
}

11
Chng trnh con - Function
V d 3
Tn hm: NhapXuatTong
Cng vic: nhp v xut tng 2 s nguyn
u vo: khng c
u ra: khng c
static void NhapXuatTong(){
int x, y;
Console.Write(Nhap s Nguyen x:);
x = int.parse(Console.ReadLine());
Console.Write(Nhap s Nguyen y:);
y = int.parse(Console.ReadLine());
Console.WriteLine( {} + {1} = {2},
x, y, s);
} 12
Tm vc

Khi nim
L phm vi hiu qu ca bin v hm.
Bin:
Ton cc: khai bo trong ngoi tt c cc hm
(k c hm main) v c tc dng ln ton b
chng trnh.
Cc b: khai bo trong hm hoc khi { } v ch
c tc dng trong bn thn hm hoc khi
(k c khi con n). Bin cc b s b xa khi
b nh khi kt thc khi khai bo n.

13
Tm vc

int a;
int ham1(){
int a1;
}

int ham2(){
int a2;
{
int a21;
}
}

void main(){
int a3;
}
14
Cc cch truyn i s

Truyn Gi tr (Call by Value)


Truyn i s cho hm dng gi tr.
C th truyn hng, bin, biu thc nhng hm
ch s nhn gi tr.
c s dng khi khng c nhu cu thay i gi
tr ca tham s sau khi thc hin hm.
static void TruyenGiaTri(int x)
{

x++;
}
16
Cc cch truyn i s

Truyn tham chiu (Call by Reference)


Truyn i s cho hm dng a ch.
Khng c truyn gi tr cho tham s ny.
c s dng khi c nhu cu thay i gi tr
ca tham s sau khi thc hin hm.

static void TruyenThamChieu([ref|out] int x)


{

x++;
}
17
Lu khi truyn i s
Lu
Trong mt hm, cc tham s c th truyn
theo nhiu cch.
Tham s khai bo tham chiu ref phi khi to
gi tr trc khi s dng hm.
void HonHop(int x, ref int y){

x++;
y++;
}

19
Lu khi truyn i s
Lu
S dng tham chiu l mt cch tr v gi
tr cho chng trnh.
int TinhTong(int x, int y){
return x + y;
}
void TinhTong(int x, int y, out int tong){
tong = x + y;
}
void TinhTongHieu(int x, int y,
out int tong, out int hieu){
tong = x + y; hieu = x y;
}
20
Li gi hm

Cch thc hin


Gi tn ca hm ng thi truyn cc i s
(hng, bin, biu thc) cho cc tham s theo
ng th t c khai bo trong hm.
Cc bin hoc tr ny cch nhau bng du ,
Cc i s ny c c t trong cp du
ngoc n ( )

<tn hm> ([ref]<i s 1>, , [ref]<i s n>);

21
Li gi hm

V d
{ Cc hm c khai bo y }
static void Main()
{
int n = 9;
XuatTong(1, 2);
XuatTong(1, n);
TinhTong(1, 2);
int tong = TinhTong(1, 2);
TruyenGiaTri(1);
TruyenGiaTri(n);
TruyenThamChieu(1);
TruyenThamChieu(ref n);
}

22
Li gi chng trnh con

V d
static void Main()
{
HoanVi(2912, 1706);
int x = 2912, y = 1706;
HoanVi(ref x, ref y);
}
static void HoanVi(ref int a, ref int b)
{
int tam = a;
a = b;
b = tam;
}

23
quy

Khi nim
Mt chng trnh con c th gi mt chng
trnh con khc.
Nu gi chnh n th c gi l s quy.
S ln gi ny phi c gii hn (im dng)
V d
Tnh S(n) = n! = 1*2**(n-1)*n
Ta thy S(n) = S(n-1)*n
Vy thay v tnh S(n) ta s i tnh S(n-1)
Tng t tnh S(n-2), , S(2), S(1), S(0) = 1

24
quy

V d
static int GiaiThua(int n)
{
if (n == 0)
return 1;
else
return GiaiThua(n 1) * n;
}
static int GiaiThua(int n)
{
if (n > 0)
return GiaiThua(n 1) * n;
else
return 1;
} 25
Mt s lu

Mi hm ch nn thc hin mt nhim v


(task) n.
Tn ca hm nn m t nhim v phi lm.
K t u ca cc t trong tn hm nn vit
hoa: SendMail(..)
Cch t tn hm: <verb>[+adjective]+noun
(vd: FindSmallestElement())
S dng lnh trong mt hm khng nn vt
qu 1 trang mn hnh (computer screen).

26
Bi tp

1. Bi tp chng cu lnh iu kin v r nhnh


a. Vit hm i mt k t hoa sang k t thng.
b. Vit th tc gii phng trnh bc nht.
c. Vit th tc gii phng trnh bc hai.
d. Vit hm tr v gi tr nh nht ca 4 s nguyn.
e. Vit th tc hon v hai s nguyn.
f. Vit th tc sp xp 4 s nguyn tng dn.

27
Bi tp

2. Bi tp chng cu lnh lp. Hm nhn vo mt


s nguyn dng n v thc hin:
a. Tr v s o ca s .
b. C phi l s i xng (Tr v True/False)
c. C phi l s chnh phng.
d. C phi l s nguyn t.
e. Tng cc ch s l.
f. Tng cc ch s nguyn t.
g. Tng cc ch s chnh phng.

28
Bi tp

3. Bi tp chng Cu lnh lp. Hm nhn vo mt


s nguyn dng n v thc hin:
a. S=1+2++n
b. S = 12 + 22 + + n2
c. S = 1 + 1/2 + + 1/n
d. S=1*2**n
e. S = 1! + 2! + + n!
4. Hm tr v USCLN ca 2 s nguyn.
5. In ra n phn t ca dy Fibonacy.

29
Tm tt chng 5

30