Lab Chapter7 Structure And Function

Preview:

Citation preview

ตวแปรแบบ Structure

structure เปนกลมขอมลซงประกอบดวยขอมลชนดเดยวกนหรอหลายชนดกไดขอมลแตละรายการใน structure เรยกวา สมาชกของสตรกเจอร (members of structure)

โครงสรางตวแปรแบบ Structure

struct StructName{ DataType

FieldName ; DataType

FieldName ;DataType FieldName ;DataType FieldName ;

}StructVariable ;

ชอ 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 ;int MathScore ;

}StudentScore ;

สามารถเรยกใชไดโดย ชอตวแปร เครองหมายจด ชอรายการ ของ Struct

StudentScore.IDStudentScore.NameStudentScore.ThaiScoreStudentScore.SocialScoreStudentScore.MathScore

ลองทำาด

เขยนโปรแกรม ในรปแบบ Structure เพอเกบขอมลดงตอไปน

ID //รหสนกศกษาMidtermScore //คะแนนกลางภาคFinalScore //คะแนนปลายภาคAnotherScore //คะแนนอนๆระหวางภาค

จากนนใหคำานวณเกรด เกบไวในตวแปร Grade แลวแสดงผลลพธทงหมดออกสหนาจอ

ฟงกชนคออะไร ?

คอ ชดคำำสงทเขยนขนเพอกำรทำำงำนเฉพำะอยำง และสำมำรถเรยกใชงำนไดหลำยๆ ครง โดยไมตองเขยนชดคำำสงดงกลำวใหม

ฟงกชนในภำษำ C++ สำมำรถแบงเปน 2 ประเภท คอ ฟงกชนมำตรฐำน หรอ ไลบรำรฟงกชน

(Library Function) ฟงกชนทผใชสรำงขนเอง (User Defined

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 (ตอ)

ฟงก ช นท ม ร บและส งค ากล บ

DataType FunctionName (Datatype Var1, Datatype Var2){

statement(s);return(value);

}

การประกาศ Prototype (ตอ)

ตวอย างฟ งก ช นท ม ร บและส งค ากล บint square (int width, int height){

int result=0;result=width*height;return(result);

}

การประกาศ Prototype (ตอ)

ตวอย างฟ งก ช นท มร บและส งค ากล บ ( ฟงก ช น main)

Void main (){

int width=0, height=0, result=0;cin>>width>>height;result= square(width, height);cout<<result;

}

การประกาศ Prototype (ตอ)

ฟงก ช นท ม การร บแต ไมม การส งกล บ

void FunctionName (Datatype Var1, Datatype Var2){

statement(s);}

การประกาศ Prototype (ตอ)

ฟงก ช นท ม การร บแต ไม มการส งกล บVoid square (int width, int height){

int result=0;result=width*height;cout<<result;

}

การประกาศ Prototype (ตอ)

ฟงก ช นท ม การร บแต ไม มการส ง กลบ ( ฟงก ช น main)

Void main (){

int width=0, height=0;cin>>width>>height;square(width, height);

}

การประกาศ Prototype (ตอ)

ฟงก ช นท ไม ม การร บและส งค ากล บ

void FunctionName (){

statement(s);}

การประกาศ Prototype (ตอ)

ฟงก ช นท ไม ม การร บและส งค ากล บVoid square (){

int width=0, height=0, result=0;cin>>width>>height;result=width*height;cout<<result;

}

การประกาศ Prototype (ตอ)

ฟงก ช นท ไม ม การร บและส งค า กลบ ( ฟงก ช น main)Void main (){

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);

}

Question

?

Recommended