SlideShare a Scribd company logo
1 of 24
ตัวแปรแบบ Structure
structure เป็นกลุ่มข้อมูลซึ่งประกอบ
ด้วยข้อมูลชนิดเดียวกันหรือหลาย
ชนิดก็ได้ข้อมูลแต่ละรายการใน
structure เรียกว่า สมาชิกของสต
รักเจอร (members of structure)
โครงสร้างตัวแปรแบบ Structure
struct StructName
{
DataType
FieldName ;
DataType
FieldName ;
DataType
FieldName ;
DataType
FieldName ;
ชื่อ Struct
ชนิดข้อมูล
ชื่อ
รายการ
ชื่อตัวแปร
Struct
ตัวอย่างโครงสร้างตัวแปรแบบ
Structure
struct STD
{
string ID;
string Name;
int ThaiScore ;
int
SocialScore ;
int MathScore ;
}StudentScore ;
การเรียกใช้ Structure และ สมาชิก
ของ Structure
struct STD
{
string ID;
string
Name;
int
ThaiScore ;
int
SocialScore
;
สามารถเรียกใช้ได้โดย
ชื่อตัวแปร เครื่องหมายจุด ชื่อ
รายการ ของ Struct
StudentScore.ID
StudentScore.Na
me
StudentScore.Th
aiScore
StudentScore.So
cialScore
StudentScore.Ma
ลองทำาดู
เขียนโปรแกรม ในรูปแบบ
Structure เพื่อเก็บข้อมูลดังต่อไปนี้
ID //รหัสนักศึกษา
MidtermScore //คะแนนกลางภาค
FinalScore //คะแนนปลายภาค
AnotherScore //คะแนนอื่นๆระหว่างภาค
จากนั้นให้คำานวณเกรด เก็บไว้
ในตัวแปร Grade แล้วแสดง
ผลลัพธทั้งหมดออกสู่หน้าจอ
ฟังก์ชั่นคืออะไร ?
คือ ชุดคำำสั่งที่เขียนขึ้นเพื่อกำรทำำงำน
เฉพำะอย่ำง และสำมำรถเรียกใช้งำน
ได้หลำยๆ ครั้ง โดยไม่ต้องเขียนชุดคำำ
สั่งดังกล่ำวใหม่
 ฟังก์ชันในภำษำ C++ สำมำรถแบ่ง
เป็น 2 ประเภท คือ
 ฟังก์ชันมำตรฐำน หรือ ไลบรำรี่ฟังก์ชัน
(Library Function)
ฟังก์ชันมำตรฐำน หรือ ไลบรำรี่
ฟังก์ชัน (Library Function)
คือ ฟังก์ชันมำตรฐำนที่บริษัทผู้สร้ำง
Compiler ภำษำ C++ ได้
สร้ำงรวบรวมไว้ในคลัง (Library) ผู้เขียน
โปรแกรมสำมำรถเรียกใช้ได้ทันทีได้แก่
ฟังก์ชันที่ประกอบอยู่ใน header file ต่ำง ๆ
ขณะเรียกใช้ ต้อง #include ชื่อไฟลที่รวบ
รวมฟังก์ชันนั้นไว้ก่อน เช่น ฟังก์ชัน pow(
, ) ต้อง include Library File
math.h
User Defined Function (ฟังก์ชันที่ผู้
ใช้สร้ำงขึ้นเอง)
คือ ฟังก์ชันที่ผู้เขียนโปรแกรมสร้ำงขึ้น
หรือกำำหนดขึ้นเอง
ตำมรูปแบบกำรสร้ำงฟังก์ชันของ C++ เพื่อ
นำำมำใช้ในกำรเขียนโปรแกรมของตนเอง
รูปแบบกำรเขียน Function
ประกำศ Prototype
สร้ำง function
 ประกำศส่วนหัวของฟังก์ชัน
 เขียนคำำสั่งต่ำงๆ ที่ต้องทำำในฟังก์ชันนั้นๆ
 ถ้ำมีกำรส่งค่ำกลับมำยังฟังก์ชันที่เรียก ให้เพิ่มคำำ
สั่ง return (ชื่อตัวแปร);
เรียกใช้ฟังก์ชัน
กำรประกำศ Prototype
ต้องคำำนึง !! ถึงรูปแบบฟังก์ชั่นก่อน
ประกำศ Prototype โดยรูปแบบฟังก์ชั่นมี 3
รูปแบบ ดังต่อไปนี้
ฟังก์ชันที่มีรับและส่งค่ำกลับ
ฟังก์ชันที่มีกำรรับแต่ไม่มีกำรส่งค่ำก
ฟังก์ชันที่ไม่มีกำรรับและส่งค่ำกลับ
การประกาศ Prototype (ต่อ)
งก์ชันที่มีรับและส่งค่ากลับ
DataTypeFunctionName(DatatypeVar1, DatatypeV
statement(s);
return(value);
การประกาศ Prototype (ต่อ)
อย่างฟังก์ชันที่มีรับและส่งค่ากลับ
int square(int width, int height)
{
int result=0;
result=width*height;
return(result);
}
การประกาศ Prototype (ต่อ)
างฟังก์ชันที่มีรับและส่งค่ากลับ (ฟังก์ช
Voidmain()
{
int width=0, height=0, result=0;
cin>>width>>height;
result=square(width, height);
cout<<result;
}
การประกาศ Prototype (ต่อ)
ก์ชันที่มีการรับแต่ไม่มีการส่งกลับ
voidFunctionName(DatatypeVar1, DatatypeVar2)
statement(s);
การประกาศ Prototype (ต่อ)
ก์ชันที่มีการรับแต่ไม่มีการส่งกลับ
Voidsquare(int width, int height)
{
int result=0;
result=width*height;
cout<<result;
}
การประกาศ Prototype (ต่อ)
ฟังก์ชันที่มีการรับแต่ไม่มีการส่ง
กลับ (ฟังก์ชั่น main)Voidmain()
{
int width=0, height=0;
cin>>width>>height;
square(width, height);
}
การประกาศ Prototype (ต่อ)
ก์ชันที่ไม่มีการรับและส่งค่ากลับ
voidFunctionName()
{
statement(s);
}
การประกาศ Prototype (ต่อ)
ก์ชันที่ไม่มีการรับและส่งค่ากลับ
Voidsquare()
{
int width=0, height=0, result=0;
cin>>width>>height;
result=width*height;
cout<<result;
}
การประกาศ Prototype (ต่อ)
ฟังก์ชันที่ไม่มีการรับและส่งค่า
กลับ (ฟังก์ชั่น main)
Voidmain()
{
square();
}
int square (int,int);
void main(){
result=square(width, height);
}
int square (int width, int height)
{
int result=0;
result=width*height;
return(result);
}
int square (int width, int height)
{
int result=0;
result=width*height;
return(result);
}
void main(){
result=square(width, height);
}
Questio
n
?

More Related Content

Viewers also liked

Grand Lake Area Real Estate Symposium 2013-Market Statistics
Grand Lake Area Real Estate Symposium 2013-Market StatisticsGrand Lake Area Real Estate Symposium 2013-Market Statistics
Grand Lake Area Real Estate Symposium 2013-Market StatisticsRE/MAX Grand Lake
 
Business Intelligence via Social Media Data The Socializers
Business Intelligence via Social Media Data The SocializersBusiness Intelligence via Social Media Data The Socializers
Business Intelligence via Social Media Data The SocializersThe Socializers
 
Samenwerken met nieuwe media tbv sociale veiligheid
Samenwerken met nieuwe media tbv sociale veiligheidSamenwerken met nieuwe media tbv sociale veiligheid
Samenwerken met nieuwe media tbv sociale veiligheidTNO
 
Greece (acrostic).Activity 3. By Michael Terezakis
Greece (acrostic).Activity 3. By Michael TerezakisGreece (acrostic).Activity 3. By Michael Terezakis
Greece (acrostic).Activity 3. By Michael TerezakisCarlos Ajamil Royo
 
Drew Production Samples Complete
Drew Production Samples CompleteDrew Production Samples Complete
Drew Production Samples Completeguestb48c6a
 
καταχώρηση 2ου σχολείου
καταχώρηση 2ου σχολείουκαταχώρηση 2ου σχολείου
καταχώρηση 2ου σχολείουPetros Michailidis
 
NJ Edge Web2.0 Presentation by Braender and Kapp 2009
NJ Edge Web2.0 Presentation by Braender and Kapp 2009NJ Edge Web2.0 Presentation by Braender and Kapp 2009
NJ Edge Web2.0 Presentation by Braender and Kapp 2009The College of New Jersey
 
Keynote Steve Outram Elluminate 12 Nov
Keynote   Steve Outram   Elluminate 12 NovKeynote   Steve Outram   Elluminate 12 Nov
Keynote Steve Outram Elluminate 12 NovJISC SSBR
 
Fixing Tajikistan’s DV Law
Fixing Tajikistan’s DV LawFixing Tajikistan’s DV Law
Fixing Tajikistan’s DV LawJosh Friedman
 
451 Lesson Plan 1
451  Lesson  Plan 1451  Lesson  Plan 1
451 Lesson Plan 1guest74979f
 
Bildspel från tidigare DIK-kongresser
Bildspel från tidigare DIK-kongresserBildspel från tidigare DIK-kongresser
Bildspel från tidigare DIK-kongresserDIK
 
Arvutiõpetus
ArvutiõpetusArvutiõpetus
ArvutiõpetusAnnika
 
Ingles Oral Presentation
Ingles Oral PresentationIngles Oral Presentation
Ingles Oral PresentationSvalent
 
[1041] basileios boulgaroktonos
[1041] basileios boulgaroktonos[1041] basileios boulgaroktonos
[1041] basileios boulgaroktonosPetros Michailidis
 

Viewers also liked (18)

Grand Lake Area Real Estate Symposium 2013-Market Statistics
Grand Lake Area Real Estate Symposium 2013-Market StatisticsGrand Lake Area Real Estate Symposium 2013-Market Statistics
Grand Lake Area Real Estate Symposium 2013-Market Statistics
 
Business Intelligence via Social Media Data The Socializers
Business Intelligence via Social Media Data The SocializersBusiness Intelligence via Social Media Data The Socializers
Business Intelligence via Social Media Data The Socializers
 
Samenwerken met nieuwe media tbv sociale veiligheid
Samenwerken met nieuwe media tbv sociale veiligheidSamenwerken met nieuwe media tbv sociale veiligheid
Samenwerken met nieuwe media tbv sociale veiligheid
 
Greece (acrostic).Activity 3. By Michael Terezakis
Greece (acrostic).Activity 3. By Michael TerezakisGreece (acrostic).Activity 3. By Michael Terezakis
Greece (acrostic).Activity 3. By Michael Terezakis
 
Drew Production Samples Complete
Drew Production Samples CompleteDrew Production Samples Complete
Drew Production Samples Complete
 
Transmedia 101
Transmedia 101Transmedia 101
Transmedia 101
 
καταχώρηση 2ου σχολείου
καταχώρηση 2ου σχολείουκαταχώρηση 2ου σχολείου
καταχώρηση 2ου σχολείου
 
Ict
IctIct
Ict
 
NJ Edge Web2.0 Presentation by Braender and Kapp 2009
NJ Edge Web2.0 Presentation by Braender and Kapp 2009NJ Edge Web2.0 Presentation by Braender and Kapp 2009
NJ Edge Web2.0 Presentation by Braender and Kapp 2009
 
Keynote Steve Outram Elluminate 12 Nov
Keynote   Steve Outram   Elluminate 12 NovKeynote   Steve Outram   Elluminate 12 Nov
Keynote Steve Outram Elluminate 12 Nov
 
JenH2k
JenH2kJenH2k
JenH2k
 
Fixing Tajikistan’s DV Law
Fixing Tajikistan’s DV LawFixing Tajikistan’s DV Law
Fixing Tajikistan’s DV Law
 
451 Lesson Plan 1
451  Lesson  Plan 1451  Lesson  Plan 1
451 Lesson Plan 1
 
Bildspel från tidigare DIK-kongresser
Bildspel från tidigare DIK-kongresserBildspel från tidigare DIK-kongresser
Bildspel från tidigare DIK-kongresser
 
Arvutiõpetus
ArvutiõpetusArvutiõpetus
Arvutiõpetus
 
11
1111
11
 
Ingles Oral Presentation
Ingles Oral PresentationIngles Oral Presentation
Ingles Oral Presentation
 
[1041] basileios boulgaroktonos
[1041] basileios boulgaroktonos[1041] basileios boulgaroktonos
[1041] basileios boulgaroktonos
 

Similar to Lab Chapter7 Structure And Function

Java Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionJava Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionIMC Institute
 
บทที่1 ความรู้พื้นฐาน
บทที่1 ความรู้พื้นฐานบทที่1 ความรู้พื้นฐาน
บทที่1 ความรู้พื้นฐานtapabnum
 
บทที่ 2 สถาปัตยกรรม
บทที่ 2 สถาปัตยกรรมบทที่ 2 สถาปัตยกรรม
บทที่ 2 สถาปัตยกรรมPrinceStorm Nueng
 
2การแสดงผลและการรับข้อมูล
2การแสดงผลและการรับข้อมูล2การแสดงผลและการรับข้อมูล
2การแสดงผลและการรับข้อมูลteedee111
 

Similar to Lab Chapter7 Structure And Function (10)

งานทำ Blog บทที่ 11
งานทำ Blog บทที่ 11งานทำ Blog บทที่ 11
งานทำ Blog บทที่ 11
 
Java Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionJava Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and Collection
 
บทที่1 ความรู้พื้นฐาน
บทที่1 ความรู้พื้นฐานบทที่1 ความรู้พื้นฐาน
บทที่1 ความรู้พื้นฐาน
 
it-06-50
it-06-50it-06-50
it-06-50
 
บทที่ 2 สถาปัตยกรรม
บทที่ 2 สถาปัตยกรรมบทที่ 2 สถาปัตยกรรม
บทที่ 2 สถาปัตยกรรม
 
2การแสดงผลและการรับข้อมูล
2การแสดงผลและการรับข้อมูล2การแสดงผลและการรับข้อมูล
2การแสดงผลและการรับข้อมูล
 
Chapter 02 Flowchart
Chapter 02 FlowchartChapter 02 Flowchart
Chapter 02 Flowchart
 
Database1
Database1Database1
Database1
 
lesson1 JSP
lesson1 JSPlesson1 JSP
lesson1 JSP
 
Lesson 1
Lesson 1Lesson 1
Lesson 1
 

More from นายพิศณุ ท่าสอน (7)

Chapter 05 Loop
Chapter 05 LoopChapter 05 Loop
Chapter 05 Loop
 
Array1
Array1Array1
Array1
 
Array 2
Array 2Array 2
Array 2
 
Chapter 05 Loop
Chapter 05 LoopChapter 05 Loop
Chapter 05 Loop
 
Chapter 04 Compare
Chapter 04 CompareChapter 04 Compare
Chapter 04 Compare
 
Variable Constant Math
Variable Constant MathVariable Constant Math
Variable Constant Math
 
Chapter 01 Analysis 1
Chapter 01 Analysis 1Chapter 01 Analysis 1
Chapter 01 Analysis 1
 

Lab Chapter7 Structure And Function