89
Department of Computer Engineering, PSU 242-101, 241-101 Introduction to Computer Programming คคคคคคคคคคคคคคค คคคคคคคคคคคคคคคคคคค [email protected]. th คคคคค คคคคค 5 5 คคคคคคคค คคคคคคคค Function Function

บทที่ 5 ฟังก์ชัน Function

  • Upload
    yuval

  • View
    45

  • Download
    1

Embed Size (px)

DESCRIPTION

บทที่ 5 ฟังก์ชัน Function. วัตถุประสงค์. เขียนโปรแกรมเรียกใช้ฟังก์ชันได้ สร้างฟังก์ชันเองได้ เขียนการรับส่งข้อมูลระหว่างฟังก์ชันได้ สร้างและใช้ฟังก์ชันแบบเรียกตัวเอง ( recursion ) ได้. เนื้อหาในบทเรียน. ฟังก์ชันคืออะไร ทำไมต้องมีฟังก์ชัน ฟังก์ชันในภาษา C การสร้างฟังก์ชัน - PowerPoint PPT Presentation

Citation preview

Page 1: บทที่ 5 ฟังก์ชัน Function

Department of Computer Engineering, PSU

242-101, 241-101 Introduction to Computer Programming

คณาจารย์�ภาควิชาวิศวิกรรมคอมพิวิเตอร�[email protected]

บทท�� บทท�� 5 5 ฟั�งก�ช�นฟั�งก�ช�นFunctionFunction

Page 2: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 2

วิ�ตถุ�ประสงค�วิ�ตถุ�ประสงค�• เขี�ย์นโปรแกรมเร�ย์กใช&ฟั�งก�ช�นได้&• สร&างฟั�งก�ช�นเองได้&• เขี�ย์นการร�บส)งขี&อม*ลระหวิ)างฟั�งก�ช�น

ได้&• สร&างและใช&ฟั�งก�ช�นแบบเร�ย์กต�วิเอง

(recursion ) ได้&

Page 3: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 3

เน-.อหาในบทเร�ย์นเน-.อหาในบทเร�ย์น1 .ฟั�งก์�ชั�นคื�ออะไร2. ทำ��ไมต้�องม�ฟั�งก์�ชั�น3. ฟั�งก์�ชั�นในภ�ษ� C4. ก์�รสร��งฟั�งก์�ชั�น5. ก์�รเร�ยก์ใชั�ง�นฟั�งก์�ชั�น6. ฟั�งก์�ชั�นแบบเร�ยก์ต้�วเอง7. ขอบเขต้ของต้�วแปร

Page 4: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 4

เน-.อหาในบทเร�ย์น เน-.อหาในบทเร�ย์น ((ต)อต)อ))8 . ฟั�งก์�ชั�นและอ�ร�เรย�9. ก์�รเร�ยก์ใชั�ฟั�งก์�ชั�นแบบ call-by-value และ

call-by-reference

10. ก์�รส#งคื#�บ�งอ�ล�เมนต้�ของอ�ร�เรย�ให้�ก์�บฟั�งก์�ชั�น1 1 .ก์�รส#งคื#�ทำ%ก์อ�ล�เมนต้�ของอ�ร�เรย�ให้�ก์�บฟั�งก์�ชั�น1 2 .ฟั�งก์�ชั�นทำ�&ร�บพ�ร�ม(เต้อร�เป)นอ�ร�เรย�13.String สต้ร(งห้ร�อส�ยต้�วอ�ก์ษร1 4 .ฟั�งก์�ชั�นทำ�&เก์�&ยวก์�บสต้ร(ง1 5 .อ�ร�เรย�ของสต้ร(ง

Page 5: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 5

11 . . ฟั�งก�ช�นค-ออะไรฟั�งก�ช�นค-ออะไร• ฟั�งก�ช�น(function) ค-อ ส)วินขีองโปรแกรมท��

ท/างานเสร0จส.นภาย์ในต�วิเอง ม�ล�กษณะเหม-อนก�บโปรแกรมย์)อย์ ท��รวิมอย์*)ในโปรแกรมหล�กอ�กท�หน2�ง

• ใช&หล�กการ Divide and Conquer - แบ)งการท/างานออกเป3นส)วินเล0กๆ ท��ท/างานเสร0จ

สมบ*รณ�ในต�วิ- ทด้สอบและแก&ไขีส)วินเล0กๆน�. - ประกอบส)วินเล0กๆน�. ขี2.นมาเป3นโปรแกรมขีนาด้ใหญ่)ท��

สมบ*รณ�ในขี�.นตอนส�ด้ท&าย์

Page 6: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 6

22 . . ท/าไมต&องม�ฟั�งก�ช�นท/าไมต&องม�ฟั�งก�ช�น• เน-�องจากโปรแกรมถุ*กพิ�ฒนาขี2.นเร-�อย์ๆ ม�ควิามซั�บ

ซั&อนและขีนาด้ใหญ่)มากขี2.น• การเขี�ย์นโปรแกรมแบบเด้มท��ไม)ม�โครงสร&าง

(Unstructured Programming ) ม�ควิามย์�)งย์ากในการพิ�ฒนาและการแก&ไขีขี&อผิด้พิลาด้• ม�ส)วินขีองโปรแกรมท��ท/างานล�กษณะเด้�ย์วิก�น

กระจาย์อย์*)หลาย์ท��• เม-�อแก&ไขีส)วินการท/างานน�.นต&องไปแก&ไขีหลาย์

ท��ในโปรแกรมจ2งเกด้ควิามผิด้พิลาด้ซั/.าซั&อนได้&ง)าย์

• การเขี�ย์นโปรแกรมแบบม�โครงสร&าง (Structure Structure ProgrammingProgramming) เหมาะสมในการพิ�ฒนาโปรแกรมท��ม�ควิามซั�บซั&อนมากกวิ)า

• การใช&ฟั�งก�ช�นเป3นล�กษณะการเขี�ย์นโปรแกรมแบบม�โครงสร&างแบบหน2�ง

Page 7: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 7

2.1 Unstructured Programming2.1 Unstructured Programming

ร*ปแบบ : โปรแกรมเขี�ย์นเร�ย์งไปเร-�อย์ๆตามล/าด้�บการประมวิลผิล

ข�อเส�ย : เม-�อโปรแกรมเร�มม�ขีนาด้ใหญ่) ท/าให&ย์ากต)อการด้*แลหร-อปร�บปร�งเปล��ย์นแปลงเช)น

•การหาจ�ด้ผิด้พิลาด้ ท/าได้&ย์ากขี2.น, •เกด้การใช&ต�วิแปรซั/.าซั&อนได้&ง)าย์ขี2.น, ...

statementsdata

statementsdata

Programmain program

Page 8: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 8

ต�วิอย์)างต�วิอย์)าง 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 ซั/.าซั&อน

Page 9: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 9

จ�ก์ต้�วอย#�ง 51.•พิบวิ)าช�ด้ค/าส��งเด้�ย์วิก�น (ส)วินเง-�อนไขีขีอง if-else) ม�การใช&งานหลาย์แห)งในโปรแกรม ท/าให&ต&องส/าเนาช�ด้ค/าส��งน�.หลาย์คร�.งในหน2�งโปรแกรม•เกด้แนวิคด้ท��จะรวิบรวิมช�ด้ค/าส��งน�.เขี&าไวิ&ท��เด้�ย์วิก�น และเร�ย์กใช&งานได้&ตามต&องการ ซั2�งเป3นหล�กการขีอง Structure Programming

Page 10: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 10

2.2 Structural Programming 2.2 Structural Programming ((1/21/2))ร*ปแบบ : น/าช�ด้ค/าส��งบางส)วินไปรวิบรวิมไวิ&ในฟั�งก�ช�น โปรแกรม

หล�ก(main program) ซั2�งเป3นส)วินท��เร�มการท/างานขีองโปรแกรม จะเร�ย์กใช&ช�ด้ค/าส��งในฟั�งก�ช�นน�.นมาท/างาน

ข�อดี� :- ม�การซั)อนราย์ละเอ�ย์ด้ซั�บซั&อนไวิ&ในฟั�งก�ช�น ท/าให&โปรแกรมหล�กง)าย์ในการ

ท/าควิามเขี&าใจ - ลด้ควิามซั/.าซั&อนขีองส)วินโปรแกรมท��เหม-อนๆก�นไวิ&ในฟั�งก�ช�นเด้�ย์วิก�น- การหาขี&อผิด้พิลาด้ขีองโปรแกรมง)าย์ขี2.น โด้ย์แย์กหาระหวิ)างส)วิน main ก�บ

ฟั�งก�ช�นอ-�นๆ

Program

main programmain program functionfunction

ล/าด้�บการท/างานจ�ด้ท��เร�ย์กใช&งาน

Page 11: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 11

2.2 Structure Programming 2.2 Structure Programming ((2/22/2))ก์�รทำ��ง�น main เร�ย์กใช&งานฟั�งก�ช�นโด้ย์อาจม�การส)งค)าให&

ก�บการเร�ย์กแต)ละคร�.ง เราอาจมองได้&วิ)า main เป3นต�วิประสานงานระหวิ)างหลาย์ๆ ฟั�งก�ช�นและเป3นส)วินคอย์ควิบค�มขี&อม*ลหล�ก

สร%ป การโปรแกรมแบบม�โครงสร&าง ประกอบด้&วิย์หน2�งโปรแกรมหล�กและหลาย์ส)วินย์)อย์ท��เร�ย์กวิ)าฟั�งก�ช�น

Main Programdata

Main Programdata

Function 1Function 1 Function 3Function 3Function 2Function 2

Page 12: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 12

ต�วิอย์)างต�วิอย์)าง 5.2 Structure 5.2 Structure 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ในการเร�ย์กใช&งานแต)ละคร�.ง

Page 13: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 13

33 . . ฟั�งก�ช�นในภาษา ฟั�งก�ช�นในภาษา CC• ฟั�งก�ช�น ค-อ ช�ด้ขีองค/าส��งท��ท/างานอย์)างใด้อย์)างหน2�งและม�ช-�อ

เร�ย์ก ซั2�งส)วินอ-�นขีองโปรแกรมสามารถุเร�ย์กใช&งานฟั�งก�ช�นได้&• ในการเร�ย์กใช&ฟั�งก�ช�น ไม)จ/าเป3นต&องทราบวิ)าภาย์ในฟั�งก�ช�น

ท/างานอย์)างไร เปร�ย์บได้&ก�บกล)องด้/าท��มองไม)เป3นภาย์ใน แต)ต&องทราบวิ)าต&องป9อนขี&อม*ลอะไรให& และได้&ร�บผิลล�พิธ์�อะไรกล�บมา

functionขี&อม*ลinput

ผิลล�พิธ์�output

ก์ล#องดี��เช)น

square rootcomputation

ผิลล�พิธ์�ท��ได้&ค-อ 4.0ป9อนขี&อม*ล16.0

Page 14: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 14

33 . . ฟั�งก�ช�นในภาษา ฟั�งก�ช�นในภาษา C C ((ต)อต)อ))

• โปรแกรมภาษา C ประกอบไปด้&วิย์หน2�งฟั�งก�ช�นหร-อมากกวิ)า (ต&องม� main )

• แต)ละฟั�งก�ช�นประกอบไปด้&วิย์หน2�งค/าส��ง (statement) หร-อมากกวิ)า• ภาษา C แบ)งฟั�งก�ช�นเป3น 2 แบบ

- ฟั�งก�ช�นมาตรฐานใน C- ฟั�งก�ช�นท��สร&างโด้ย์ผิ*&เขี�ย์นโปรแกรม

FUNCTIONS IN CFUNCTIONS IN C

PROGRAMMER DEFINEDFUNCTION

PROGRAMMER DEFINEDFUNCTION

C STANDARDLIBRARY

C STANDARDLIBRARY

Page 15: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 15

ฟั�งก�ช�นมาตรฐานใน ฟั�งก�ช�นมาตรฐานใน C C ((C Standard C Standard LibraryLibrary))

• ฟั�งก�ช�นท��ผิ*&ผิลต C compiler เป3นผิ*&เขี�ย์นขี2.น โด้ย์เก0บไวิ&ใน 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.

Page 16: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 16

ต�วิอย์)าง ต�วิอย์)าง 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

Page 17: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 17

ต�วิอย์)าง ต�วิอย์)าง 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 => ยก์ก์��ล�ง ?

Page 18: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 18

44 . . การสร&างฟั�งก�ช�นการสร&างฟั�งก�ช�น• การสร&างฟั�งก�ช�นม� 2 ส)วินค-อ

1 .ต&นแบบฟั�งก�ช�น (function prototype)

2. นย์ามฟั�งก�ช�น (function definition)

• ต&นแบบฟั�งก�ช�นจะม�หร-อไม) ขี2.นก�บต/าแหน)งท��เรานย์ามฟั�งก�ช�นในโปรแกรม

- ถุ&าฟั�งก�ช�นนย์ามก)อน main ไม)จ/าเป3นต&องม�ต&นแบบฟั�งก�ช�น

- ถุ&าฟั�งก�ช�นนย์ามหล�ง main จ/าเป3นต&องม�การประกาศต&นแบบฟั�งก�ช�นก)อนฟั�งก�ช�น main

Page 19: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 19

4.1 Function Prototype4.1 Function Prototype

ต&นแบบขีองฟั�งก�ช�นเป3นต�วิบอกให&คอมไพิเลอร�ร* &ถุ2งส�งเหล)าน�.1 . ชนด้ขี&อม*ลท��จะส)งค)ากล�บ (1)2. จ/านวินพิารามเตอร�ท��ฟั�งก�ช�นต&องการ (2)3. ชนด้ขีองพิารามเตอร�แต)ละต�วิ รวิมท�.งล/าด้�บขีอง

พิารามเตอร�เหล)าน�.น

functionขี&อม*ล (2)input

ผิลล�พิธ์� (1)output

ข�อส�งเก์ต้ ฟั�งก�ช�นอาจจะไม)ม�การส)งค)ากล�บ และ ไม)ต&องการขี&อม*ล input

Page 20: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 20

• ร*ปแบบ

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

Page 21: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 21

• เท�ย์บก�บตอนเป3นกล)องด้/า

4.1 Function prototype 4.1 Function prototype ((ต)อต)อ))

functionขี&อม*ลinput

ผิลล�พิธ์�output

ก์ล#องดี��

function-nameparameter-list ผิลล�พิธ์�

ชนด้เด้�ย์วิก�บ return-type

Page 22: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 22

• นย์ามฟั�งก�ช�นเป3นการเขี�ย์นราย์ละเอ�ย์ด้การท/างานขีองฟั�งก�ช�นน�.นๆ

• ประกอบด้&วิย์ส)วินขีอง header และ algorithm- header จะม�การเขี�ย์นเหม-อน ต&นแบบฟั�งก�ช�น แต)ไม)ม� ;- algorithm เขี�ย์นอย์*)ภาย์ใน { }

• ร*ปแบบ

ถุ&า return-type ไม)ใช) void ต&องม�การส)งค)ากล�บโด้ย์ใช&ค/าส��ง return ตามด้&วิย์ค)าท��จะส)งกล�บ

return-type function-name (parameter-list) { ราย์ละเอ�ย์ด้การท/างาน }

4.24.2 น(ย�มฟั�งก์�ชั�น น(ย�มฟั�งก์�ชั�น ((function definitionfunction definition))

Page 23: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 23

• ต�วิอย์)างฟั�งก�ช�นท��ม� 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”)}

Page 24: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 24

ต�วิอย์)างต�วิอย์)าง 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นต&องม� ต&นแบบฟั�งก�ช�น

Page 25: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 25

ต�วิอย์)างต�วิอย์)าง 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นต&องม� ต&นแบบฟั�งก�ช�น

Page 26: บทที่ 5 ฟังก์ชัน Function

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

Page 27: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 27

ต�วิอย์)างต�วิอย์)าง 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

Page 28: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 28

55 . . การเร�ย์กใช&งานฟั�งก�ช�นการเร�ย์กใช&งานฟั�งก�ช�น

BOSS(calling

function)

WORKER(called

function)

1 . ส�&งให้�ทำ��ง�น

3 . ร�ยง�นผล

2 . ทำ��ง�น

BOSS อาจเป3นโปรแกรม main หร-อเป3นฟั�งก�ช�นอ-�นWORKER ค-อฟั�งก�ช�นท��ถุ*กเร�ย์กใช&งาน

Page 29: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 29

5.15.1 ฟั�งก�ช�นแบ)งตามการร�บส)งขี&อม*ลฟั�งก�ช�นแบ)งตามการร�บส)งขี&อม*ลแบบท�� 1 - ฟั�งก�ช�นท��ไม)ร�บผิ)านค)า และไม)ส)งผิ)านค)ากล�บ

void func1();

แบบท�� 2 - ฟั�งก�ช�นท��ม�การร�บผิ)านค)า แต)ไม)ส)งผิ)านค)ากล�บ

void func2(int a);

แบบท�� 3 - ฟั�งก�ช�นท��ม�การร�บผิ)านค)า และส)งผิ)านค)ากล�บ int func3(int a);

แบบท�� 4 - ฟั�งก�ช�นท��ไม)ร�บผิ)านค)า แต)ม�การส)งผิ)านค)ากล�บ

int func4();

Page 30: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 30

ต�วิอย์)าง ต�วิอย์)าง 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 . เสร0จแล&วิคร�บ

Page 31: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 31

ต�วิอย์)าง ต�วิอย์)าง 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 . เสร0จแล&วิคร�บmain

my_print

Note :ฟั�งก�ช�นต&องการพิารามเตอร� 2 ต�วิ ตอนเร�ย์กใช&งานต&องให&อากวิเมนต�ให&ถุ*กต&องตามจ/านวินและชนด้ท��ต&องการ

Page 32: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 32

ต�วิอย์)าง ต�วิอย์)าง 5.105.10• ฟั�งก�ช�นท��ม�การร�บผิ)านค)า และส)งผิ)านค)ากล�บ

1 . เร�ย์ก my_print(5 )ให& x ม�ค)า 5 และให& chมาร�บค)าท��ส)งกล�บ 2 . ร�บค)า

พิมพิ� แล&วิส)งค)ากล�บ

3 . เสร0จแล&วิคร�บผิลเก0บในต�วิแปร 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;}

Page 33: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 33

ต�วิอย์)าง ต�วิอย์)าง 5.115.11

• ฟั�งก�ช�นท��ไม)ร�บผิ)านค)า แต)ม�การส)งผิ)านค)ากล�บ

1 . เร�ย์ก my_function() 2 . ร�บค)า

แล&วิส)งค)ากล�บ

3 . เสร0จแล&วิคร�บค)าท��ส)งกล�บจะพิมพิ�ออกทางหน&าจอ

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

Page 34: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 34

5.25.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--; }}

Page 35: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 35

5.25.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)

Page 36: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 36

โจทำย�ต้�วอย#�งโจทำย�ต้�วอย#�งจงเข�ยนโปรโต้ไทำป/ให้�ฟั�งก์�ชั�นชั�&อ script ทำ�&ม�พ�ร�ม(เต้อร� 3 ต้�ว 1 จ��นวนชั#องว#�งทำ�&ต้�องก์�รให้�แสดีงเม�&อเร(&มบรรทำ�ดี 2 ต้�วอ�ก์ษรทำ�&ต้�องก์�รให้�แสดีงห้ล�งจ�ก์ชั#องว#�งในพ�ร�ม(เต้อร�แรก์ 3 จ��นวนคืร�0งทำ�&ต้�องก์�รให้�แสดีงต้�วอ�ก์ษรในพ�ร�ม(เต้อร�ทำ�& 2 โดียฟั�งก์�ชั�น script ไม#ม�ก์�รส#งคื#�ก์ล�บ

void script(int space, char c, int time);

Page 37: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 37

โจทย์�ต�วิอย์)าง โจทย์�ต�วิอย์)าง ((ต)อต)อ))

• เขี�ย์นราย์ละเอ�ย์ด้การท/างานขีองฟั�งก�ช�น script- ฟั�งก�ช�น script ม�พิารามเตอร� 3 ต�วิ

1 จ/านวินช)องวิ)างท��ต&องการให&แสด้งเม-�อเร�มบรรท�ด้ 2 ต�วิอ�กษรท��ต&องการให&แสด้งหล�งจากช)องวิ)างใน

พิารามเตอร�แรก 3 จ/านวินคร�.งท��ต&องการให&แสด้งต�วิอ�กษรใน

พิารามเตอร�ท�� 2 โด้ย์ฟั�งก�ช�น script ไม)ม�การส)งค)ากล�บ

Page 38: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 38

โจทย์�ต�วิอย์)าง โจทย์�ต�วิอย์)าง ((ต)อต)อ))

• เขี�ย์นราย์ละเอ�ย์ด้การท/างานขีองฟั�งก�ช�น 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);

}

Page 39: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 39

ต้�วอย#�ง ต้�วอย#�ง 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

Page 40: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 40

#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;}

Page 41: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 41

ต�วิอย์)าง ต�วิอย์)าง 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นจ/านวินเต0ม)

xmain get_fx

F(x)

Page 42: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 42

#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;}

Page 43: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 43

66 . . ฟั�งก�ช�นแบบเร�ย์กต�วิเอง ฟั�งก�ช�นแบบเร�ย์กต�วิเอง ((recursive recursive functionfunction))

ฟั�งก�ช�นท��ม�การเร�ย์กต�วิเองโด้ย์ให&พิารามเตอร�ท��แตกต)างก�นออกไปเช)น การหา 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);

}

Page 44: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 44

ฟั�งก�ช�นหาค)าแฟัคตอเร�ย์ลแบบเร�ย์กต�วิเองฟั�งก�ช�นหาค)าแฟัคตอเร�ย์ลแบบเร�ย์กต�วิเอง#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);}

Page 45: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 45

ข�อคืวรระว�ง :ฟั�งก�ช�นแบบเร�ย์กต�วิเอง จ/าเป3นจะต&องม� if statement เพิ-�อใช&ในการต�ด้สนใจวิ)าฟั�งก�ช�น จะเร�ย์กต�วิเองต)อไป หร-อ หย์�ด้เพิ-�อส)งค)ากล�บ

Page 46: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 46

ต�วิอย์)าง ต�วิอย์)าง 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ม และให้�ต้�0งชั�&อ

ฟั�งก์�ชั�นว#� fib

(อน%ก์รม Fibonacci)

• Fibonacci numbers: 1 1 2 3 5 8 13 21 34 55 89 144 233 ...

Page 47: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 47

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

}

Page 48: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 48

77 . . ขีอบเขีตขีองต�วิแปรขีอบเขีตขีองต�วิแปร

ต้�วแปรภ�ยใน (local variable) – ต�วิแปรท��ประกาศในบล0อค (ภาย์ในเคร-�องหมาย์ { } ) ขีองฟั�งก�ช�นใด้ๆ จะร* &จ�กเฉพิาะในฟั�งก�ช�นน�.นๆ

ต้�วแปรภ�ยนอก์ (global variable) - ต�วิแปรท��ประกาศนอกฟั�งก�ช�นใด้ๆ (รวิมถุ2งฟั�งก�ช�น main ) จะร* &จ�กในท�กฟั�งก�ช�นท��เขี�ย์นถุ�ด้จากการประกาศต�วิแปร

Page 49: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 49

ต�วิอย์)างต�วิอย์)าง 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

Page 50: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 50

ต�วิอย์)างต�วิอย์)าง 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

Page 51: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 51

#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

Page 52: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 52

แบบฝึ?กห�ด้ฟั�งก�ช�นแบบฝึ?กห�ด้ฟั�งก�ช�น

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)

Page 53: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 53

แบบฝึ?กห�ด้ฟั�งก�ช�น แบบฝึ?กห�ด้ฟั�งก�ช�น ((ต)อต)อ))

4 .เขี�ย์นโปรแกรมร�บต�วิเลขีจ/านวินเต0มเขี&ามา 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

Page 54: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 54

ฟั�งก์�ชั�นและอ�ร�เรย�ฟั�งก์�ชั�นและอ�ร�เรย�การส)งผิ)านขี&อม*ลขีองอาร�เรย์�ไปย์�งฟั�งก�ช�นสามารถุ

แบ)งได้&เป3น 2 ล�กษณะค-อ• การส)งผิ)านค)าอ�ล�เมนต�อาร�เรย์�ให&ก�บฟั�งก�ช�น

เป3นการเร�ย์กใช&ฟั�งก�ช�นแบบ Call-by-value• การส)งอาร�เรย์�ท�กอ�ล�เมนต�ให&ก�บฟั�งก�ช�น เป3นการ

เร�ย์กใช&ฟั�งก�ช�นแบบ Call-by-reference

Page 55: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 55

การเร�ย์กใช&แบบ การเร�ย์กใช&แบบ Call-by-valueCall-by-value• ใช&วิธ์�การส)งค)าขีองต�วิแปร (value) ให&ก�บฟั�งก�ช�น โด้ย์

ผิ)านพิารามเตอร�• ไม)สามารถุแก&ไขีค)าขีองอาร�กวิเมนต�(หร-อพิารามเตอร� ) ภาย์ใน

ฟั�งก�ช�นได้& = การแก&ไขีค)าต)างๆในฟั�งก�ช�น ไม)ม�ผิลต)อต�วิแปรท��ส)งค)ามา

• ใช&ก�บฟั�งก�ช�นท��ประกาศร�บพิารามเตอร�เป3นต�วิแปรธ์รรมด้า (int, float, char,...) ท��ไม)ใช)อาร�เรย์�

• เช)น void triple(int x) { x=x*3; printf(“x = %d”,x); } ….

int x=5, y[2]={10,11}; triple(x); triple(y[0]);

triple(x) ส#งคื#� 5 ให้�ก์�บฟั�งก์�ชั�น ในฟั�งก์�ชั�น x เร(&มต้�นเป)น 5และถ้*ก์ทำ��ให้�ก์ล�ยเป)น 15 ห้ล�งจบฟั�งก์�ชั�น คื#� x นอก์ฟั�งก์�ชั�น ไม#เปล�&ยนแปลงtriple(y[0]) ส#งคื#� 10 ให้�ก์�บฟั�งก์�ชั�น ในฟั�งก์�ชั�น x เร(&มต้�นเป)น 10และถ้*ก์ทำ��ให้�ก์ล�ยเป)น 30 ห้ล�งจบฟั�งก์�ชั�น คื#� y[0] ย�งเห้ม�อนเดี(ม

Page 56: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 56

• ใช&วิธ์�การส)งค)า แอด้เด้รส (Address)*** ขีองต�วิแปรไปให&ฟั�งก�ช�น

• ใช&ก�บฟั�งก�ช�นท��ร �บค)าเขี&า(พิารามเตอร� ) เป3น อ�ร�เรย�• สามารถุแก&ไขีค)าขีองอาร�กวิเมนต�ภาย์ในฟั�งก�ช�นได้& = การ

แก&ไขีค)าต�วิแปรอาร�เรย์� ภาย์ในฟั�งก�ช�น ม�ผิลการเปล��ย์นแปลงต)อต�วิแปรท��ส)งค)ามาเพิราะ การม�การจ�ด้การค)าขีองหน)วิย์ควิามจ/าในต/าแหน)งเด้�ย์วิก�น***แอด้เด้รส (Address) ค-อ ค)าท��ใช&อ&างถุ2งต�วิขี&อม*ล

ภาย์ในหน)วิย์ควิามจ/า เหม-อนก�บหมาย์เลขีบ&านเลขีท��***

การเร�ย์กใช&แบบ การเร�ย์กใช&แบบ Call-by-referenceCall-by-reference

Page 57: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 57

ฟั�งก�ช�นท��ม�การร�บค)าเขี&าเป3นอาร�เรย์�ฟั�งก�ช�นท��ม�การร�บค)าเขี&าเป3นอาร�เรย์�• ฟั�งก�ช�นสามารถุท��จะร�บค)าเขี&าเป3นอาร�เรย์�ได้& ซั2�งร*ป

แบบขีองการเขี�ย์นต&นแบบขีองฟั�งก�ช�นเป3นด้�งน�.ชนด้ขี&อม*ล ช-�อฟั�งก�ช�น(ชนด้ขี&อม*ล ช-�อต�วิแปร[ขน�ดีอ�ร�เรย�] );

• ในกรณ�ฟั�งก�ช�นม�การร�บค)าเขี&าเป3นอาร�เรย์� 1 มต อาจจะไม)ต&องก/าหนด้ขีนาด้ขีองอาร�เรย์�ก0ได้&

• ต�วิอย์)างเช)นint sum_arr(int num[10]);

void print_arr(int a[5]);

float average(int num[]);

Page 58: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 58

การส)งผิ)านค)าอ�ล�เมนต�อาร�เรย์�ให&ก�บฟั�งก�ช�นการส)งผิ)านค)าอ�ล�เมนต�อาร�เรย์�ให&ก�บฟั�งก�ช�น

• หากฟั�งก�ช�น my_func ม�ต&นแบบขีองฟั�งก�ช�นด้�งน�.void my_func(int x);

• และใน main ได้&ม�การประกาศต�วิแปรอาร�เรย์�ช-�อวิ)า num

int num[10];• การส)งอ�ล�เมนต�ท�� 0 ขีองอาร�เรย์� num ไปเป3น

อาร�กวิเมนต�ขีองฟั�งก�ช�น my_func สามารถุเขี�ย์นได้&ด้�งน�.my_func(num[0]);

Page 59: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 59

ต�วิอย์)างการส)งค)าแต)ละอ�ล�เมนต�ในอาร�เรย์�ให&ต�วิอย์)างการส)งค)าแต)ละอ�ล�เมนต�ในอาร�เรย์�ให&ก�บฟั�งก�ช�นก�บฟั�งก�ช�น

• ไฟัล� arrayex6.c#include <stdio.h>void check_val(int x);int main(){ int i,a[3] = {2,-1,5}; for(i=0;i<3;i++) check_val( a[i] ); return 0;}void check_val(int x){ if(x >= 0)

printf(“%d : Positive\n”,x); else printf(”%d: Negative\n”,x);}

2 : Positive-1 : Negative5 : Positive

Page 60: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 60

ต�วิอย์)างการส)งค)าบางอ�ล�เมนต�ขีองอาร�เรย์�ให&ต�วิอย์)างการส)งค)าบางอ�ล�เมนต�ขีองอาร�เรย์�ให&ก�บฟั�งก�ช�นก�บฟั�งก�ช�น

• ไฟัล� arrayex7.c#include <stdio.h>void showVal(int val); /* function prototype */void main(){

int nums[5] = {2, 18, 1, 27, 16};showVal(nums[0]);

}

void showVal(int val) /* show 1 value */{

printf("Value is %d\n", val);}

Value is 2

Page 61: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 61

การส)งอาร�เรย์�ท�กอ�ล�เมนต�ขีองอาร�เรย์�ให&ก�บการส)งอาร�เรย์�ท�กอ�ล�เมนต�ขีองอาร�เรย์�ให&ก�บฟั�งก�ช�นฟั�งก�ช�น

• การส)งอาร�เรย์�ในกรณ�น�. ใช&แค)ช-�อต�วิแปรอาร�เรย์�เท)าน�.น เช)น หากใน main ม�การประกาศอาร�เรย์�ด้�งน�.

10int num[ ];• และฟั�งก�ช�น print_array ม�ต&นแบบฟั�งก�ช�นด้�งน�.

void print_array(int a[10]);• การส)งอาร�เรย์� num ท�กอ�ล�เมนต�ไปให&ฟั�งก�ช�น

print_array สามารถุเขี�ย์นก์�รเร�ยก์ใชั�ฟั�งก์�ชั�นได้&ด้�งน�.print_array(num);

Page 62: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 62

ต�วิอย์)างการส)งอาร�เรย์�ท�กอ�ล�เมนต�ให&ก�บต�วิอย์)างการส)งอาร�เรย์�ท�กอ�ล�เมนต�ให&ก�บฟั�งก�ช�นฟั�งก�ช�น

• ไฟัล� arrayex8.c#include <stdio.h>void print_array(int a[4]);int main() { int num[4] = {5,2,-1,8}; print_array(num); return 0;}

void print_array(int a[4]){ int i; for (i =0; i<4; i++ ) printf(“%d”, a[i]); }

5 2 -1 8

Page 63: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 63

ต�วิอย์)าง การส)งอาร�เรย์�ท�กอ�ล�เมนต�ให&ก�บต�วิอย์)าง การส)งอาร�เรย์�ท�กอ�ล�เมนต�ให&ก�บฟั�งก�ช�นฟั�งก�ช�น

• ไฟัล� arrayex9.c#include <stdio.h>void find_max(int vals[5]); /* function prototype */void main(){

int nums[5] = {2, 18, 1, 27, 16};find_max(nums);

}

void find_max(int vals[5]) /* find the maximum value */{

int i, max = vals[0];for ( i=1; i < 5; ++i) if (max < vals[i]) max = vals[i];

printf(”The maximum value is %d\n” , max);}

The maximum value is 27

Page 64: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 64

โจทย์�ฝึ?กสมองโจทย์�ฝึ?กสมอง 1 1• จงเข�ยนโปรแก์รมทำ�&ร�บจ��นวนเต้4มจ�ก์ผ*�ใชั�

10 ต้�ว และจ��นวนเต้4มบวก์ N จ�ก์น�0นให้�น�บว#�ในจ��นวนเต้4มทำ�0ง 10 ต้�วน�0 ม�ต้�วประก์อบของ N อย*#ก์�&ต้�ว ก์��ห้นดีให้�ส#วนทำ�&ร�บคื#�จ�ก์ผ*�ใชั�และส#วนทำ�&แสดีงผลล�พธ์�อย*#ใน main และให้�ส#วนทำ�&น�บต้�วประก์อบอย*#ในฟั�งก์�ชั�นชั�&อ count_factor

main count_factorอ�ร�เรย�ของจ��นวนเต้4ม

ผลก์�รน�บต้�วประก์อบ

Page 65: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 65

#include <stdio.h>int count_factor(int n, int x[10]); int main(){ int i, num[10],num_factor, N; printf(“N = ”); scanf(“%d”,&N); for(i=0;i<10;++i) { printf(”Enter integer %d: ”,i+1); scanf(”%d”,&num[i]); } num_factor = count_factor(N, num); printf(”Found factor of %d : %d”, N, num_factor); return 0;}int count_factor(int n, int x[10]){ int i,count = 0; for(i=0; i<10; ++i) { if ( n%x[i]==0 ) count++; } return count;}

N = 40Enter integer 1: 2Enter integer 2: 15Enter integer 3: 20Enter integer 4: 10Enter integer 5: 3Enter integer 6: 1Enter integer 7: 5Enter integer 8: 4Enter integer 9: 25Enter integer 10: 30Found factor of 40 : 6

Page 66: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 66

โจทย์�ฝึ?กสมอง โจทย์�ฝึ?กสมอง 22• จงเข�ยนโปรแก์รมส��ห้ร�บร�บคื#�คืะแนนของ

น�ก์ศึ2ก์ษ�จ��นวน N คืน (N<100) และให้�พ(มพ�คื#�เฉล�&ยของคืะแนนทำ�0งห้มดี โดียก์��ห้นดีให้�ส#วนร�บคืะแนนจ�ก์ผ*�ใชั�และส#วนทำ�&แสดีงคื#�เฉล�&ยอย*#ใน main ส��ห้ร�บส#วนทำ�&คื��นวณคื#�เฉล�&ยให้�อย*#ฟั�งก์�ชั�นชั�&อ average โดียให้�ส#งอ�ร�เรย�ทำ�&เก์4บคืะแนนจ�ก์ main ม�ให้�ฟั�งก์�ชั�น average (คืะแนนส�ม�รถ้เป)นทำศึน(ยมไดี�)

main averageอ�ร�เรย�ของคืะแนน

คื#�เฉล�&ย

Page 67: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 67

#include <stdio.h>#define MAX 100float average(float s[MAX],int N); int main(){ int i,N; float score[10],avg_score; printf(“Number of students N = ”); scanf(“%d”,&N); for(i=0;i<N;++i) { printf(”Enter score %d : ”,i+1); scanf(“%f”,&score[i]); } avg_score = average(score, N); printf("Average score is %.2f\n",avg_score); return 0;}float average(float s[MAX], int N){ float total=0.0, avg; int i; for(i=0; i<N; i++) total+= s[i]; avg = total/N; return avg;}

Number of students N = 10Enter score 1: 10Enter score 2: 20Enter score 3: 30Enter score 4: 40Enter score 5: 50Enter score 6: 60Enter score 7: 70Enter score 8: 80Enter score 9: 90Enter score 10: 100Average score is : 55.00

Page 68: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 68

การแก&ไขีค)าขีองอาร�เรย์�ภาย์ในฟั�งก�ช�นการแก&ไขีค)าขีองอาร�เรย์�ภาย์ในฟั�งก�ช�น

ฟั�งก�ช�นท��ม�การร�บพิารามเตอร�เป3นอาร�เรย์� หากม�การแก&ไขีค)าภาย์ในอาร�เรย์�ด้�งกล)าวิ จะท/าให&อาร�เรย์�ท��ถุ*กส)งมาเป3นอาร�กวิเมนต� น�.น ม�การเปล��ย์นแปลงด้&วิย์

Call-by–reference การส)ง ช-�อขีองอาร�เรย์�ให&ก�บฟั�งก�ช�น เป3นการส)งต/าแหน)งเร�มต&นขีองต�วิแปรอาร�เรย์� ท/าให&ฟั�งก�ช�นสามารถุเขี&าถุ2งขี&อม*ลในอาร�เรย์�ได้&โด้ย์ตรง และเขี&าไปแก&ไขีขี&อม*ลขีองอาร�เรย์�น�.นได้&

Page 69: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 69

ต�วิอย์)างการแก&ไขีขี&อม*ลอาร�เรย์�ในฟั�งก�ช�นต�วิอย์)างการแก&ไขีขี&อม*ลอาร�เรย์�ในฟั�งก�ช�น

• ไฟัล� arrayex10.c#include <stdio.h>void edit_arr(int a[4]);int main() { int i,num[4] = {2,5}; printf(“Before: ”); for(i=0;i<4;i++) printf(”%d ”,num[i]); printf(“\nAfter: ”); edit_arr(num); for(i=0;i<4;i++) printf(”%d ”,num[i]); return 0;}void edit_arr(int a[4]) { int i; for(i=0;i<4;i++) a[i] = a[i]*a[i]+1;}

Before: 2 5 0 0 After: 5 26 1 1

Page 70: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 70

ต�วิอย์)างฟั�งก�ช�นร�บอาร�เรย์�สองมตต�วิอย์)างฟั�งก�ช�นร�บอาร�เรย์�สองมต (Matrix)(Matrix)• เมตรกซั�

#include <stdio.h>#define MX 10void inputMatrix(int m[MX][MX], int row, int col){ int i,j; for(i=0; i<row; i++) for(j=0; j<col; j++) { printf(“Matrix [%d][%d] : ”,i+1,j+1); scanf(“%d”,&m[i][j]); }}int sumMatrix(int m[MX][MX], int row, int col){ int i,j,sum=0; for(i=0; i<row; i++) for(j=0; j<col; j++) sum+=m[i][j]; return sum; }

Page 71: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU

ต�วิอย์)างฟั�งก�ช�นร�บอาร�เรย์�สองมตต�วิอย์)างฟั�งก�ช�นร�บอาร�เรย์�สองมต (Matrix)(Matrix)void showMatrix(int m[MX][MX], int row, int col)

{ int i,j; for(i=0; i<row; i++) { for(j=0; j<col; j++) printf(“%d ”,m[i][j]); printf(“\n”); }}

int main(){

int mat[MX][MX]={0},r, c;

printf(“Matrix dimension: ”);

scanf(“%d %d”,&r,&c); showMatrix(mat,r,c);

inputMatrix(mat,r,c); showMatrix(mat,r,c); printf(“Sum all elements = %d\n”, sumMatrix(mat,r,c));

return 0;

}

71

Matrix dimension : 2 30 0 00 0 0Matrix[1][1] : 1Matrix[1][2] : 2Matrix[1][3] : 3Matrix[2][1] : 4Matrix[2][2] : 5Matrix[2][3] : 61 2 34 5 6Sum all elements = 21

Page 72: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 72

สาย์ต�วิอ�กษร สาย์ต�วิอ�กษร ((String)String)

• สตรง หร-อ สาย์ต�วิอ�กษร ค-อ ขี&อม*ลท��ประกอบไปด้&วิย์ค)าคงท��ชนด้ต�วิอ�กษรเร�ย์งต)อเน-�องก�นไป โด้ย์ม�จ�ด้ส.นส�ด้ท��ต�วิอ�กษร Null Character (‘\0’) ซั2�งม�ค)ารห�สเป3น 0 เป3นต�วิบ)งบอกจ�ด้ส.นส�ด้ขีองstring (หร-อเร�ย์กวิ)า end character)

• การเก0บขี&อม*ลสตรง ใช&ต�วิแปรชนด้ อาร�เรย์�ขีองต�วิอ�กษร จ2งจ�ด้เป3นโครงสร&างขีองอาร�เรย์� 1 มต

char stringVariable[ length_of_string ]

Page 73: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 73

การประกาศต�วิแปรชนด้สตรงการประกาศต�วิแปรชนด้สตรงท/าได้&เช)นเด้�ย์วิก�บการประกาศต�วิแปรอาร�เรย์� 1 มต

ในการก/าหนด้ต�วิสตรงและม�การก/าหนด้ค)าเร�มต&นไปพิร&อมก�นน�.น

อาจไม)ต&องใส)จ/านวินขี&อม*ลภาย์ในวิงเล0บบอกจ/านวินสมาชกก0ได้& เช)น

char str1[] ={‘A’, ‘B’, ‘C’, ‘D’, ‘\0’};char str2[10]={‘A’, ‘B’, ‘C’, ‘D’, ‘\0’};char str3[] = ”ABCD”;

ต�วิอย์)างการประกาศท��ผิด้char str4[2] ={‘A’, ‘B’, ‘C’, ‘D’, ‘\0’};

Page 74: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 74

ต�วิอย์)างขีองสตรงต�วิอย์)างขีองสตรง

char a[7] = “Hello!”;

จากต�วิอย์)าง “Hello!” ค-อ ค)าคงท��สตรง ซั2�งจะเขี�ย์นล&อมไวิ&ด้&วิย์ เคร-�องหมาย์ ” ค)าคงท�� “Hello!” ถุ*กก/าหนด้ให&เป3นค)าเร�มต&นแก)ต�วิแปร a ซั2�งเป3นอาร�เรย์�ขีนาด้ 7 หน)วิย์ หล�งจากการก/าหนด้ค)าแล&วิ ในต�วิแปร a จะ ม�ล�กษณะ ด้�งน�.

‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘!’‘\0’

a[0]

a[1]

a[2]

a[3]

a[5]

a[4]

a[6]

Page 75: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 75

• คอมไพิเลอร�จะก/าหนด้จ/านวินพิ-.นท��ต�วิอ�กษรจากจ/านวินต�วิอ�กษรในค)าคงท��ขีองสตรงรวิมก�บ ‘\0’

• หร-ออาจก/าหนด้พิ-.นท��ต�วิอ�กษรให&มากกวิ)าควิามย์าวิขีองสตรงท��ใช&ก/าหนด้เป3นค)าเร�มต&นได้& เช)น

char a[10] = “Hello!”;

• ม�การเก0บขี&อม*ล ด้�งร*ป

ต�วิอย์)างขีองสตรงต�วิอย์)างขีองสตรง

‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘!’‘\0’

a[0]

a[1]

a[2]

a[3]

a[5]

a[4]

a[6] 0 0 0

a[7]

a[8]

a[9]

• a[6] = 0 หร-อ ‘\0’ , a[6] เป3น end character ขีองสตรง ส)วิน a[7] = a[8] = a[9] = 0 ถุ*กเซั0ตเป3นศ*นย์�อ�ตโนม�ต เพิราะ เป3น element ส)วินท��เหล-อ ท��ย์�งไม)ได้&การก/าหนด้ค)าเร�มต&นให&

Page 76: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 76

ต�วิอย์)างต�วิอย์)าง : : การแสด้งค)าขีองสตรงการแสด้งค)าขีองสตรง

• ใช& %s ใน printf

• ไฟัล� arrayex17.c#include <stdio.h>int main(){ char name[5] = {'J','o','h','n','\0'} ; char surname[8] = {'F','\0','K','E','N'}; printf(“Name: %s \n",name); printf(“Surname: %s \n",surname); printf(“String constant: %s \n",”any text”); return 0;}

Name: JohnSurname: FString constant: any text

Page 77: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 77

ต�วิอย์)างท�� ต�วิอย์)างท�� : : การร�บค)าขีองสตรงการร�บค)าขีองสตรง• ใช& %s ใน scanf (ร�บขี&อควิาม ไม)รวิม space

หร-อ newline )

• ไฟัล� arrayex18.c#include <stdio.h>int main() { char n[10],s[10]; printf(”Enter name: “); scanf(“%s”,&n); // or scanf("%s",n);

printf(“Enter surname : “); scanf(“%s” ,&s); // or scanf("%s",s);

printf(“Hello %s %s\n” , n, s); return 0;}

Enter name: John F.Enter surname: KennedyHello John Kennedy

Page 78: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 78

ต�วิอย์)างท�� ต�วิอย์)างท�� : : การร�บค)าขีองสตรงการร�บค)าขีองสตรง ท�ละบรรท�ด้ท�ละบรรท�ด้• ใช&ฟั�งก�ช�น gets (ร�บขี&อควิามท�.งบรรท�ด้ รวิม

ช)องวิ)าง หร-อ space )

• ไฟัล� arrayex18.c#include <stdio.h>int main() { char n[20],c[20]; printf(”Enter name and surname: “); gets(n);

printf(“City : ”); gets(c);

printf(“Hello %s %s\n” , n, c); return 0;}

Enter name and surname: John F. KennedyCity : Washington D.C. Hello John F. Kennedy Washington D.C.

Page 79: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 79

การก/าหนด้ค)าให&ก�บต�วิแปรชนด้สตรงการก/าหนด้ค)าให&ก�บต�วิแปรชนด้สตรง

การก/าหนด้ค)าขีองอาร�เรย์�ให&เป3นขี&อควิาม จะไม)สามารถุใช& โอเปอร�เรเตอร� = ได้&โด้ย์ตรง เช)น

char a[20];

a = “Hello World!”;

ถุ&าต&องการเก0บขี&อควิามใด้ๆ ลงในอาร�เรย์� ต&องใช&วิธ์�การเร�ย์กใช&ฟั�งก�ช�น เช)น ฟั�งก�ช�นมาตรฐาน strcpy( ) ท��นย์ามไวิ&ใน <string.h>

Page 80: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 80

ต�วิอย์)างท�� ต�วิอย์)างท�� 19 : 19 : การก/าหนด้ค)าขีองสตรงโด้ย์การก/าหนด้ค)าขีองสตรงโด้ย์ใช&ฟั�งก�ช�น ใช&ฟั�งก�ช�น strcpy()strcpy()

#include <stdio.h>#include <string.h>int main(){ char a[20] = "XXXXXXXXXX"; // 10 of X

printf("%s\n", a); strcpy(a, "ABCD"); printf("%s\n", a); strcpy(a, "0123456789"); printf("%s\n", a);

return 0;}

XXXXXXXXXXABCD0123456789

Page 81: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 81

ฟั�งก�ช�นมาตรฐานท��นย์ามใน ฟั�งก�ช�นมาตรฐานท��นย์ามใน <string.h><string.h>• strcpy (char str1[],char str2[])

ค�ด้ลอกขี&อควิามจาก str2 ไปใส)ใน str1 (ต�วิแรกจะถุ*กเปล��ย์นค)าไป ต�วิท��สองค)าคงเด้ม) string copy

• int strlen (char str1[ ]) ฟั�งก�ช�น return ควิามย์าวิสาย์อ�กขีระ = จ/านวินต�วิอ�กษรก)อนถุ2ง ‘\0’ ต�วิแรก string length

• strcat( 1 2char str [ ], char str [ ] )

น/าขี&อม*ลจากสาย์อ�กขีระ str2ไปใส)ต)อท&าย์สาย์อ�กขีระ str1 (ต�วิแรกจะถุ*กเปล��ย์นค)าไป ต�วิท��สองค)าคงเด้ม) string concatenation

Page 82: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 82

• int strcmp ( 1 2char str [ ], char str [ ] )

เท�ย์บค)าสตรงท�.งสอง โด้ย์ค)าท�� return จะม�ค)า- น�อยก์ว#� 0 (-1,-2, …) เม-�อ str1[ ] น&อย์กวิ)า 2str [ ]

เช)น “abc”< ”xyz”, “a1”< ”a2”

- เทำ#�ก์�บ 0 เม-�อ str1[ ] เท)าก�บ 2str [ ]- ม�ก์ก์ว#� 0 (1,2,…) เม-�อ str1[ ] มากกวิ)า 2str [ ] เช)น

“apples”> ”apple”, “a”> “A”, “rat”> ”cat”, “cat”>”Rat”

ฟั�งก�ช�นมาตรฐานท��นย์ามใน ฟั�งก�ช�นมาตรฐานท��นย์ามใน <string.h><string.h>

Page 83: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 83

ต�วิอย์)างต�วิอย์)าง การใช&ฟั�งก�ช�นเก��ย์วิก�บสตรงการใช&ฟั�งก�ช�นเก��ย์วิก�บสตรง• ไฟัล� arrayex20.c

#include <stdio.h>#include <string.h>int main() { char name[20]; int len; strcpy(name, ”David Young” ); len = strlen(name); printf(“Length of %s is %d\n”,name, len); return 0;}

Length of David Young is : 11

Page 84: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 84

ต�วิอย์)างต�วิอย์)าง การใช&ฟั�งก�ช�นเก��ย์วิก�บสตรงการใช&ฟั�งก�ช�นเก��ย์วิก�บสตรง• ไฟัล� arrayex21.c#include <stdio.h>#include <string.h>int main(){ char str[] = "CAN"; int i, len = strlen(str);

printf("%s\n",str); for(i=0; i<len; i++) str[i] = str[i] + 1;

printf("%s\n",str); return 0;}

CANDBO

Page 85: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 85

แบบฝึ?กห�ด้การใช&ฟั�งก�ช�นเก��ย์วิก�บสตรงแบบฝึ?กห�ด้การใช&ฟั�งก�ช�นเก��ย์วิก�บสตรง• จงเขี�ย์นโปรแกรม น�บจ/านวินต้�วเลข(digit)ท��

ปรากฏในขี&อควิามแต)ละบรรท�ด้ ถุ&าขี&อควิามเป3น “END” ให&จบการท/างาน #include <stdio.h>

#include <string.h>#define MX 101 //max characters in one lineint main(){ char line[MX]=””; int d, i; gets(line); do { for(i=0, d=0; i<strlen(line); i++) if(line[i]>=‘0’ && line[i]<=‘9’) d++; printf(“%d\n”, d); gets(line); } while( strcmp(line,”END”)!= 0 ); return 0;}

Input line 11Another line0123 93457Date:11/12/2013 8ENDING 0END

Page 86: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 86

อ�ร�เรย�ของสต้ร(งอ�ร�เรย�ของสต้ร(ง

• เน-�องจาก String จ�ด้เป3นอาร�เรย์� 1 มต char name[6] = "Nesta";

• ด้�งน�.นอาร�เรย์�ขีอง String จ2งต&องม�ร*ปแบบเป3นอาร�เรย์� 2 มต เช)น

char names[2][8] ={ "Nesta","Maldini" };

N e s t a \0 \0 \0

M a l d i n i \0

names[0]

names[1]

Page 87: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 87

โจทย์�ฝึ?กสมองท�� โจทย์�ฝึ?กสมองท�� 4 4 : : ไฟัล� ไฟัล� arrayprac4.carrayprac4.c

• จงเขี�ย์นโปรแกรมส/าหร�บร�บขี&อควิามจากผิ*&ใช& 4ขี&อควิาม โด้ย์แต)ละขี&อควิามม�ควิามย์าวิไม)ถุ2ง

20 ต�วิอ�กษร จากน�.นพิมพิ�ค)าควิามย์าวิขีองแต)ละขี&อควิามออกทางหน&าจอ(ฟั�งก�ช�นท��ใช&ในการหาควิามย์าวิขีองขี&อควิามค-อ strlen )

ต�วิอย์)างผิลการร�นเป3นด้�งน�.

Enter text 1: MathsEnter text 2: PhysicsEnter text 3: ChemistryEnter text 4: BiologyMaths: 5 Physics: 7 Chemistry: 9 Biology: 7

Page 88: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 88

//arrayprac4.c#include <stdio.h>#include <string.h>int main(){ char texts[4][20]; int i; for(i=0;i<4;i++) { printf(“Enter text %d : ” ,i+1 ); scanf(“%s”,&texts[i]); } for(i=0;i<4;i++) printf(“%s: %d\n” ,texts[i],strlen(texts[i]); return 0;}

Enter text 1: MathsEnter text 2: PhysicsEnter text 3: ChemistryEnter text 4: BiologyMaths: 5 Physics: 7 Chemistry: 9 Biology: 7

Page 89: บทที่ 5 ฟังก์ชัน Function

242-101, 241-101 Introduction to Computer Programming

Department of Computer Engineering, PSU 89

• จงเขี�ย์นโปรแกรมค/านวิณผิลรวิมจ/านวินเต0มจาก 1 ถุ2ง n โด้ย์สร&างฟั�งก�ช�น int summ(int n) ส/าหร�บการค/านวิณ (ทด้ลองเขี�ย์นเวิอร�ช��นการใช&ล*ป ก�บ เวิอร�ช��นการใช& recursive function)

• จงเขี�ย์นฟั�งก�ช�นส/าหร�บการบวิกเมตรกส� ท��ม�ขีนาด้เท)าก�น ให&ฟั�งก�ช�นร�บอาร�เรย์� 2 มต (เมตรกส� ) สามต�วิ และ จ/านวินแถุวิและหล�กขีองเมตรกส�

• จงเขี�ย์นฟั�งก�ช�นแก&ไขีค)าขีองท�ก element ในอาร�เรย์�ให&เป3นส�มบ*รณ� (absolute) หร-อค)าบวิกเสมอ โปรโตไทป@ void absolute (int array[100], int n) อาร�เรย์�ม�ควิามย์าวิไม)เกน 100 และค)า n ค-อจ/านวิน element ขีองอาร�เรย์�

แบบฝึ?กห�ด้ท&าย์บทแบบฝึ?กห�ด้ท&าย์บท