Upload
mansuang1978
View
631
Download
0
Embed Size (px)
Citation preview
ฟงก ช นในภาษาซ
ใชส ำาหร บ Turbo C++ Version 3.0
เน อหาเน อหา ฟงกชนคออะไร โครงสรางของฟงกชน ฟงกชนชนดตาง การเขยน prototype ของฟงกชน Library Function
ฟงกช นคออะไรฟงกช นคออะไรในการเขยนโปรแกรมคอมพวเตอรบางครง
จะตองมชดคำาสงบางชดทจะตองทำาบอยๆ ถา หากจะตองเขยนชดคำาสงนนบอยครง กจะ
ทำาใหโปรแกรมมขนาดใหญ เราจงสามารถรวมเอาชดคำาสงบอยๆ นนมารวมเปนฟงกชน
ฟงกชน คอ กลมของคำาสงทสามารถใชเพอทำางานเฉพาะอยาง
ประโยชนทสำาคญของฟงกชนคอชวยลด ความซำาซอนของโปรแกรม นอกจากนยง
ทำาใหตรวจไดงาย
ฟงกช นในภาษาซฟงกช นในภาษาซ ฟงกชนในภาษาซ แบงออกไดเปน 2 ประเภทคอ
ฟงก ช นมาตรฐาน (Standard Function) เปนฟงกชนทตว Compiler มมาใหอยแลว
เชนฟงกชนการคำานวณทางคณตศาสตร ฟงกชนการรบและแสดงผล เหลานเรยกวา
Library Function ในการใชงานจะตอง เรยกใช Include Directives จงสามารถใช
งานฟงกชนนนๆ ไดฟงก ช นท ผ เข ยนโปรแกรมสร างข น (User-defined Function) เปนฟงกชนหรอโปรแกรมยอยทผใชสรางขนมาใชงาน
ในโปรแกรม เพอทำางานใดงานหนงmain() ถอเปนฟงกชนชนดหนงทเปนจดเรม
ตนทำางานของโปรแกรม
ฟงกช นในภาษาซฟงกช นในภาษาซในการเขยนโปรแกรมดวยภาษาซจะตองม
ฟงกชน main() เสมอ และถาตองการให โปรแกรมทำางานใดๆ ฟงกชน main() จะไป
เรยกใชฟงกชนอนๆ ใหทำางาน และในฟงกชน ทสรางขนกยงสามารถเรยกใชฟงกชนอนๆ ได
อกดวย
ร ปโครงสร างของฟงก ช นในภาษาซ
โครงสร างของฟงกช นโครงสร างของฟงกช น ฟงกชนมาตรฐานและฟงกชนทผใชสรางขน ม
โครงสราง ดงน
type เปนชนดของผลลพธทไดจากการทำางานของ ฟงกชน หากไมกำาหนด จะถกกำาหนดเปน int ใน
กรณทไมตองการสงคากลบใหใส void function-name คอ ชอของฟงกชนทสรางชน
ควรตงใหสอความหมาย หลกการตงชอเชนเดยวกบตวแปร
type function-name(type para-1, type para-2, ... , type para-n){ local variable-declarations ; statement-1 ; statement-2 ; ... statement-n ; return(value) ;}
type function-name(type para-1, type para-2, ... , type para-n){ local variable-declarations ; statement-1 ; statement-2 ; ... statement-n ; return(value) ;}
type function-name(type para-1, type para-2, ... , type para-n){ local variable-declarations ; statement-1 ; statement-2 ; ... statement-n ; return(value) ;}
โครงสร างของฟงกช นโครงสร างของฟงกช น ฟงกชนมาตรฐานและฟงกชนทผใชสรางขน ม
โครงสราง ดงน
type para-1, type para-2, ... , type para-n หรอ parameter เปนตวแปรทตองสงเขามาเพอ
นำามาประมวลผลเมอมการเรยกใชฟงกชนพรอมทง กำาหนดชนดของตวแปรดวย type หากมตวแปร
มากกวาหนงใหคนแตละตวดวยเครองหมาย "," หากไมมตวแปรสงเขามาใหใสวงเลบวาง () หรอใส
(void)
type function-name(type para-1, type para-2, ... , type para-n){ local variable-declarations ; statement-1 ; statement-2 ; ... statement-n ; return(value) ;}
type function-name(type para-1, type para-2, ... , type para-n){ local variable-declarations ; statement-1 ; statement-2 ; ... statement-n ; return(value) ;}
โครงสร างของฟงกช นโครงสร างของฟงกช น ฟงกชนมาตรฐานและฟงกชนทผใชสรางขน ม
โครงสราง ดงน
ชดค ำาส งท งหมด จะตองรวมอยในวงเลบปกกา { } local variable-declarations เปนการสรางตวแปรเพอใชงานภายในฟงกชน
statement-1, statement-2, ... , statement-n ชดคำาสงทรวมอยภายในฟงกชน
type function-name(type para-1, type para-2, ... , type para-n){ local variable-declarations ; statement-1 ; statement-2 ; ... statement-n ; return(value) ;}
type function-name(type para-1, type para-2, ... , type para-n){ local variable-declarations ; statement-1 ; statement-2 ; ... statement-n ; return(value) ;}
type function-name(type para-1, type para-2, ... , type para-n){ local variable-declarations ; statement-1 ; statement-2 ; ... statement-n ; return(value) ;}
type function-name(type para-1, type para-2, ... , type para-n){ local variable-declarations ; statement-1 ; statement-2 ; ... statement-n ; return(value) ;}
โครงสร างของฟงกช นโครงสร างของฟงกช น ฟงกชนมาตรฐานและฟงกชนทผใชสรางขน ม
โครงสราง ดงน
return(value) ใชคนคาออกจากฟงกชน โดยคาทคนออกไปจะเปนชนดเดยวกนกบทประกาศไว
กอน หนาชอฟงกชน หากไมมการคนคากจะไมตองใสในสวนน
type function-name(type para-1, type para-2, ... , type para-n){ local variable-declarations ; statement-1 ; statement-2 ; ... statement-n ; return(value) ;}
type function-name(type para-1, type para-2, ... , type para-n){ local variable-declarations ; statement-1 ; statement-2 ; ... statement-n ; return(value) ;}
ฟงกช นประเภทตางๆฟงกช นประเภทตางๆ ฟงกชนสามารถแบงออกเปน 3 ประเภท ตาม
การรบ-สงคาขอมล ฟงก ช นประเภทไมมการร บส งค าใดๆ เปนแบบ
ทงายทสด คอไมตองสงพารามเตอรใหกบฟงกชน และฟงกชนไมสงคาอะไรคนออกมา โดยปกตใช
แสดงขอความทมการใชงานบอยๆ ฟงก ช นแบบสงค าผ านทางเด ยว สามารถเปน
ได 2 แบบคอ ฟงกชนทมการรบคาเขามาทำางานใน ฟงกชนอยางเดยว หรอมการคนคาออกมาอยาง
เดยว ฟงก ช นแบบสงค าสองทาง ฟงกชนทมการรบคา
เขามาทำางานในฟงกชนและ มการคนคาออกมา ซง สวนใหญใชคำานวณคาตางๆ จะตองมตวแปร
รองรบคาจากฟงกชนดวย
ตวอย าง 1 โปรแกรมทย งไม ได สร างฟงก ชน#include <stdio.h>#include <conio.h>void main(){ printf("********************\n"); printf("StarLine Printing\n"); printf("********************\n"); getch();}
โปรแกรม
ผลการ ทำางาน ?********************
StarLine Printing********************
ตวอย าง 1 โปรแกรมทย งไม ได สร างฟงก ชน#include <stdio.h>#include <conio.h>
void main(){ printf("********************\n"); printf("StarLine Printing\n"); printf("********************\n"); getch();}
โปรแกรม
ผลการ ทำางาน ?********************
StarLine Printing********************
แทรกชองว าง
ตวอย าง 1 โปรแกรมทย งไม ได สร างฟงก ชน#include <stdio.h>#include <conio.h> StarLine( )
printf("********************\n");
void main(){ StarLine(); printf("StarLine Printing\n"); StarLine(); getch();}
โปรแกรม
ผลการ ทำางาน ?********************
StarLine Printing********************
ชอท ต องการเร ยกใชงาน
คำาส งเด มท ใช
ช อของฟงก ช น
ตวอย าง 1 ฟงกชนท ไมม การร บส งค า#include <stdio.h>#include <conio.h>void StarLine(void){ printf("********************\n");}void main(){ StarLine(); printf("StarLine Printing\n"); StarLine(); getch();}
โปรแกรม
ผลการ ทำางาน ?********************
StarLine Printing********************
ไมม การร บสงคา
การเร ยกใชงาน
ตวอย าง 2 ฟงกชนท มการร บค า#include <stdio.h>#include <conio.h>void StarLine(int n){ int i; for(i=1; i<=n; i++) printf("*"); printf("\n");}void main(){ StarLine(17); printf("StarLine Printing\n"); StarLine(17); getch();}
โปรแกรม
ผลการ ทำางาน ?*****************
StarLine Printing*****************
มการร บคาn
การเร ยกใช
ฟงกช นท มการรบค าฟงกช นท มการรบค าฟงกชนทมการรบคาสามารถแบงยอยได
เปนประเภทตางๆ ตามลกษณะของพารามเตอร ทสงใหฟงกชน ดงน
Pass by Value ฟงกชนจะรบคาตวแปร คาคงท หรอนพจน โดยการเปลยนแปลงของตวแปรภายใน
จะไมมผลกบตวแปรดานนอก ถงแมตวแปรจะมชอเดยวกน
Pass by Reference ฟงกชนจะรบตำาแหนงใน หนวยความจำาของตวแปร หากมการเปลยนแปลง
คาภายในฟงกชน ตวแปรทอยนอกฟงกชนจะ เปลยนแปลงดวย เนองจากเปนตวแปรทใชหนวย
ความจำาตำาแหนงเดยวกนนอกจากนยงมการสงอาร เรย เปน
พารามเตอร ใหกบฟงกชน ซงจะถอเปนแบบPass by Reference
ตวอย าง 3 ฟงกชน Pass by Value
#include <stdio.h>#include <conio.h>void NumChange(int number){ printf("Before change : %d\n",number); number += 5; printf("After change : %d\n",number);}void main(){ int number = 15; NumChange(number); printf("After entering the function : %d\n",number); getch();}
โปรแกรม
ผลการ ทำางาน ?
Before change : 15After change : 20After entering the function : 15
มการร บคาnumberPass by Value
ตวอย าง 4 ฟงกชน Pass by Reference
#include <stdio.h>#include <conio.h>void NumChange(int &number2){ printf("Before change : %d\n",number2); number2 += 5; printf("After change : %d\n",number2);}void main(){ int number = 15; NumChange(number); printf("After entering the function : %d\n",number); getch();}
โปรแกรม
ผลการ ทำางาน ?
Before change : 15After change : 20After entering the function : 20
มการร บคาnumber2Pass by Reference
ตวอย าง 5 ฟงกชน Pass by Reference
#include <stdio.h>#include <conio.h>void NumChange(int *number2){ printf("Before change : %d\n",*number2); *number2 += 5; printf("After change : %d\n",*number2);}void main(){ int number = 15; NumChange(&number); printf("After entering the function : %d\n",number); getch();}
โปรแกรม
ผลการ ทำางาน ?
Before change : 15After change : 20After entering the function : 20
มการร บคาnumber2Pass by Reference
ตวอย าง 6 ฟงกชนท ร บค าเป นอาร เรย #include <stdio.h>#include <conio.h>void Dollar(float baht[], int num){ int i; printf("Before changing : %.02f\n", baht[0]); for(i=0; i<num; i++) baht[i] *= 40.0; printf("After changing : %.02f\n", baht[0]);}void main(){ float money[3] = {200.0, 400.0, 600.0}; Dollar(money, 3); printf("After entering function: %.02f\n", money[0]); getch();}
โปรแกรม
ผลการ ทำางาน ?
Before changing : 200.00After changing : 8000.00After entering function: 8000.00
ตวอย าง 7 ฟงกชนท มการส งค ากล บ#include <stdio.h>#include <conio.h>float FindArea(float radius){ return(3.14*radius*radius);}void main(){ float radius, area; printf("Enter Radius: "); scanf("%f", &radius); area = FindArea(radius); printf("Area is %0.2f\n", area); getch();}
โปรแกรม
ผลการ ทำางาน ?
Enter Radius: 5Enter Radius: 5Area is 78.50
มการร บสงคาสงคากล บ
เร ยกใชฟ งก ชนและม ตวแปรร บคา
การเรยกใชฟงกช นท มการการเรยกใชฟงกช นท มการสงคากล บสงคากล บ
การเรยกใชฟงกชนจะเรยกใชในสวนคำา สงของฟงกชนหลก (main) ลกษณะการเรยก
ใชจะเหมอนกบฟงกชนมาตรฐานทมอยแลวดงน
area = FindArea(radius);
หรอprintf("%f",FindArea(radius));
จะเหนไดวาการเรยกฟงกชนในฟงกชน หลกจะตองมตวแปร มารองรบผลลพธทไดจาก
ฟงกชนเสมอ เนองจากฟงกชนจะตองมการสง คากลบ (return) หรอเปนการเรยกใชและได
ผลลพธไปแสดงผลออกสหนาจอเลย
การเขยน การเขยน PrototypePrototype ของของฟงกช นฟงกช น
หากตองการเขยน main() เปนฟงกชนแรก ของโปรแกรมแลว จะใช prototype เพอ
ประกาศฟงกชนอนไวกอน โดยประกาศไวหลงPreprocessor Directive หรอไวใน main() เลยกได#include <stdio.h>#include <conio.h>void StarLine(void);void NumChange(int number);float FindArea(float radius);void main(){...}void StarLine(void){...}void NumChange(int number){...}float FindArea(float radius){...}
Prototype
Function
คอ ห วของฟงก ช นท สร างไว
การเรยกใชฟงกช นการเรยกใชฟงกช น การเรยกใชฟงกชน ในกรณทมการสราง ฟงกชนไวหลายฟงกชน จะสามารถเรยกใช
ฟงกชนทอยกอนหนาเทานนถาตองการทจะเรยกใชฟงกชนทอยดาน ลาง จะตองมการกำาหนด Prototype ของ
ฟงกชนนน ไวกอนฟงกชนทจะเรยกใช#include <stdio.h>#include <conio.h>void StarLine(void){...}void NumChange(int number){...}float FindArea(float radius){...}void main(){...}
เร ยกใช StarLine และNumChange ได
เร ยกใช StarLine ได
ไม สามารถเร ยกฟงก ช นใดได
เร ยกใชได ท กฟ งก ช น
แทรก Prototype จะทำาให เร ยกใชได ท กฟ งก ช นvoid NumChange(int number);float FindArea(float radius);
#include <stdio.h>#include <conio.h>void StarLine(void);void main(){ StarLine(); printf("StarLine Printing\n"); StarLine(); getch();}void StarLine(void){ printf("********************\n");}
โปรแกรม
ผลการ ทำางาน ?********************
StarLine Printing********************
Prototype
Function
ตวอย าง 8 การเข ยนPrototype ของฟงกชน
ตวอย าง 9 การเข ยนPrototype ของฟงกชน
#include <stdio.h>int ADD_AB(int A, int B);void main(){ int A = 2, B=7, C; C = ADD_AB(A,B); printf("Answer is %d",C);}int ADD_AB(int A, int B){ int x; x = A+B; return x;}
โปรแกรม
ผลการ ทำางาน ?
Answer is 9
Prototype
Function
ชนดของตวแปรในฟงกช นชนดของตวแปรในฟงกช นชนดของตวแปรแยกตามขอบเขตการทำางาน
ตวแปร Gobal เปนตวแปรทเรยกใชจากท ใดกไดในโปรแกรมโดยจะถกประกาศไวหลง
Preprocessor Directive
ตวแปร Local ถกสรางขนภายในฟงกชนการเปลยนแปลงจะมผลภายในฟงกชน
เทานน หากมชอซำากนจะถอวาเปนคนละ ตวแปรกน ตวแปรประเภทน เมอโปรแกรม
ออกจากฟงกชนจะถกทำาลาย
ตวอย าง 10 ตวแปร Gobal
#include <stdio.h>#include <conio.h>int num = 19;void increase(){ num = num + 5;}void main(){ printf("num = %d\n", num); increase(); printf("num = %d\n", num); getch();}
โปรแกรม
ผลการ ทำางาน ?
num = 19num = 19num = 24
Gobal variable
ตวอย าง 11 ตวแปร Local
#include <stdio.h>#include <conio.h>void new_value(){ int x; x = 100;}void main(){ int x = 59; printf("x = %d\n", x); new_value(); printf("x = %d\n", x); getch();}
โปรแกรม
ผลการ ทำางาน ?
num = 59num = 59num = 59
Local variable
Local variable
ฟงกช นมาตรฐาน ฟงกช นมาตรฐาน ((Library Library FunctionFunction))
Library Function เปนฟงกชนสำาเรจรปท มมาใหพรอมกบ Compiler โดยเกบอยใน
ไฟล Header หรอ *.h การใชงานจะตอง#include <file.h> ไวกอน
Library Function ทสำาคญ math.h ฟงกชนการคำานวณทางคณตศาสตร
string.h ฟงกชนสำาหรบอกขระและขอความ
conio.h ฟงกชนจดการหนาจอ
ฟงกช นการค ำานวณทางฟงกช นการค ำานวณทาง คณตศาสตร คณตศาสตร –– math.hmath.h
FunctionFunction DescriptionDescription
sinsin((xx)) sine ของ x, x มหนวยเปนเรเดยนcoscos((xx)) cosine ของ x, x มหนวยเปนเรเดยนtantan((xx)) tangent ของ x, x มหนวยเปนเรเดยนsqrtsqrt((xx)) รากทสองของ x, x>=0
powpow((x,yx,y)) x ยกกำาลง y
loglog((xx)) logarithm ฐาน e ของ x, x>=0
log10log10((xx)) logarithm ฐาน 10 ของ x, x>=0
expexp((xx)) e ยกกำาลง x
absabs((xx)) คาสมบรณของ x, x เปนเลขจำานวนเตมfabsfabs((xx)) คาสมบรณของ x, x เปนเลขจำานวนจรง
ตวอย าง 12 การใช math.h
#include <stdio.h>#include <conio.h>#include <math.h>double x1 = M_PI, x2 = M_PI_4, y;void main(){ y = sin(x1); printf("sin(pi) = %7.4f\n", y); printf("cos(pi/4) = %7.4f\n", cos(x2)); printf("tan(pi/6) = %7.4f\n", tan(M_PI/6)); printf("sqrt(2) = %7.4f\n", sqrt(2)); printf("2.5^3 = %7.4f\n", pow(2.5,3)); printf("loge(e) = %7.4f\n", log(M_E)); printf("log(100) = %7.4f\n", log10(100)); printf("e^3 = %7.4f\n", exp(3)); printf("abs(-7) = %7.4f\n", fabs(-7)); getch();}
โปรแกรม
ผลการ ทำางาน ?
sin(pi) = 0.0000cos(pi/4) = 0.7071tan(pi/6) = 0.5774sqrt(2) = 1.41422.5^3 = 15.6250loge(e) = 1.0000log(100) = 2.0000e^3 = 20.0855abs(-7) = 7.0000
ฟงกช นส ำาหรบข อความ ฟงกช นส ำาหรบข อความ –– string.hstring.h
FunctionFunction DescriptionDescription
strcpystrcpy((str1, str2str1, str2)) คดลอกขอความจาก str2 ไปเกบทstr1
strcatstrcat((str1, str2str1, str2)) ตอขอความใน str1 ดวย str2
strcmpstrcmp((str1, str2str1, str2)) เปรยบเทยบตวอกษรในขอความ (case
sensitive) ถา str1 = str2 จะได 0
strlenstrlen((strstr)) หาความยาวขอความ ฟงกช นส ำาหรบอกขระ ฟงกช นส ำาหรบอกขระ –– ctype.hctype.hFunctionFunction DescriptionDescription
tolowertolower((chch)) เปลยนอกขระเปนตวพมพเลกtouppertoupper((chch)) เปลยนอกขระเปนตวพมพใหญ
ตวอย าง 13 การใช strcpy และ strcat
#include <stdio.h>#include <conio.h>#include <string.h>void main(){ char str1[30] = "Current Date "; char str2[30] = "18 August 2006"; char str3[30]; strcpy(str3, str1); strcat(str1, str2); printf("str1 = %s\n", str1); printf("str3 = %s\n", str3); getch();}
โปรแกรม
ผลการ ทำางาน ?
str1 = Current Date 18 August 2006str3 = Current Date
ตวอย าง 14 การใช strcmp และ strlen
#include <stdio.h>#include <conio.h>#include <string.h>void main(){ char buf1[] = "Nanometer", buf2[] = "Nanometer"; int ptr; printf("buffer 1 : \"%s\"\n",buf1); printf("buffer 2 : \"%s\"\n",buf2); ptr = strcmp(buf2, buf1); if(ptr == 0) printf("buffer 2 is equal to buffer 1\n"); printf("Length of buffer 1 : %d\n", strlen(buf1)); getch();}
โปรแกรม
ผลการ ทำางาน ?
buffer 1 : "Nanometer"buffer 2 : "Nanometer"buffer 2 is equal to buffer 1Length of buffer 1 : 9
ตวอย าง 15 การใช tolower และ toupper
#include <stdio.h>#include <conio.h>#include <string.h>#include <ctype.h>void main(){ int length, i; char *string = "This Is a String"; length = strlen(string); for(i=0; i<length; i++) string[i] = toupper(string[i]); printf("%s\n",string); for(i=0; i<length; i++) string[i] = tolower(string[i]); printf("%s\n",string); getch();}
โปรแกรมผลการ
ทำางาน ?THIS IS A STRINGthis is a string
ฟงกช นจดการหนาจอ ฟงกช นจดการหนาจอ –– conio.hconio.h
FunctionFunction DescriptionDescription
clrscrclrscr()() ลบหนาจอ
clreolclreol()() ลบขอความตงแตตำาแนง cursor จนจบ
บรรทด
gotoxygotoxy((x,yx,y)) ยาย cursor ไปตำาแหนงทตองการ
x : column 1-80 | y : row 1-25
dellinedelline()() ลบบรรทดท cursor อยแลวเลอน
บรรทดลางขน
inslineinsline()() แทรกบรรทดวางเขามากอนบรรทดท
cursor อยgetchgetch()() หยดคอยใหกดคยใดกได 1 ครง
ตวอย าง 16 การใชฟ งกชนจ ดการหนาจอ#include <stdio.h>#include <conio.h>void main(){ char line1[] = "Ferguson wants Rooney for life"; char line2[] = "Henry pleading with Cole to stay"; char line3[] = "Mourinho not giving up on Gallas"; clrscr(); printf("%s\n", line1); printf("%s\n", line2); printf("%s\n", line3); gotoxy(14,1); clreol(); gotoxy(1,2); insline(); getch();}
โปรแกรม
ผลการ ทำางาน ?Ferguson want
Henry pleading with Cole to stayMourinho not giving up on Gallas
ฟงกช น ฟงกช น sizeof(x)sizeof(x)
sizeof(x) หาขนาดพนทในหนวยความจำา ทเกบตวแปร มหนวยเปน byte, x คอตวแปรท
ตองการหาขนาด ไมตองม include ใดๆ เพม#include <stdio.h>#include <conio.h>void main(){ int age = 14; float grade = 4.00; char song[] = "More than word"; printf("size of age = %d bytes\n", sizeof(age)); printf("size of grade = %d bytes\n", sizeof(grade)); printf("size of song = %d bytes\n", sizeof(song)); printf("size of double = %d bytes\n", sizeof(double)); getch();}
ตวอย าง 17 โปรแกรมใช งานฟงก ชน sizeof(x)
ผลการ ทำางาน ?
size of age = 2 bytessize of grade = 4 bytessize of song = 15 bytessize of double = 8 bytes
จบฟงก ช นในภาษาซ
Question ?Question ?