บทที่ 4 ฟังก์ชัน function
DESCRIPTION
บทที่ 4 ฟังก์ชัน Function. วัตถุประสงค์. สามารถเขียนโปรแกรมโดยใช้ฟังก์ชัน สามารถสร้างฟังก์ชันเองได้ สามารถเขียนการรับส่งข้อมูลระหว่างฟังก์ชัน สามารถสร้างและใช้ฟังก์ชันแบบเรียกตัวเอง ( recursion ). เนื้อหาในบทเรียน. ฟังก์ชันคืออะไร ทำไมต้องมีฟังก์ชัน ฟังก์ชันในภาษา C - PowerPoint PPT PresentationTRANSCRIPT
Department of Computer Engineering, PSU
242-101, 241-101 Introduction to Computer Programming
คณาจารย์�ภาควิชาวิศวิกรรมคอมพิวิเตอร�[email protected]
บทท�� บทท�� 44 ฟั�งก�ช�น ฟั�งก�ช�น FunctionFunction
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 2
วิ�ตถุ�ประสงค�วิ�ตถุ�ประสงค�• สามารถุเขี�ย์นโปรแกรมโดย์ใช'
ฟั�งก�ช�น• สามารถุสร'างฟั�งก�ช�นเองได'• สามารถุเขี�ย์นการร�บส)งขี'อม*ล
ระหวิ)างฟั�งก�ช�น• สามารถุสร'างและใช'ฟั�งก�ช�นแบบ
เร�ย์กต�วิเอง (recursion)
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 3
เน-.อหาในบทเร�ย์นเน-.อหาในบทเร�ย์น1 .ฟั�งก�ช�นค-ออะไร2. ท/าไมต'องม�ฟั�งก�ช�น3. ฟั�งก�ช�นในภาษา C4. สร'างฟั�งก�ช�นด'วิย์ตนเอง5. เร�ย์กใช'งานฟั�งก�ช�นได'อย์)างไร6. เร�ย์นร* 'ฟั�งก�ช�นแบบเร�ย์กต�วิเอง7. ขีอบเขีตขีองต�วิแปร
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 4
11 . . ฟั�งก์�ชั�นคื�ออะไรฟั�งก์�ชั�นคื�ออะไร• ฟั�งก์�ชั�น(function) ค-อ ส)วินขีองโปรแกรมท��
ท/างานเสร1จส.นภาย์ในต�วิเอง ม�ล�กษณะเหม-อนก�บโปรแกรมย์)อย์ ท��รวิมอย์*)ในโปรแกรมหล�กอ�กท�หน2�ง
• ใช'หล�กการ Divide and Conquer - แบ)งการท/างานออกเป3นส)วินเล1กๆ ท��ท/างานเสร1จ
สมบ*รณ�ในต�วิ- ทดสอบและแก'ไขีส)วินเล1กๆน�. - ประกอบส)วินเล1กๆน�. ขี2.นมาเป3นโปรแกรมขีนาดใหญ่)ท��
สมบ*รณ�ในขี�.นตอนส�ดท'าย์
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 5
22 . . ท/าไมต'องม�ฟั�งก�ช�นท/าไมต'องม�ฟั�งก�ช�น
Unstructured Unstructured ProgrammingProgrammingUnstructured Unstructured ProgrammingProgramming
โปรแกรมเร�มซั�บซั'อนม�ขีนาดใหญ่) ม�การเปล��ย์นแปลงโครงสร'างขีองโปรแกรม
Procedural Procedural ProgrammingProgramming
Procedural Procedural ProgrammingProgramming
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 6
2.1 Unstructured Programming2.1 Unstructured Programming
ร�ปแบบ : โปรแกรมเขี�ย์นเร�ย์งไปเร-�อย์ๆตามล/าด�บการประมวิลผล
ข้�อเสี�ย : เม-�อโปรแกรมเร�มม�ขีนาดใหญ่) ท/าให'ย์ากต)อการด*แลหร-อปร�บปร�งเปล��ย์นแปลงเช)น
•การหาจ�ดผดพิลาด ท/าได'ย์ากขี2.น, •เกดการใช'ต�วิแปรซั/.าซั'อนได'ง)าย์ขี2.น, ...
statementsdata
statementsdata
Programmain program
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 7
ต�วิอย์)างต�วิอย์)าง 5.1 Unstructured 5.1 Unstructured ProgrammingProgramming
#include <stdio.h>main(){ int first, second, third; printf(“\n F(X) = 3X + 10 if X > 0\n”); printf(“\n F(X) = 10 if X <= 0\n”); printf(“\n Enter 3 values\n”); scanf(“%d %d %d”, &first, &second, &third);if (first > 0) printf(“F(%d) is %d”, first, 3*first + 10);else printf(“F(%d) is 10”, first); if (second > 0) printf(“F(%d) is %d”, second, 3*second + 10);else printf(“F(%d) is 10”, second);if (third > 0) printf(“F(%d) is %d”, third, 3*third + 10);else printf(“F(%d) is 10”, third);}
ค/านวิณ first
ค/านวิณ second
ค/านวิณ third
Code program ซั/.าซั'อน
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 8
แนวคื�ดเพื่��อปร�บปร�ง (แนวคื�ดเร��องฟั�งก์�ชั�น) :ช�ด statements เด�ย์วิก�น อาจม�การใช'งานหลาย์แห)งในโปรแกรม ท/าให'ต'อง copy ช�ด statements น�. หลาย์คร�.งในหน2�งโปรแกรม เกดแนวิคดท��จะรวิบรวิมช�ด statements น�.เขี'าไวิ'ท��เด�ย์วิก�น และม�การเร�ย์กใช'งานเม-�อต'องการเกดเป3น Procedure
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 9
2.2 Procedural Programming2.2 Procedural Programming ((1/21/2))ร�ปแบบ : ช�ด statements สามารถุรวิมไวิ'ในท��ท��หน2�งแย์กจากส)วิน
ขีอง main และม�การใช' procedure call เพิ-�อเร�ย์กใช'งาน ช�ด statements น�.น
ข้�อด� :- ม�การซั)อนราย์ละเอ�ย์ดซั�บซั'อนไวิ'ใน Procedure ท/าให'โปรแกรมหล�กง)าย์ใน
การท/าควิามเขี'าใจ - ลดควิามซั/.าซั'อนขีองส)วินโปรแกรมท��เหม-อนๆก�นไวิ'ใน Procedure เด�ย์วิก�น- การหาขี'อผดพิลาดขีองโปรแกรมง)าย์ขี2.น โดย์แย์กหาระหวิ)างส)วิน main ก�บ
procedures ใดๆ
Program
main programmain programprocedureprocedure
ล/าด�บการท/างานจ�ดท��เร�ย์กใช'งาน
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 10
2.2 Procedural Programming 2.2 Procedural Programming ((2/22/2))ก์ารทำ"างาน main เร�ย์กใช'งาน procedure โดย์อาจม�การส)งค)า
ให'ก�บการเร�ย์กแต)ละคร�.ง เราอาจมองได'วิ)า main เป3นต�วิประสานงานระหวิ)างหลาย์ๆ procedures และเป3นส)วินคอย์ควิบค�มขี'อม*ลหล�ก
สีร�ป Procedural Programming ม�หน2�งโปรแกรมหล�กท��ประกอบไปด'วิย์ส)วินย์)อย์ๆ เร�ย์กวิ)า procedures หร-อฟั�งก�ช�น
main programdata
main programdata
Procedure 1Procedure 1 Procedure 3Procedure 3Procedure 2Procedure 2
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 11
ต�วิอย์)างต�วิอย์)าง 5.2 Procedural 5.2 Procedural ProgrammingProgramming#include <stdio.h>
void get_Fx(int x);main(){ int first, second, third; printf(“\n F(X) = 3X + 10 if X > 0\n”); printf(“\n F(X) = 10 if X <= 0\n”); printf(“\n Enter 3 values\n”); scanf(“%d %d %d”, &first, &second, &third); get_Fx(first); get_Fx(second); get_Fx(third);}
void get_Fx(int x){ if (x > 0) printf(“F(%d) is %d”, x, (3*x) + 10); else printf(“F(%d) is 10”, x);}
•รวิมการท/างานแบบเด�ย์วิก�นไวิ'ด'วิย์ก�น
•เปล��ย์นแปลงต�วิแปร xในการเร�ย์กใช'งานแต)ละคร�.ง
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 12
33 . . ฟั�งก�ช�นในภาษา ฟั�งก�ช�นในภาษา C C ((1/21/2))
ฟั�งก�ช�น หร-อ Procedure ค-อ - ช�ดขีอง statements ท��ท/างานอย์)างใดอย์)าง
หน2�ง และม�ช-�อเร�ย์ก- ส)วินอ-�นขีองโปรแกรมสามารถุเร�ย์กใช'งาน
ฟั�งก�ช�นได'function
ขี'อม*ลinput
ผลล�พิธ์�output
ก์ล่$องด"าเช)น
square rootcomputation
ผลล�พิธ์�ท��ได'ค-อ 4.0ป9อนขี'อม*ล16.0
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 13
33 . . ฟั�งก์�ชั�นในภาษา ฟั�งก์�ชั�นในภาษา C C ((2/22/2))
• โปรแกรมภาษา C ประกอบไปด'วิย์หน2�งฟั�งก�ช�น (main ) หร-อมากกวิ)า
• แต)ละฟั�งก�ช�นประกอบไปด'วิย์หน2�ง statement หร-อมากกวิ)า• ภาษา C แบ)งฟั�งก�ช�นเป3น 2 แบบ
- ฟั�งก�ช�นมาตรฐานใน C- ฟั�งก�ช�นท��สร'างโดย์ผ*'เขี�ย์นโปรแกรม
FUNCTIONS IN CFUNCTIONS IN C
PROGRAMMER DEFINEDFUNCTION
PROGRAMMER DEFINEDFUNCTION
C STANDARDLIBRARY
C STANDARDLIBRARY
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 14
ฟั�งก์�ชั�นมาตรฐานใน ฟั�งก์�ชั�นมาตรฐานใน C C ((C Standard C Standard LibraryLibrary))
• ฟั�งก�ช�นท��ผ*'ผลต C compiler เป3นผ*'เขี�ย์นขี2.น โดย์เก1บไวิ'ใน C library
• ประกอบด'วิย์ฟั�งก�ช�นเก��ย์วิก�บ- disk I/O (input/output), standard I/O ex . printf(),
scanf(), … - string manipulation ex . strlen(), strcpy(), …- mathematics ex . sqrt(), sin(), cos(), …- etc..
• สามารถุเร�ย์กใช'งานได'เลย์ แต)ต'องม�การ include header file ท��นย์ามฟั�งก�ช�นน�.นๆไวิ' เช)น
จะใช' printf(), scanf () ต'องเขี�ย์น #include <stdio.h>จะใช' sqrt(), sin(), cos () ต'องเขี�ย์น #include <math.h> etc.
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 15
ต�วอย$าง ต�วอย$าง 5.35.3 ก์ารใชั�งานฟั�งก์�ชั�นมาตรฐานก์ารใชั�งานฟั�งก์�ชั�นมาตรฐานใน ใน CC
โปรแกรมหาค)ารากท��สอง โดย์ใช'ฟั�งก�ช�น sqrt () ใน math.h
ต'นแบบ(prototype ) : double sqrt(double num)
ให'ค)า num#include <stdio.h>
#include <math.h>
int main()
{ printf("%.2f\n", sqrt(16.0));
printf("%.2f\n", sqrt(sqrt(16.0)));
}
4.00
2.00
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 16
ต�วิอย์)าง ต�วิอย์)าง 5.45.4 การใช'งานฟั�งก�ช�นมาตรฐานใน การใช'งานฟั�งก�ช�นมาตรฐานใน CCโปรแกรมแสดง 10 ย์กก/าล�ง 1 ถุ2ง 5 โดย์เร�ย์กใช'ฟั�งก�ช�น
pow () ใน math.hต'นแบบ double pow(double base, double exp); ให'ค)า baseexp
#include <stdio.h>#include <math.h>int main() { double x=10.0, y =1.0;
do { printf(“%.2f\n”, pow(x,y)); y++; } while (y < 6);}
10.00
100.00
1000.00
10000.00
100000.00
หาราก์ทำ�� 2 => ยก์ก์"าล่�ง 0.5หาราก์ทำ�� 5 => ยก์ก์"าล่�ง ?
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 17
44 . . สร'างฟั�งก�ช�นด'วิย์ตนเองสร'างฟั�งก�ช�นด'วิย์ตนเองการสร'างฟั�งก�ช�นม� 2 ส)วินค-อ
1 .ต�นแบบฟั�งก์�ชั�น (function prototype)
2. น�ยามฟั�งก์�ชั�น (function definition)
ต'นแบบฟั�งก�ช�นจะม�หร-อไม) ขี2.นก�บต/าแหน)งท��เรานย์ามฟั�งก�ช�นในโปรแกรม
- ถุ'าฟั�งก�ช�นนย์ามก)อน main (in-line function) ไม)จ/าเป3นต'องม�ต'นแบบฟั�งก�ช�น
- ถุ'าฟั�งก�ช�นนย์ามหล�ง main จ/าเป3นต'องม�การประกาศต'นแบบฟั�งก�ช�นก)อนฟั�งก�ช�น main
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 18
ต�วิอย์)างต�วิอย์)าง 5.55.5 โครงสร'างโดย์รวิมขีองโครงสร'างโดย์รวิมขีองโปรแกรม โปรแกรม 11
#include <stdio.h>
void func2();void func1();
int main () { func2(); printf("3 "); return 0;}
void func2 () { func1(); printf("2 ");}
void func1 () { printf("1 ");}
ส)วินขีองการ include header file(s)
ส)วินท��ประกาศวิ)าม�ฟั�งก�ช�นอะไรบ'างท��สร'างขี2.นใช'ในโปรแกรมน�.
โปรแกรมหล�ก
ส)วินการนย์ามราย์ละเอ�ย์ดการท/างานขีองฟั�งก�ช�น 2
ส)วินการนย์ามราย์ละเอ�ย์ดการท/างานขีองฟั�งก�ช�น 1
โครงสร'างท��ฟั�งก�ช�นนย์ามหล�งการเร�ย์กใช' จ/าเป3นต'องม� ต'นแบบฟั�งก�ช�น
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 19
ต�วิอย์)างต�วิอย์)าง 5.65.6 โครงสร'างโดย์รวิมขีองโครงสร'างโดย์รวิมขีองโปรแกรม โปรแกรม 22
#include <stdio.h>
void func1 () { printf("1");}
void func2 () { func1(); printf("2");}
int main () { func2(); printf("3"); return 0;}
ส)วินขีองการ include header file(s)
ส)วินการนย์ามราย์ละเอ�ย์ดการท/างานขีองฟั�งก�ช�น 1
ส)วินการนย์ามราย์ละเอ�ย์ดการท/างานขีองฟั�งก�ช�น 2
โปรแกรมหล�กโครงสร'างท��ฟั�งก�ช�นนย์ามก)อนเร�ย์กใช'
ไม)จ/าเป3นต'องม� ต'นแบบฟั�งก�ช�น
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 20
4.1 Function prototype 4.1 Function prototype ((1/31/3))ต'นแบบขีองฟั�งก�ช�นเป3นต�วิบอกให'คอมไพิเลอร�ร* 'ถุ2ง:
1 . ชนดขี'อม*ลท��จะส)งค)ากล�บ (1)2. จ/านวินพิารามเตอร�ท��ฟั�งก�ช�นต'องการ (2)3. ชนดขีองพิารามเตอร�แต)ละต�วิ รวิมท�.งล/าด�บขีอง
พิารามเตอร�เหล)าน�.น
functionขี'อม*ล (2)input
ผลล�พิธ์� (1)output
Note เป3นไปได'ท��ฟั�งก�ช�นจะไม)ม�การส)งค)ากล�บ และ ไม)ต'องการขี'อม*ล input
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 21
• ร*ปแบบ
- return-type หร-อชนดขี'อม*ลท��จะส)งค)ากล�บได'แก) void, int, double, char, …
- function-name ช-�อขีองฟั�งก�ช�น- parameter-list จ/านวินพิารามเตอร�ท��ฟั�งก�ช�นต'องการ
• แต)ละพิารามเตอร�ประกอบด'วิย์ ชนดต�วิแปรและช-�อต�วิแปร
• แต)ละพิารามเตอร�แย์กด'วิย์เคร-�องหมาย์ “ , ”
ต�วอย$าง int add (int a, int b) ;
return-type function-name (parameter-list ) ;
4.1 Function prototype 4.1 Function prototype ((2/32/3))
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 22
• เท�ย์บก�บตอนเป3นกล)องด/า
4.1 Function prototype 4.1 Function prototype ((3/33/3))
functionขี'อม*ลinput
ผลล�พิธ์�output
ก์ล่$องด"า
function-nameparameter-list ผลล�พิธ์�
ชนดเด�ย์วิก�บ return-type
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 23
• นย์ามฟั�งก�ช�นเป3นการเขี�ย์นราย์ละเอ�ย์ดการท/างานขีองฟั�งก�ช�นน�.นๆ
• ประกอบด'วิย์ส)วินขีอง header และ algorithm- header จะม�การเขี�ย์นเหม-อน ต'นแบบฟั�งก�ช�น แต)ไม)ม� ;- algorithm เขี�ย์นอย์*)ภาย์ใน { }
• ร*ปแบบ
ถุ'า return-type ไม)ใช) void ต'องม�การส)งค)ากล�บโดย์ใช'ค/าส��ง return ตามด'วิย์ค)าท��จะส)งกล�บ
return-type function-name (parameter-list) { ราย์ละเอ�ย์ดการท/างาน }
4.24.2 น�ยามฟั�งก์�ชั�น น�ยามฟั�งก์�ชั�น ((function function definitiondefinition))
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 24
• ต�วิอย์)างฟั�งก�ช�นท��ม� return
int add (int a, int b )
{ int result; result = a+b;
return result;
}
• ต�วิอย์)างฟั�งก�ช�นท��ไม)ม� return
void checkresult (int result){ if(result < 0 )
printf(“result is negative”); if(result ==0)
printf(“result is zero”); if(result > 0)
printf(“result is positive”)}
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 25
ต�วิอย์)างต�วิอย์)าง 5.75.7 ช-�อต)างๆเก��ย์วิก�บฟั�งก�ช�นช-�อต)างๆเก��ย์วิก�บฟั�งก�ช�น#include <stdio.h>int square(int x);int main(){ int a=2,b; b = square(a); printf("b = %d \n”,b); return 0;}
int square(int x){ int y; y = x*x; return y;}
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 26
ต�วิอย์)างต�วิอย์)าง5.75.7 ช-�อต)างๆเก��ย์วิก�บฟั�งก�ช�นช-�อต)างๆเก��ย์วิก�บฟั�งก�ช�น#include <stdio.h>int square(int x);int main(){ int a=2,b; b = square(a); printf("b = %d \n”,b); return 0;}
int square(int x){ int y; y = x*x; return y;}
นย์ามฟั�งก�ช�นfunction definition
พิารามเตอร�
อากวิเมนต�
ต'นแบบฟั�งก�ช�นfunction prototypeเร�ย์กใช'งานฟั�งก�ช�นcall function
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 27
55 . . เร�ยก์ใชั�งานฟั�งก์�ชั�นได�อย$างไรเร�ยก์ใชั�งานฟั�งก์�ชั�นได�อย$างไร
BOSS(calling
function)
WORKER(called
function)
1 . สี��งให�ทำ"างาน
3 . รายงานผล่
2 . ทำ"างาน
BOSS อาจเป3นโปรแกรม main หร-อเป3นฟั�งก�ช�นอ-�นWORKER ค-อฟั�งก�ช�นท��ถุ*กเร�ย์กใช'งาน
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 28
5.15.1 ฟั�งก์�ชั�นแบ$งตามก์ารร�บสี$งข้�อม�ล่ฟั�งก์�ชั�นแบ$งตามก์ารร�บสี$งข้�อม�ล่แบบท�� 1 - ฟั�งก�ช�นท��ไม)ร�บผ)านค)า และไม)ส)งผ)านค)ากล�บ
void func1();
แบบท�� 2 - ฟั�งก�ช�นท��ม�การร�บผ)านค)า แต)ไม)ส)งผ)านค)ากล�บ
void func2(int a);
แบบท�� 3 - ฟั�งก�ช�นท��ม�การร�บผ)านค)า และส)งผ)านค)ากล�บ int func3(int a);
แบบท�� 4 - ฟั�งก�ช�นท��ไม)ร�บผ)านค)า แต)ม�การส)งผ)านค)ากล�บ
int func4();
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 29
ต�วอย$าง ต�วอย$าง 5.85.8
• ฟั�งก�ช�นท��ไม)ร�บผ)านค)า และไม)ส)งผ)านค)ากล�บ
#include <stdio.h>void my_print();
void main(){ my_print();}
void my_print(){ printf(“Hello world”);}
main
my_print
1 . เร�ย์ก my_print()2 . พิมพิ�Hello world
3 . เสร1จแล'วิคร�บ
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 30
ต�วิอย์)าง ต�วิอย์)าง 5.95.9
• ฟั�งก�ช�นท��ม�การร�บผ)านค)า แต)ไม)ส)งผ)านค)ากล�บ#include <stdio.h>void my_print(char ch, int x);
void main(){ my_print(‘a’, 5);}
void my_print(char ch, int x){ while (x > 0) { printf(“%c”, ch); x--; }}
1 . เร�ย์ก my_print(‘a’,5 ) ให' ch เท)าก�บ a และให' x ม�ค)า 5
2 . พิมพิ�อ�กษร a จ/านวิน 5 คร�.ง
3 . เสร1จแล'วิคร�บmain
my_print
Note :ฟั�งก�ช�นต'องการพิารามเตอร� 2 ต�วิ ตอนเร�ย์กใช'งานต'องให'อากวิเมนต�ให'ถุ*กต'องตามจ/านวินและชนดท��ต'องการ
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 31
ต�วิอย์)าง ต�วิอย์)าง 5.105.10• ฟั�งก�ช�นท��ม�การร�บผ)านค)า และส)งผ)านค)ากล�บ
1 . เร�ย์ก my_print(5 )ให' x ม�ค)า 5 และให' chมาร�บค)าท��ส)งกล�บ 2 . ร�บค)า
พิมพิ� แล'วิส)งค)ากล�บ
3 . เสร1จแล'วิคร�บผลเก1บในต�วิแปร chmai
nmy_print
#include <stdio.h>
char my_print(int x);
void main(){ char ch; ch = my_print(5); printf(“%c”, ch);}
char my_print(int x){ char lch; scanf(“%c”, &lch); while (x > 0) { printf(“%c”, lch); x--; } return lch;}
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 32
ต�วิอย์)าง ต�วิอย์)าง 5.115.11
• ฟั�งก�ช�นท��ไม)ร�บผ)านค)า แต)ม�การส)งผ)านค)ากล�บ
1 . เร�ย์ก my_function() 2 . ร�บค)า
แล'วิส)งค)ากล�บ
3 . เสร1จแล'วิคร�บค)าท��ส)งกล�บจะพิมพิ�ออกทางหน'าจอ
main
my_print
#include <stdio.h>
int my_function();
void main(){ printf(“%d”, my_function());}
int my_function(){ int a; scanf(“%d”, &a); return a* 5;}
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 33
5.25.2 สีร�ปก์ารเร�ยก์ใชั�งานฟั�งก์�ชั�น สีร�ปก์ารเร�ยก์ใชั�งานฟั�งก์�ชั�น ((1/21/2))
• ฟั�งก�ช�นท��ไม)ม�การส)งค)ากล�บ สามารถุเร�ย์กช-�อฟั�งก�ช�นได'เลย์ โดย์ต'องให'อากวิเมนต�ให'ถุ*กต'องเช)น my_print(); [จาก ex.5.8], my_print(‘a’,5);
[จาก ex.5.9]
ex . 5.8
void my_print(){ printf(“Hello world”);}
ex . 5.9
void my_print(char ch, int x){ while (x > 0) { printf(“%c”, ch); x--; }}
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 34
5.25.2 สีร�ปก์ารเร�ยก์ใชั�งานฟั�งก์�ชั�น สีร�ปก์ารเร�ยก์ใชั�งานฟั�งก์�ชั�น ((2/22/2))• ฟั�งก�ช�นท��ม�การส)งค)ากล�บ ให'มองเสม-อนวิ)าเป3นต�วิแปรต�วิหน2�งท��ม�ชนด
เด�ย์วิก�บชนดขีองขี'อม*ลท��ส)งค)ากล�บ วิธ์�เร�ย์กใช'งาน เอาต�วิแปรมาร�บค)า เช)น ch = my_print(5); [จาก ex.5.10 ] ใช'เป3นส)วินหน2�งในนพิจน� เช)น printf(“%d”, my_function());
[จาก ex.5.11 ] ใช'เป3นอาร�กวิเมนต�ขีองฟั�งก�ช�นอ-�น เช)น ม�ฟั�งก�ช�น int add(int a,
int b);
int x = add(add(1,2),4); ค/านวิณ add(1,2 ) ก)อน แล'วิผลล�พิธ์�ท��ได' ให'เป3นอาร�กวิเมนต�ขีอง add ( … ,4)
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 35
โจทำย�ต�วอย$างโจทำย�ต�วอย$างจงเข้�ยนโปรโตไทำป/ให�ฟั�งก์�ชั�นชั��อ script ทำ��ม�พื่าราม�เตอร� 3 ต�ว 1 จ"านวนชั$องว$างทำ��ต�องก์ารให�แสีดงเม��อเร��มบรรทำ�ด 2 ต�วอ�ก์ษรทำ��ต�องก์ารให�แสีดงหล่�งจาก์ชั$องว$างในพื่าราม�เตอร�แรก์ 3 จ"านวนคืร�0งทำ��ต�องก์ารให�แสีดงต�วอ�ก์ษรในพื่าราม�เตอร�ทำ�� 2 โดยฟั�งก์�ชั�น script ไม$ม�ก์ารสี$งคื$าก์ล่�บ
void script(int space, char c, int time);
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 36
โจทย์�ต�วิอย์)างโจทย์�ต�วิอย์)าง
เขี�ย์นราย์ละเอ�ย์ดการท/างานขีองฟั�งก�ช�น script ในขี'อท��แล'วิ ฟั�งก�ช�น script ม�พิารามเตอร� 3 ต�วิ 1 จ/านวินช)องวิ)างท��ต'องการให'แสดงเม-�อเร�มบรรท�ด 2 ต�วิอ�กษรท��ต'องการให'แสดงหล�งจากช)องวิ)างใน
พิารามเตอร�แรก 3 จ/านวินคร�.งท��ต'องการให'แสดงต�วิอ�กษรใน
พิารามเตอร�ท�� 2 โดย์ฟั�งก�ช�น script ไม)ม�การส)งค)ากล�บ
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 37
โจทย์�ต�วิอย์)างโจทย์�ต�วิอย์)าง
เขี�ย์นราย์ละเอ�ย์ดการท/างานขีองฟั�งก�ช�น script ในขี'อท��แล'วิ
void script(int space, char c, int time )
{ int i;
for(i=0; i< space; i ++)
printf(“ “);
for(i=0; i< time; i++)
printf(“%c”, c);
}
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 38
ต�วอย$าง ต�วอย$าง 5.165.16จงเขี�ย์นโปรแกรมส/าหร�บหาค)าศ�กย์�ไฟัฟั9าซั2�งม�สมการด�งน�.
V = I* R
โดย์ท�� V ค-อ ค)าศ�กย์�ไฟัฟั9า , I ค-อ ค)ากระแสไฟัฟั9า ส)วิน R ค-อ ค)าควิาม
ต'านทาน และท�.งสามค)าน�.เป3นจ/านวินจรง โดย์ก/าหนดให'ส)วินท��ค/านวิณ
ค)า V อย์*)ในฟั�งก�ช�น get_volt ส/าหร�บส)วินท��ร�บค)า I และ R จากผ*'ใช' รวิม
ท�.งส)วินท��แสดงผลล�พิธ์�ขีองค)า V ให'อย์*)ในฟั�งก�ช�น main
main get_voltI, R
V
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 39
#include <stdio.h>
float get_volt(float I,float R);int main(){ float i,r,v; printf(“Enter I : ”); scanf(“%f”, &i); printf(“Enter R : ”); scanf(“%f”, &r); v = get_volt(i,r); printf(“Volt = %f \n”,v); return 0;}
float get_volt(float I,float R){ float V = I*R; return V;}
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 40
ต�วิอย์)าง ต�วิอย์)าง 5.175.17
จงเขี�ย์นโปรแกรมส/าหร�บหาค)า F(x ) ซั2�งม�สมการด�งน�.F(x ) = x2 + 2x +1 ถุ'า x ม�ค)าไม)ต/�ากวิ)า 0
=0 ถุ'า x ม�ค)าต/�ากวิ)า 0 ก/าหนดให'ส)วินท��ใช'ในการค/านวิณค)า F(x ) อย์*)ในฟั�งก�ช�น
get_fx
ส/าหร�บส)วินท��ร�บค)า x จากผ*'ใช' และแสดงผลล�พิธ์�ขีองค)า F(x )อย์*)ใน
ฟั�งก�ช�น main (x และ F(x ) เป3นจ/านวินเต1ม)
xmain get_fx
F(x)
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 41
#include <stdio.h>
int get_fx(int a);
int main(){ int x,fx; printf(“Enter x : ”); scanf(“%d”, &x); fx = get_fx(x); printf(“f(%d) = %d \n”,x,fx); return 0;}int get_fx(int a){ if(a>=0) return (a*a)+(2*a)+1; else return 0;}
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 42
66 . . ฟั�งก์�ชั�นแบบเร�ยก์ต�วเอง ฟั�งก์�ชั�นแบบเร�ยก์ต�วเอง ((recursive functionrecursive function))
ฟั�งก�ช�นท��ม�การเร�ย์กต�วิเองโดย์ให'พิารามเตอร�ท��แตกต)างก�นออกไปเช)น การหา Factorial หร-อการหา Fibonacci
5!
5 *4!
4 *3!
3 *2!
2 *1!
1
n ! = n * (n-1 )! factorial(n ) = n * factorial(n-1)
#include<stdio.h>
int factorial(int x);
int main()
{
int y = factorial(3);
printf("3 ! = %d“, y);
return 0;
}
int factorial(int x)
{
if(x < =1)
return 1;
else
return x * factorial(x-1);
}
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 43
ฟั�งก�ช�นแบบเร�ย์กต�วิเอง ฟั�งก�ช�นแบบเร�ย์กต�วิเอง ((recursive recursive functionfunction))#include<stdio.h>
int factorial(int x);int main(){ int y = factorial(3); printf("3! = %d“, y); return 0;}int factorial(int x){ if(x <= 1) return 1; else return x* factorial(x-1);}
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 44
ข้�อคืวรระว�ง :ฟั�งก�ช�นแบบเร�ย์กต�วิเอง จ/าเป3นจะต'องม� if statement เพิ-�อใช'ในการต�ดสนใจวิ)าฟั�งก�ช�นจะเร�ย์กต�วิเองต)อไป หร-อ หย์�ดเพิ-�อส)งค)ากล�บ
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 45
ต�วอย$าง ต�วอย$าง 5.135.13
• จงเข้�ยนฟั�งก์�ชั�นสี"าหร�บหาคื$า F(x ) ซึ่2�งม�สีมก์ารด�งน�0F(x ) = 0ถ้�า x เทำ$าก์�บ 0
= 1ถ้�า x เทำ$าก์�บ 1 = F(x-1 ) + F(x-2 ) ถ้�า x มาก์ก์ว$า 1• ก์"าหนดให� x แล่ะ F(x ) เป4นจ"านวนเต5ม แล่ะให�ต�0งชั��อ
ฟั�งก์�ชั�นว$า fib
(อน�ก์รม Fibonacci)
• Fibonacci numbers: 1 1 2 3 5 8 13 21 34 55 89 144 233 ...
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 46
int fib(int x)
{
if(x == 0)
return 0;
else if(x == 1)
return 1;
else if(x > 1)
return fib(x-1) + fib(x-2);
}
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 47
77 . . ข้อบเข้ตข้องต�วแปรในเร��องข้องข้อบเข้ตข้องต�วแปรในเร��องข้องฟั�งก์�ชั�นฟั�งก์�ชั�น
ต�วแปรภายใน (local variable) – ต�วิแปรท��ประกาศในบล1อค (ภาย์ในเคร-�องหมาย์ { } ) ขีองฟั�งก�ช�นใดๆ จะร* 'จ�กเฉพิาะในฟั�งก�ช�นน�.นๆ
ต�วแปรภายนอก์ (global variable) - ต�วิแปรท��ประกาศนอกฟั�งก�ช�นใดๆ (รวิมถุ2งฟั�งก�ช�น main ) จะร* 'จ�กในท�กฟั�งก�ช�นท��เขี�ย์นถุ�ดจากการประกาศต�วิแปร
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 48
ต�วิอย์)างต�วิอย์)าง 5.145.14 ขีอบเขีตขีองต�วิแปรภาย์ในขีอบเขีตขีองต�วิแปรภาย์ในฟั�งก�ช�นฟั�งก�ช�น
#include <stdio.h>void my_func();int main(){ double x = 1.1; printf("In main, x = %.2f \n",x); my_func(); printf("In main, x = %.2f \n",x); return 0;}void my_func(){ double x; x = 2.5; printf("In my_func, x = %.2f \n",x);}
ผล่ล่�พื่ธ์�In main, x =1.10In my_func, x
= 2.50In main, x =1.10
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 49
ต�วิอย์)างต�วิอย์)าง 5.155.15 ขีอบเขีตขีองต�วิแปรภาย์นอกขีอบเขีตขีองต�วิแปรภาย์นอกฟั�งก�ช�นฟั�งก�ช�น
#include <stdio.h>double x; void my_func();int main(){ x = 1.1; printf("In main, x = %.2f \n",x); my_func(); printf("In main, x = %.2f \n",x); return 0;}void my_func(){ x = 2.5; printf("In my_func, x =%.2f \n",x);}
ผล่ล่�พื่ธ์�In main, x =1.10In my_func, x = 2.50In main, x =2.50
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 50
#define MAX 950
void one(int anarg, double second)
{
int onelocal;
…
}
#define LIMIT 200
int fun_two(int one, char anarg)
{ int localvar;
…
}
int main(void)
{ int localvar;
…
}
localvar(main)
localvar(fun_two)
anarg(char)
one (parameter)
LIMIT
onelocal
second
anarg(int)
MAX
ร* 'จ�กในmain
ร* 'จ�กในfun_two
ร* 'จ�กในone
ต�วิแปรสร�ปขีอบเขีตขีองสร�ปขีอบเขีตขีองต�วิแปรต�วิแปร
ฟั�งก�ช�น ร* 'จ�กในone
ร* 'จ�กในfun_two
ร* 'จ�กในmain
one (function)
fun_two
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 51
ค/าถุามท'าย์บท ค/าถุามท'าย์บท 1/21/2
1 .เราจะประกาศฟั�งก�ช�นท��ไม)ม�การส)งค)ากล�บได'อย์)างไร2. ฟั�งก�ช�นโปรโตไทป=ค-ออะไร ม�ไวิ'เพิ-�ออะไร3. จากควิามย์าวิ 3 ด'านขีองสามเหล��ย์มใดๆ จงเขี�ย์น
ฟั�งก�ช�นในการหาพิ-.นท��ขีองสามเหล��ย์ม โดย์ม�ส*ตรด�งน�. โดย์
ให'เขี�ย์นโปรแกรมร�บค)าควิามย์าวิท�.ง 3 ด'านขีองสามเหล��ย์ม (a, b, c ) แล'วิค/านวิณพิ-.นท�� (A ) จากน�.นแสดงค)า A, a, b, c ออกทางหน'าจอ
2a + b + c
S =A = s(s-a)(s-b)(s-c)
242-101, 241-101 Introduction to Computer Programming
Department of Computer Engineering, PSU 52
ค/าถุามท'าย์บท ค/าถุามท'าย์บท 2/22/2
4 .เขี�ย์นโปรแกรมร�บต�วิเลขีจ/านวินเต1มเขี'ามา 1 ค)า แล'วิเขี�ย์นฟั�งก�ช�น sum_digit เพิ-�อหาผลรวิมขีองต�วิเลขีแต)ละหล�ก แล'วิแสดงค)าออกทางหน'าจอ เช)น ค)า 443 ค/านวิณ sum_digit (4+4+3 ) ได' 11
5. เขี�ย์นฟั�งก�ช�นในการคดเกรดน�กศ2กษา โดย์ร�บคะแนนเขี'ามาแล'วิค/านวิณเกรดท��ได'ส)งกล�บไปย์�งโปรแกรมหล�ก โดย์ม�เกณฑ์�ด�งน�.90-100 = A, 80-89 = B, 70-79 = C, 60-69 = D, ไม)ถุ2ง 60 = F