SlideShare a Scribd company logo
Ngöôøi thöïc hieän: TRAÀN VAÊN SÓ
Toå: Toaùn – Tin.
SÔÛ GIAÙO DUÏC VAØ ÑAØO TAÏO HAØ
GIANG
Tröôøng THPT Ñoàng Yeân
Chöông 6: CHÖÔNG TRÌNH CON VAØ
LAÄP TRÌNH COÙ CAÁU
TRUÙC
Chöông trình con laø gì ?
Phaân loaïi chöông
trình con ?
Ñieåm khaùc nhau cô baûn giöõa
haøm vaø thuû tuïc ?
• 1. Caùch vieát vaø söû duïng
thuû tuïc
• a. Caáu truùc cuûa thuû tuïc
• b. Ví duï veà thuû tuïc
• 2. Caùch vieát vaø söû duïng
haøm
• Xeùt ví duï veõ hình chöõ nhaät coù daïng
sau:
• * * * * * * *
• * *
• * * * * * * *
Caùch vieát vaø söû duïng thuû tuïc
Ñeå veõ hình chöõ nhaät treân, ta caàn
duøng nhöõng caâu leänh naøo?
writeln(‘* * * * * * *’);
writeln(‘* *’);
writeln(‘* * * * * * *’);
procedure Ve_Hcn;
• begin
writeln(‘* * * * * * *’);
writeln(‘* *’);
writeln(‘* * * * * * *’);
• end;
Caùch vieát vaø söû duïng thuû tuïc
{baét ñaàu thuû
tuïc}
{keát thuùc thuû
tuïc}
procedure <teân thuû tuïc> [(<danh saùch tham
soá>)];
• [<phaàn khai baùo>]
begin
[<daõy caùc leänh>]
end;
Caùch vieát vaø söû duïng thuû tuïc
•a. Caáu truùc cuûa thuû tuïc:
procedure
;
Chuù yù:
 Khaùc vôùi chöông trình chính keát thuùc
laø daáu chaám (.) sau töø khoaù End
coøn sau end keát thuùc thuû tuïc laø
daáu chaám phaåy (;).
 Khi thöïc hieän, ta phaûi vieát leänh goïi
thuû tuïc töông töï nhö caùc thuû tuïc
chuaån. Trong Pascal leänh goïi thöïc
hieän thuû tuïc nhö sau:
<teân thuû tuïc>[(caùc tham soá
thöïc söï)];
program VD_thutuc1;
procedure Ve_Hcn;
begin
writeln('* * * * * * *');
writeln('* *');
writeln('* * * * * * *');
end;
begin
Ve_Hcn;
writeln;writeln;
Ve_Hcn;
writeln;writeln;
Ve_Hcn;
readln
end.
Caùch vieát vaø söû duïng thuû tuïc
Thuû tuïc Ve_Hcn
(chöông trình con Ve_Hcn)
Leänh goïi chöông trình con
program VD_thutuc1;
procedure Ve_Hcn;
begin
writeln('* * * * * * *');
writeln('* *');
writeln('* * * * * * *');
end;
Begin
Ve_Hcn;
writeln;writeln;
Ve_Hcn;
writeln;writeln;
Ve_Hcn;
readln
end.
Giaû söû ta caàn veõ nhieàu hình
chöõ nhaät vôùi kích thöôùc khaùc
nhau (25x10; 5x10; 4x2), thuû tuïc
naøy coù thöïc hieän ñöôïc
khoâng ?
Ñeå giaûi quyeát baøi toaùn treân,
thuû tuïc Ve_Hcn ñöôïc vieát laïi nhö
theá naøo ?
Caùch vieát vaø söû duïng thuû tuïc
• Ví duï 1: Chöông trình veõ caùc hình chöõ
nhaät coù kích thöôùc khaùc nhau baèng
caùch duøng thuû tuïc Ve_Hcn vôùi caùc
tham soá chdai, chrong. Giaù trò cuûa
caùc tham soá chdai, chrong laø
nguyeân (integer).
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
Khi ñoù phaàn ñaàu cuûa thuû tuïc ñöôïc
vieát nhö sau:
procedure Ve_hcn(chdai, chrong: integer);
Khi ñoù phaàn ñaàu cuûa thuû
tuïc Ve_Hcn ñöôïc vieát nhö
theá naøo?
1. procedure Ve_Hcn(chdai, chrong: integer);
2. var i, j: integer;
3. begin
4. for i:=1 to chdai do write('*');
5. writeln;
6. for j:=1 to chrong-2 do
begin
write('*');
for i:=1 to chdai-2 do write(' ');
writeln('*');
end;
7. for i:=1 to chdai do write('*');
8. writeln;
9. end;
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
Tham soá hình thöùc
Veõ caïnh treân cuûa
hình chöõ nhaät
Veõ hai caïnh beân
Veõ caïnh döôùi
begin
clrscr;
Ve_Hcn(25,10);
writeln;writeln;
Ve_Hcn(5,10);
readln;
clrscr;
a:=4; b:=2;
for i:=1 to 4 do
begin
Ve_Hcn(a,b);
readln; clrscr;
a:=a*2; b:=b*2;
end;
readln;
end.
* Trong leänh goïi thuû tuïc, caùc
tham soá hình thöùc ñöôïc thay
baèng caùc tham soá thöïc söï
töông öùng laø caùc giaù trò cuï
theå ñöôïc goïi laø tham soá
giaù trò (tham trò).
•Caâu leänh goïi thuû tuïc coù
daïng:
•<teân thuû tuïc>[(caùc tham soá
thöïc söï)];
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
• Ví duï 2: Chöông trình hoaùn ñoåi giaù trò
cuûa hai bieán, baèng caùch duøng thuû
tuïc Hoan_doi.
x y
TG
TG
TG
x y
TG
x y
TG := x (1)
x := y (2) y := TG (3)
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
1. program VD_thambien1;
2. uses crt;
3. var a, b: integer;
4. procedure Hoan_doi(var x,y: integer);
5. var TG: integer;
6. begin
TG:= x;
x:= y;
y:= TG;
end;
7. begin
8. clrscr;
9. a:=5; b:=10;
10. writeln(a:6, b:6);
11. Hoan_doi(a,b);
12. writeln(a:6, b:6);
13. readln;
14. end.
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
Tham soá hình thöùc laø caùc
tham soá bieán
Leänh goïi thuû tuïc Hoan_doi
vôùi caùc tham soá ñöôïc
thay baèng caùc bieán
Thay ñoåi giaù trò cuûa caùc bieán
program VD_thambien1;
uses crt;
var a, b: integer;
procedure Hoan_doi(var x,y: integer);
var TG: integer;
begin
TG:= x;
x:= y;
y:= TG;
end;
begin
clrscr;
a:=5; b:=10;
writeln(a:6, b:6);
Hoan_doi(a,b);
writeln(a:6, b:6);
readln;
end.
•Trong leänh goïi thuû
tuïc, caùc tham soá hình
thöùc ñöôïc thay baèng
caùc tham soá thöïc söï
töông öùng laø teân caùc
bieán chöùa döõ lieäu ra
ñöôïc goïi laø caùc
tham soá bieán (tham
bieán).
•Ñeå phaân bieät tham
bieán vaø tham trò,
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
program VD_thambien2;
uses crt;
var a, b: integer;
procedure Hoan_doi(x: integer; var y: integer);
var TG: integer;
begin
TG:= x;
x:= y;
y:= TG;
end;
begin
clrscr;
a:=5; b:=10;
writeln(a:6, b:6);
Hoan_doi(a,b);
writeln(a:6, b:6);
readln;
end.
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
Khaùc nhau trong khai baùo
tham soá hình thöùc
Khaùc nhau ôû keát quaû sau khi
thöïc hieän thuû tuïc
Ñieåm khaùc nhau giöõa
tham trò vaø tham bieán ?
•* Tham soá bieán ñöôïc duøng ñeå ghi laïi
•döõ lieäu keát quaû cuûa vieäc thöïc hieän
•thuû tuïc.
•* Tham soá giaù trò chæ ñeå ñöa döõ lieäu
•vaøo cho thuû tuïc khi baét ñaàu thöïc hieän
•thuû tuïc.
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
1. program VD_thambien2;
2. uses crt;
3. var a, b: integer;
4. procedure Hoan_doi(x: integer; var y: integer);
5. var TG: integer;
6. begin
TG:= x;
x:= y;
y:= TG;
end;
7. begin
8. clrscr;
9. a:=5; b:=10;
10. writeln(a:6, b:6);
11. Hoan_doi(a,b);
12. writeln(a:6, b:6);
13. readln;
14.end.
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
procedure Hoan_doi(var x: integer; y: integer);
Keát quaû nhaän ñöôïc (treân
maøn hình) nhö theá naøo khi
thöïc hieän chöông trình ?
* Chuù yù:
• - Khi khai baùo caùc tham soá hình thöùc,
neáu coù kieåu khaùc nhau thì caùc tham soá
seõ caùch nhau baèng daáu chaám phaåy,
neáu coù cuøng kieåu thì caùch nhau baèng
daáu phaåy.
• Ví duï:
• procedure CTC(x, y, z: integer; r: real);
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
* Chuù yù:
• - Khi khai baùo döõ lieäu cho caùc tham soá
hình thöùc chæ ñöôïc duøng teân kieåu. Teân
kieåu laø teân chuaån hoaëc teân do ngöôøi
laäp trình ñaët.
• Ví duï:
Khai baùo hôïp leä:
procedure Alpha(N: integer; var s: string);
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
-Khai baùo khoâng hôïp leä:
procedure Delta(M: array[1..10] of byte; var X:
string[25]);
-Khai baùo hôïp leä:
type mang = array[1..10] of byte; xau =
string[25];
procedure Delta(M: mang; var X: xau);
•b. Ví duï veà thuû tuïc
Caùch vieát vaø söû duïng thuû tuïc
CUÛNG COÁ VAØ LUYEÄN TAÄPCUÛNG COÁ VAØ LUYEÄN TAÄP
 Caùch vieát moät thuû tuïc trong ngoân ngöõ laäp
trình Pascal. Hieåu moái lieân quan giöõa chöông
trình chính vaø thuû tuïc;
 Leänh goïi thöïc hieän thuû tuïc trong chöông trình
chính;
 Tham soá bieán vaø tham soá giaù trò;
CUÛNG COÁCUÛNG COÁ
1. Töø khoùa ñeå khai baùo thuû tuïc laø:
A. Program B. Procedure
C. Function D. Processor
2. Muoán khai baùo x, y laø tham soá giaù trò (x, y
thuoäc kieåu integer) trong thuû tuïc coù teân
laø “ABC” thì khai baùo naøo sau ñaây laø
ñuùng?
A. Procedure ABC(x, y: integer);
B. Procedure ABC(var x, y: integer);
C. Procedure ABC(x: integer; var y: integer);
D. Procedure ABC(var x: integer; y: integer);
CUÛNG COÁ
3. Muoán khai baùo x laø tham soá giaù trò, y vaø
z laø tham soá bieán (x, y, z thuoäc kieåu
integer) trong thuû tuïc coù teân laø “CTC” thì
khai baùo naøo sau ñaây laø ñuùng?
A. Procedure CTC(x, y, z: integer);
B. Procedure CTC(var x, y, z: integer);
C. Procedure CTC(var x:integer; y, z:
integer);
D. Procedure CTC(x: integer; var y, z:
integer);
CUÛNG COÁ
4. Cho chöông trình sau:
var a, b, c: integer;
procedure Hoan_doi(var a, b: integer);
var TG: integer;
begin
TG:= a;
a:= b;
b:= TG;
end;
begin
clrscr;
a:= 10; b:= 11; c:= 12;
Hoan_doi(b,c);
Hoan_doi(a,b);
Hoan_doi(b,c);
writeln(a, ‘ ’, b ‘ ’, c);
end.
Keát quaû
nhaän ñöôïc
(treân maøn
hình) sau khi
thöïc hieän
chöông trình laø:
A. 10 11 12
B. 11 10 12
C. 12 11 10
D. 12 10 11
CUÛNG COÁCUÛNG COÁ
DAËN DOØ VAØ HÖÔÙNG DAÃN TÖÏDAËN DOØ VAØ HÖÔÙNG DAÃN TÖÏ
HOÏCHOÏC
 Veà nhaø hoïc laïi toaøn boä kieán thöùc baøi
hoïc.
 Ñoïc tröôùc muïc 2. Caùch vieát vaø söû duïng
haøm
 Laøm vaø traû lôøi caùc caâu hoûi vaø baøi
taäp trong saùch giaùo khoa vaø laøm theâm
caùc baøi taäp 6.1, 6.1, 6.3 tr. 55; Baøi 6.13,
6.14, 6.17, 6.18 trang 58,60.
Tieát hoïc ñeán ñaây laø heát.
• Xin chuùc quyù Thaày
(Coâ) cuøng caùc em
hoïc sinh
• Vui - Khoeû vaø
Thaønh coâng.
• Xin caùm ôn!

More Related Content

Recently uploaded

guia informatica Guanajuato modulo 22 nuples
guia informatica Guanajuato modulo 22 nuplesguia informatica Guanajuato modulo 22 nuples
guia informatica Guanajuato modulo 22 nuples
Examenes Preparatoria Abierta
 
أفضل 11 موقع لعمل اختبارات إلكترونية (Slide Decks).pdf
أفضل 11 موقع لعمل اختبارات إلكترونية (Slide Decks).pdfأفضل 11 موقع لعمل اختبارات إلكترونية (Slide Decks).pdf
أفضل 11 موقع لعمل اختبارات إلكترونية (Slide Decks).pdf
qorrectdm
 
DAFTAR LAMPIRAN CIP FORTUNA [Autosaved].pptx
DAFTAR LAMPIRAN CIP FORTUNA [Autosaved].pptxDAFTAR LAMPIRAN CIP FORTUNA [Autosaved].pptx
DAFTAR LAMPIRAN CIP FORTUNA [Autosaved].pptx
divadewantara1
 
الهندسيه الميكانيكيه الشامله hvac 1
الهندسيه  الميكانيكيه  الشامله  hvac   1الهندسيه  الميكانيكيه  الشامله  hvac   1
الهندسيه الميكانيكيه الشامله hvac 1
maymohamed29
 
Ingresantes en el examen general unap 2023 I.pdf
Ingresantes en el examen general unap 2023 I.pdfIngresantes en el examen general unap 2023 I.pdf
Ingresantes en el examen general unap 2023 I.pdf
wilfacemeet
 
النزاع-ا لعرقي-والقبلي-في-دولة-جنوب-السودان-وانعكاساته-على-بناء-الدولة-4.pdf
النزاع-ا   لعرقي-والقبلي-في-دولة-جنوب-السودان-وانعكاساته-على-بناء-الدولة-4.pdfالنزاع-ا   لعرقي-والقبلي-في-دولة-جنوب-السودان-وانعكاساته-على-بناء-الدولة-4.pdf
النزاع-ا لعرقي-والقبلي-في-دولة-جنوب-السودان-وانعكاساته-على-بناء-الدولة-4.pdf
Gamal Mansour
 

Recently uploaded (6)

guia informatica Guanajuato modulo 22 nuples
guia informatica Guanajuato modulo 22 nuplesguia informatica Guanajuato modulo 22 nuples
guia informatica Guanajuato modulo 22 nuples
 
أفضل 11 موقع لعمل اختبارات إلكترونية (Slide Decks).pdf
أفضل 11 موقع لعمل اختبارات إلكترونية (Slide Decks).pdfأفضل 11 موقع لعمل اختبارات إلكترونية (Slide Decks).pdf
أفضل 11 موقع لعمل اختبارات إلكترونية (Slide Decks).pdf
 
DAFTAR LAMPIRAN CIP FORTUNA [Autosaved].pptx
DAFTAR LAMPIRAN CIP FORTUNA [Autosaved].pptxDAFTAR LAMPIRAN CIP FORTUNA [Autosaved].pptx
DAFTAR LAMPIRAN CIP FORTUNA [Autosaved].pptx
 
الهندسيه الميكانيكيه الشامله hvac 1
الهندسيه  الميكانيكيه  الشامله  hvac   1الهندسيه  الميكانيكيه  الشامله  hvac   1
الهندسيه الميكانيكيه الشامله hvac 1
 
Ingresantes en el examen general unap 2023 I.pdf
Ingresantes en el examen general unap 2023 I.pdfIngresantes en el examen general unap 2023 I.pdf
Ingresantes en el examen general unap 2023 I.pdf
 
النزاع-ا لعرقي-والقبلي-في-دولة-جنوب-السودان-وانعكاساته-على-بناء-الدولة-4.pdf
النزاع-ا   لعرقي-والقبلي-في-دولة-جنوب-السودان-وانعكاساته-على-بناء-الدولة-4.pdfالنزاع-ا   لعرقي-والقبلي-في-دولة-جنوب-السودان-وانعكاساته-على-بناء-الدولة-4.pdf
النزاع-ا لعرقي-والقبلي-في-دولة-جنوب-السودان-وانعكاساته-على-بناء-الدولة-4.pdf
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Bai 18 vi du ve cach viet va su dung ctc

  • 1. Ngöôøi thöïc hieän: TRAÀN VAÊN SÓ Toå: Toaùn – Tin. SÔÛ GIAÙO DUÏC VAØ ÑAØO TAÏO HAØ GIANG Tröôøng THPT Ñoàng Yeân
  • 2.
  • 3. Chöông 6: CHÖÔNG TRÌNH CON VAØ LAÄP TRÌNH COÙ CAÁU TRUÙC Chöông trình con laø gì ? Phaân loaïi chöông trình con ? Ñieåm khaùc nhau cô baûn giöõa haøm vaø thuû tuïc ?
  • 4. • 1. Caùch vieát vaø söû duïng thuû tuïc • a. Caáu truùc cuûa thuû tuïc • b. Ví duï veà thuû tuïc • 2. Caùch vieát vaø söû duïng haøm
  • 5. • Xeùt ví duï veõ hình chöõ nhaät coù daïng sau: • * * * * * * * • * * • * * * * * * * Caùch vieát vaø söû duïng thuû tuïc Ñeå veõ hình chöõ nhaät treân, ta caàn duøng nhöõng caâu leänh naøo? writeln(‘* * * * * * *’); writeln(‘* *’); writeln(‘* * * * * * *’);
  • 6. procedure Ve_Hcn; • begin writeln(‘* * * * * * *’); writeln(‘* *’); writeln(‘* * * * * * *’); • end; Caùch vieát vaø söû duïng thuû tuïc {baét ñaàu thuû tuïc} {keát thuùc thuû tuïc}
  • 7. procedure <teân thuû tuïc> [(<danh saùch tham soá>)]; • [<phaàn khai baùo>] begin [<daõy caùc leänh>] end; Caùch vieát vaø söû duïng thuû tuïc •a. Caáu truùc cuûa thuû tuïc: procedure ;
  • 8. Chuù yù:  Khaùc vôùi chöông trình chính keát thuùc laø daáu chaám (.) sau töø khoaù End coøn sau end keát thuùc thuû tuïc laø daáu chaám phaåy (;).  Khi thöïc hieän, ta phaûi vieát leänh goïi thuû tuïc töông töï nhö caùc thuû tuïc chuaån. Trong Pascal leänh goïi thöïc hieän thuû tuïc nhö sau: <teân thuû tuïc>[(caùc tham soá thöïc söï)];
  • 9. program VD_thutuc1; procedure Ve_Hcn; begin writeln('* * * * * * *'); writeln('* *'); writeln('* * * * * * *'); end; begin Ve_Hcn; writeln;writeln; Ve_Hcn; writeln;writeln; Ve_Hcn; readln end. Caùch vieát vaø söû duïng thuû tuïc Thuû tuïc Ve_Hcn (chöông trình con Ve_Hcn) Leänh goïi chöông trình con
  • 10. program VD_thutuc1; procedure Ve_Hcn; begin writeln('* * * * * * *'); writeln('* *'); writeln('* * * * * * *'); end; Begin Ve_Hcn; writeln;writeln; Ve_Hcn; writeln;writeln; Ve_Hcn; readln end. Giaû söû ta caàn veõ nhieàu hình chöõ nhaät vôùi kích thöôùc khaùc nhau (25x10; 5x10; 4x2), thuû tuïc naøy coù thöïc hieän ñöôïc khoâng ? Ñeå giaûi quyeát baøi toaùn treân, thuû tuïc Ve_Hcn ñöôïc vieát laïi nhö theá naøo ? Caùch vieát vaø söû duïng thuû tuïc
  • 11. • Ví duï 1: Chöông trình veõ caùc hình chöõ nhaät coù kích thöôùc khaùc nhau baèng caùch duøng thuû tuïc Ve_Hcn vôùi caùc tham soá chdai, chrong. Giaù trò cuûa caùc tham soá chdai, chrong laø nguyeân (integer). •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc Khi ñoù phaàn ñaàu cuûa thuû tuïc ñöôïc vieát nhö sau: procedure Ve_hcn(chdai, chrong: integer); Khi ñoù phaàn ñaàu cuûa thuû tuïc Ve_Hcn ñöôïc vieát nhö theá naøo?
  • 12. 1. procedure Ve_Hcn(chdai, chrong: integer); 2. var i, j: integer; 3. begin 4. for i:=1 to chdai do write('*'); 5. writeln; 6. for j:=1 to chrong-2 do begin write('*'); for i:=1 to chdai-2 do write(' '); writeln('*'); end; 7. for i:=1 to chdai do write('*'); 8. writeln; 9. end; •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc Tham soá hình thöùc Veõ caïnh treân cuûa hình chöõ nhaät Veõ hai caïnh beân Veõ caïnh döôùi
  • 13. begin clrscr; Ve_Hcn(25,10); writeln;writeln; Ve_Hcn(5,10); readln; clrscr; a:=4; b:=2; for i:=1 to 4 do begin Ve_Hcn(a,b); readln; clrscr; a:=a*2; b:=b*2; end; readln; end. * Trong leänh goïi thuû tuïc, caùc tham soá hình thöùc ñöôïc thay baèng caùc tham soá thöïc söï töông öùng laø caùc giaù trò cuï theå ñöôïc goïi laø tham soá giaù trò (tham trò). •Caâu leänh goïi thuû tuïc coù daïng: •<teân thuû tuïc>[(caùc tham soá thöïc söï)]; •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc
  • 14. •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc • Ví duï 2: Chöông trình hoaùn ñoåi giaù trò cuûa hai bieán, baèng caùch duøng thuû tuïc Hoan_doi. x y TG TG TG x y TG x y TG := x (1) x := y (2) y := TG (3) •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc
  • 15. 1. program VD_thambien1; 2. uses crt; 3. var a, b: integer; 4. procedure Hoan_doi(var x,y: integer); 5. var TG: integer; 6. begin TG:= x; x:= y; y:= TG; end; 7. begin 8. clrscr; 9. a:=5; b:=10; 10. writeln(a:6, b:6); 11. Hoan_doi(a,b); 12. writeln(a:6, b:6); 13. readln; 14. end. •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc Tham soá hình thöùc laø caùc tham soá bieán Leänh goïi thuû tuïc Hoan_doi vôùi caùc tham soá ñöôïc thay baèng caùc bieán Thay ñoåi giaù trò cuûa caùc bieán
  • 16. program VD_thambien1; uses crt; var a, b: integer; procedure Hoan_doi(var x,y: integer); var TG: integer; begin TG:= x; x:= y; y:= TG; end; begin clrscr; a:=5; b:=10; writeln(a:6, b:6); Hoan_doi(a,b); writeln(a:6, b:6); readln; end. •Trong leänh goïi thuû tuïc, caùc tham soá hình thöùc ñöôïc thay baèng caùc tham soá thöïc söï töông öùng laø teân caùc bieán chöùa döõ lieäu ra ñöôïc goïi laø caùc tham soá bieán (tham bieán). •Ñeå phaân bieät tham bieán vaø tham trò, •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc
  • 17. program VD_thambien2; uses crt; var a, b: integer; procedure Hoan_doi(x: integer; var y: integer); var TG: integer; begin TG:= x; x:= y; y:= TG; end; begin clrscr; a:=5; b:=10; writeln(a:6, b:6); Hoan_doi(a,b); writeln(a:6, b:6); readln; end. •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc Khaùc nhau trong khai baùo tham soá hình thöùc Khaùc nhau ôû keát quaû sau khi thöïc hieän thuû tuïc Ñieåm khaùc nhau giöõa tham trò vaø tham bieán ? •* Tham soá bieán ñöôïc duøng ñeå ghi laïi •döõ lieäu keát quaû cuûa vieäc thöïc hieän •thuû tuïc. •* Tham soá giaù trò chæ ñeå ñöa döõ lieäu •vaøo cho thuû tuïc khi baét ñaàu thöïc hieän •thuû tuïc. •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc
  • 18. 1. program VD_thambien2; 2. uses crt; 3. var a, b: integer; 4. procedure Hoan_doi(x: integer; var y: integer); 5. var TG: integer; 6. begin TG:= x; x:= y; y:= TG; end; 7. begin 8. clrscr; 9. a:=5; b:=10; 10. writeln(a:6, b:6); 11. Hoan_doi(a,b); 12. writeln(a:6, b:6); 13. readln; 14.end. •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc procedure Hoan_doi(var x: integer; y: integer); Keát quaû nhaän ñöôïc (treân maøn hình) nhö theá naøo khi thöïc hieän chöông trình ?
  • 19. * Chuù yù: • - Khi khai baùo caùc tham soá hình thöùc, neáu coù kieåu khaùc nhau thì caùc tham soá seõ caùch nhau baèng daáu chaám phaåy, neáu coù cuøng kieåu thì caùch nhau baèng daáu phaåy. • Ví duï: • procedure CTC(x, y, z: integer; r: real); •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc
  • 20. * Chuù yù: • - Khi khai baùo döõ lieäu cho caùc tham soá hình thöùc chæ ñöôïc duøng teân kieåu. Teân kieåu laø teân chuaån hoaëc teân do ngöôøi laäp trình ñaët. • Ví duï: Khai baùo hôïp leä: procedure Alpha(N: integer; var s: string); •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc
  • 21. -Khai baùo khoâng hôïp leä: procedure Delta(M: array[1..10] of byte; var X: string[25]); -Khai baùo hôïp leä: type mang = array[1..10] of byte; xau = string[25]; procedure Delta(M: mang; var X: xau); •b. Ví duï veà thuû tuïc Caùch vieát vaø söû duïng thuû tuïc
  • 22. CUÛNG COÁ VAØ LUYEÄN TAÄPCUÛNG COÁ VAØ LUYEÄN TAÄP  Caùch vieát moät thuû tuïc trong ngoân ngöõ laäp trình Pascal. Hieåu moái lieân quan giöõa chöông trình chính vaø thuû tuïc;  Leänh goïi thöïc hieän thuû tuïc trong chöông trình chính;  Tham soá bieán vaø tham soá giaù trò; CUÛNG COÁCUÛNG COÁ
  • 23. 1. Töø khoùa ñeå khai baùo thuû tuïc laø: A. Program B. Procedure C. Function D. Processor 2. Muoán khai baùo x, y laø tham soá giaù trò (x, y thuoäc kieåu integer) trong thuû tuïc coù teân laø “ABC” thì khai baùo naøo sau ñaây laø ñuùng? A. Procedure ABC(x, y: integer); B. Procedure ABC(var x, y: integer); C. Procedure ABC(x: integer; var y: integer); D. Procedure ABC(var x: integer; y: integer); CUÛNG COÁ
  • 24. 3. Muoán khai baùo x laø tham soá giaù trò, y vaø z laø tham soá bieán (x, y, z thuoäc kieåu integer) trong thuû tuïc coù teân laø “CTC” thì khai baùo naøo sau ñaây laø ñuùng? A. Procedure CTC(x, y, z: integer); B. Procedure CTC(var x, y, z: integer); C. Procedure CTC(var x:integer; y, z: integer); D. Procedure CTC(x: integer; var y, z: integer); CUÛNG COÁ
  • 25. 4. Cho chöông trình sau: var a, b, c: integer; procedure Hoan_doi(var a, b: integer); var TG: integer; begin TG:= a; a:= b; b:= TG; end; begin clrscr; a:= 10; b:= 11; c:= 12; Hoan_doi(b,c); Hoan_doi(a,b); Hoan_doi(b,c); writeln(a, ‘ ’, b ‘ ’, c); end. Keát quaû nhaän ñöôïc (treân maøn hình) sau khi thöïc hieän chöông trình laø: A. 10 11 12 B. 11 10 12 C. 12 11 10 D. 12 10 11 CUÛNG COÁCUÛNG COÁ
  • 26. DAËN DOØ VAØ HÖÔÙNG DAÃN TÖÏDAËN DOØ VAØ HÖÔÙNG DAÃN TÖÏ HOÏCHOÏC  Veà nhaø hoïc laïi toaøn boä kieán thöùc baøi hoïc.  Ñoïc tröôùc muïc 2. Caùch vieát vaø söû duïng haøm  Laøm vaø traû lôøi caùc caâu hoûi vaø baøi taäp trong saùch giaùo khoa vaø laøm theâm caùc baøi taäp 6.1, 6.1, 6.3 tr. 55; Baøi 6.13, 6.14, 6.17, 6.18 trang 58,60.
  • 27. Tieát hoïc ñeán ñaây laø heát. • Xin chuùc quyù Thaày (Coâ) cuøng caùc em hoïc sinh • Vui - Khoeû vaø Thaønh coâng. • Xin caùm ôn!