21
259201 Computer Programming for Engineers ฟฟฟฟฟฟฟฟ Function

259201 Computer Programming for Engineers

  • Upload
    vala

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

259201 Computer Programming for Engineers. ฟังก์ชัน Function. Outline. บทนำ โปรแกรมย่อยภาษาซี การนิยามฟังก์ชัน ต้นแบบของฟังก์ชัน ตัวแปรภายใน และตัวแปรภายนอก. บทนำ. - PowerPoint PPT Presentation

Citation preview

Page 1: 259201 Computer Programming for Engineers

259201Computer Programming for Engineers

ฟั�งก์�ชั�นFunction

Page 2: 259201 Computer Programming for Engineers

Outline

• บทนำ��• โปรแกรมย่ อย่ภ�ษ�ซี�• ก�รนำ�ย่�มฟั�งก�ชั�นำ• ต้�นำแบบของฟั�งก�ชั�นำ• ต้�วแปรภ�ย่ในำ และต้�วแปรภ�ย่นำอก

Page 3: 259201 Computer Programming for Engineers

บทน��

• โปรแกรมม�ขนำ�ดใหญ่ ท��ให�ก�รเข�ย่นำไว�ภ�ย่ใต้� main() เก�ด คว�มย่' งย่�ก ผิ�ดพล�ดง �ย่ นำอกจ�นำ�+นำเม,-อต้�องก�รต้รวจ

สอบคว�มผิ�ดพล�ดท�-เก�ดจ�กโปรแกรมย่�งท��ได�ย่�กอ�กด�วย่

• Divide and Conquer ค,อก�รแก�ไขป�ญ่ห�ข��งต้�นำ โดย่แบ ง โปรแกรมออกเป/นำส วนำย่ อย่ ๆ ท�-ม�ขนำ�ดเล1ก (Module)

โดย่เม,-อสร��งและทดสอบโปรแกรมย่ อย่ ๆ แล�ว ก1ประกอบเป/นำโปรแกรมใหญ่ ท�-สมบ2รณ์�ในำข�+นำต้อนำส'ดท��ย่

Page 4: 259201 Computer Programming for Engineers

4

โปรแก์รมย่�อย่ ในภ�ษ�ซี�

BOSS(calling func)

WORKER(called func)

1. ส�-งให�ท��ง�นำ (call)

3. ร�ย่ง�นำผิล (return)

2. ท��ง�นำ

main

worker1 worker3worker2

worker4 worker5

Page 5: 259201 Computer Programming for Engineers

5

โปรแก์รมย่�อย่ ในภ�ษ�ซี�FUNCTIONS IN CFUNCTIONS IN C

C STANDARDLIBRARY

C STANDARDLIBRARY

PROGRAMMER DEFINED

FUNCTION

PROGRAMMER DEFINED

FUNCTION

• ฟั�งก�ชั�นำม�ต้รฐ�นำในำภ�ษ�ซี� (C Standard Function)– ฟั�งก�ชั�นำซี6-งอย่2 ในำไลบร�ร�ภ�ษ�ซี�ม�ต้รฐ�นำ (C Standard Library)

ซี6-งประกอบไปด�วย่ฟั�งก�ชั�นำพ,+นำฐ�นำท�-จ��เป/นำส��หร�บก�รท��ง�นำ เชั นำ ก�รจ�ดก�รก�บ input/output (printf ซี6-งอย่2 ในำไลบร�ร� stdio) ก�ร

ค��นำวณ์ท�งคณ์�ต้ศ�สต้ร� (sqrt ซี6-งอย่2 ในำไลบร�ร� math)

printf() #include <stdio.h>

Page 6: 259201 Computer Programming for Engineers

6

โปรแก์รมย่�อย่ ในภ�ษ�ซี�

• ฟั�งก�ชั�นำท�-สร��งข6+นำใหม โดย่โปรแกรมเมอร� (Programmer-Defined Function)

– โปรแกรมเมอร�ส�ม�รถเข�ย่นำฟั�งก�ชั�นำเพ,-อนำ�ย่�มก�รท��ง�นำท�-จะเร�ย่กใชั�ในำส วนำ ต้ �งๆของโปรแกรม โดย่ฟั�งก�ชั�นำก�รท��ง�นำด�งกล �วจะถ2กเข�ย่นำไว�ในำฟั�งก�ชั�นำ

เพ�ย่งคร�+งเด�ย่วเท �นำ�+นำ แต้ ส�ม�รถเร�ย่กใชั�ง�นำได�หล�ย่คร�+ง

Page 7: 259201 Computer Programming for Engineers

7

ก์�รน�ย่�มฟั�งก์�ชั�น

#include<stdio.h>

int main( ){

int i;

for(i = 1; i <= 10; i++) printf("%d ", i * i);

printf("\n”);return 0;

}

#include<stdio.h>

int square(int);

int main( ){

int i;

for(i = 1; i <= 10; i++) printf("%d ", square(i));

printf("\n”);return 0;

}

int square(int y){ return(y*y);}

Page 8: 259201 Computer Programming for Engineers

8

ก์�รน�ย่�มฟั�งก์�ชั�น

x

square( )

y

COPY ARGUMENTS

COPY RETURN-VALUE

y * yprintf

#include<stdio.h>

int square(int);

int main( ){ int i;

for(i = 1; i <= 10; i++)printf("%d ", square(i));

printf("\n”); return 0;}

int square(int y){ return(y*y);}

Page 9: 259201 Computer Programming for Engineers

9

ก์�รน�ย่�มฟั�งก์�ชั�นreturn-value-type function-name(parameter-list){

declarations;statements;

}

• return-type-value ค,อ ชันำ�ดของข�อม2ลท�-ต้�องก�รจะส งค �ผิ �นำกล�บออกไปย่�ง ฟั�งก�ชั�นำผิ2�เร�ย่กใชั�หร,อจ'ดท�-เร�ย่กใชั�ง�นำเชั นำ int หร,อ float ห�กไม ส งค �กล�บให�ใชั�

void• function-name ค,อ ชั,-อของฟั�งก�ชั�นำท�-จะถ2กสร��งข6+นำ

• parameter-list ค,อร�ย่ก�รต้�วแปรพ�ร�ม�เต้อร�ท�+งหมดท�-ต้�องก�รส งผิ �นำไป ย่�งฟั�งก�ชั�นำเม,-อเร�ย่กใชั� ต้�มล��ด�บ

• declarations ค,อ ส วนำของก�รประก�ศต้�วแปรภ�ย่ในำฟั�งก�ชั�นำ• statements ค,อ ส วนำของค��ส�-งท�-กระท��ในำฟั�งก�ชั�นำนำ�+นำ

Page 10: 259201 Computer Programming for Engineers

10

ตั�วอย่��งท�� 1: ฟั�งก�ชั�นำท�-ไม ร�บผิ �นำค �ต้�วแปร และไม ส งผิ �นำค �กล�บ

void main(){ my_print();} void my_print(){ printf(“Hello world”);}

main my_print

1. ท��ง�นำ

2. พ�มพ�

3. เสร1จแล�วคร�บ

Page 11: 259201 Computer Programming for Engineers

11

ตั�วอย่��งท�� 2: ฟั�งก�ชั�นำท�-ม�ก�รร�บผิ �นำค �ต้�วแปร แต้ ไม ส งผิ �นำค �

กล�บ

void main(){ my_print(2);} void my_print(int x){ printf(“%d”, x);}

main my_print

1. ท��ง�นำ(2)

2. พ�มพ�

3. เสร1จแล�วคร�บ

Page 12: 259201 Computer Programming for Engineers

12

ตั�วอย่��งท�� 2: ฟั�งก�ชั�นำท�-ม�ก�รร�บผิ �นำค �ต้�วแปร แต้ ไม ส งผิ �นำค �

กล�บvoid main(){ my_print(‘a’, 5);}  void my_print(char ch, int x){ while (x > 0) {

printf(“%c”, ch);x--;

}}

main my_print

1. ท��ง�นำ(‘a’ และ 5)

2. พ�มพ�

3. เสร1จแล�วคร�บ

Page 13: 259201 Computer Programming for Engineers

13

ตั�วอย่��งท�� 3: ฟั�งก�ชั�นำท�-ม�ท� +งก�รร�บผิ �นำค � และส งผิ �นำค �กล�บ

void main(){ char ch; ch = my_print(5); printf(“%c\n”, ch);}  char my_print(int x){ char lch; printf(“Enter your character: ”); scanf(“%c”, &lch); while (x > 0) {

printf(“%c”, lch);x--;

} printf(“\n”); return lch;}

main my_print

1. ท��ง�นำ(5)

2. พ�มพ�

3. เสร1จแล�วคร�บ

ผิลค,อ lch

Page 14: 259201 Computer Programming for Engineers

14

ตั�วอย่��งท�� 4: ฟั�งก�ชั�นำท�-ไม ม�ก�รร�บผิ �นำค � และแต้ ม�ก�รส งผิ �นำค �

กล�บvoid main(){ char ch; ch = my_print(); printf(“%c\n”, ch);}  char my_print(void){ char lch; printf(“Enter your character: ”); scanf(“%c”, &lch); printf(“\n”); return lch;}

main my_print

1. ท��ง�นำ

2. พ�มพ�

3. เสร1จแล�วคร�บ

ผิลค,อ lch

Page 15: 259201 Computer Programming for Engineers

15

ตั�นแบบของฟั�งก์�ชั�น (Prototype)

#include<stdio.h>

int square(int);

void main( ) { int x=3;

printf(“Square of %d = %d\n”, x, square(x));}

int square(int y) { return y * y;}

ต้�นำแบบของฟั�งก�ชั�นำเป/นำต้�วบอกให�ต้�วแปลภ�ษ�ร2 �ถ6งชันำ�ดของข�อม2ลท�-จะส งค �กล�บ จ��นำวนำของต้�วแปรพ�ร�ม�เต้อร�ท�-ฟั�งก�ชั�นำค�ดหว�งว �จะได�ร�บ ชันำ�ดของพ�ร�ม�เต้อร�แต้ ละต้�ว และล��ด�บของพ�ร�ม�เต้อร�เหล �นำ�+นำ ต้�วแปลภ�ษ�ส�ม�รถท�-จะนำ��ข�อม2ล

เหล �นำ�+ในำก�รต้รวจสอบคว�มถ2กต้�องของก�รเร�ย่กใชั�ฟั�งก�ชั�นำ ในำต้�วอย่ �ง int square(int); ค,อต้�นำแบบของฟั�งก�ชั�นำ

PROTOTYPE

CALLING

DEFINITION

Page 16: 259201 Computer Programming for Engineers

16

ตั�นแบบของฟั�งก์�ชั�น (ตั�วอย่��งท��ถู!ก์ตั�อง)#include <stdio.h> int maximum(int, int, int); /*function prototype */

int main(){ int a, b, c;

printf(“Enter three integers: “);scanf(“%d%d%d”, &a, &b, &c);printf(“Maximum is: %d\n”,

maximum(a, b, c);

return 0;} /* Function maximum definition */int maximum(int x, int y, int z){

int max = x;if(y > max)

max = y;if(z > max)

max = z; 

return max;}

โปรแกรมเร�ย่กใชั�ฟั�งก�ชั�นำก อนำก�รนำ�ย่�ม ด�งนำ�+นำเพ,-อก�รต้รวจสอบคว�มถ2กต้�องในำก�รเร�ย่ก

ใชั� จ6งจ��เป/นำต้�องม�ต้�นำแบบของฟั�งก�ชั�นำ

int maximum(int, int, int);

Page 17: 259201 Computer Programming for Engineers

17

ตั�นแบบของฟั�งก์�ชั�น (ตั�วอย่��งท��ส�ม�รถูเข�ย่นได้�)

#include <stdio.h> int maximum(int x, int y, int z){

int max = x;if(y > max)

max = y;if(z > max)

max = z; 

return max;}

int main(){ int a, b, c;

printf(“Enter three integers: “);

scanf(“%d%d%d”, &a, &b, &c);

printf(“Maximum is: %d\n”, maximum(a, b, c);

return 0;} 

โปรแกรมได�นำ�ย่�มฟั�งก�ชั�นำไว�ก อนำก�รเร�ย่กใชั� จ6งไม จ��เป/นำต้�องม�ต้�นำแบบฟั�งก�ชั�นำ แต้ ข�อเส�ย่ค,อฟั�งก�ชั�นำ main จะถ2กเล,-อนำไปท��ย่

โปรแกรม เนำ,-องจ�กฟั�งก�ชั�นำ main เป/นำฟั�งก�ชั�นำหล�ก ในำก�รอ �นำ

โปรแกรมจ6งควรพบฟั�งก�ชั�นำนำ�+ก อนำ นำอกจ�กนำ�+ก�รเข�ย่นำในำล�กษณ์ะนำ�+ย่�งไม เหม�ะสมในำกรณ์�ท�-โปรแกรมม�ขนำ�ดใหญ่ ม�กและซี�บซี�อนำด�วย่

Page 18: 259201 Computer Programming for Engineers

18

ตั�วแปรภ�ย่ใน และตั�วแปรภ�ย่นอก์

ตั�วแปรภ�ย่ใน (Local Variable) ค,อ ต้�วแปรท�-ถ2กสร��งข6+นำภ�ย่ในำฟั�งก�ชั�นำ ส�ม�รถเร�ย่กใชั�ง�นำได�เฉพ�ะภ�ย่ในำฟั�งก�ชั�นำท�-สร��งข6+นำ และจะถ2กท��ล�ย่ลงเม,-อเสร1จส�+นำก�ร

ท��ง�นำของฟั�งก�ชั�นำนำ�+นำๆ

ตั�วแปรภ�ย่นอก์ (Global Variable) ค,อ ต้�วแปรท�-ถ2กสร��งข6+นำภ�ย่นำอกฟั�งก�ชั�นำ ส�ม�รถใชั�ง�นำได�ในำท'กฟั�งก�ชั�นำ หร,อท�+งโปรแกรม (ย่กเว�นำฟั�งก�ชั�นำท�-ม�ต้�วแปรภ�ย่ในำชั,-อ

เด�ย่วก�บต้�วแปรภ�ย่นำอก ซี6-งม�คว�มเป/นำไปได�) และจะคงอย่2 ต้ลอดก�รท��ง�นำของโปรแกรม

Page 19: 259201 Computer Programming for Engineers

19

ตั�วแปรภ�ย่ใน และตั�วแปรภ�ย่นอก์

#include<stdio.h>

int ans = 0; int inc_one(int); /* function prototype */ void main(){

ans = inc_one(3);printf(“Answer is %d\n”, ans);

 }

/* function definition: return x+1 */int inc_one(int x){

int ans, b;

b = 2;ans = x + b;return ans;

ans เป/นำต้�วแปรภ�ย่นำอกเพร�ะประก�ศไว�

ข��งนำอก ซี6-งจะเห1นำว �ฟั�งก�ชั�นำ main

ส�ม�รถเร�ย่กใชั�ได�โดย่ไม ต้�องประก�ศ

ต้�วแปร ans อ�ก

x, ans, b เป/นำต้�วแปรภ�ย่ในำฟั�งก�ชั�นำ

inc_one ท�-ร2 �จ�กแต้ ในำฟั�งก�ชั�นำนำ�+

เท �นำ�+นำ และต้�ว แปร ans ท�-ประก�ศในำ

ฟั�งก�ชั�นำนำ�+จะ เป/นำคนำละต้�วก�บต้�วแปร ans

ท�- ประก�ศข��งนำอกไม

เก�-ย่วข�องก�นำ

Page 20: 259201 Computer Programming for Engineers

20

ตั�วแปรภ�ย่ใน และตั�วแปรภ�ย่นอก์#include<stdio.h>

void my_func(); //prototype of my_func

void main(){

int x=3;

printf(“Main: Before call function x=%d\n”, x);

my_func(); //call my_funcprintf(“Main: After call function x=%d\

n”, x);}

void my_func(){

int x;

x=2;printf(“My_func: x=%d\n”, x);

ต้�วอย่ �งนำ�+แสดงถ6งต้�วแปรภ�ย่ในำฟั�งก�ชั�นำ main ชั,-อ x และต้�วแปร

ภ�ย่ในำฟั�งก�ชั�นำ my_func ชั,-อ x ซี6-งแม�จะม�ชั,-อเด�ย่วก�นำแต้ ค �ท�-เก1บก1ไม

เก�-ย่วข�องก�นำ ด�งนำ�+นำแม�เม,-อฟั�งก�ชั�นำ my_func ถ2กเร�ย่กใชั�ซี6-งค �ในำต้�วแปร x ในำฟั�งก�ชั�นำ my_func ถ2กก��หนำด

เป/นำ 2 ค � x ในำฟั�งก�ชั�นำ main ก1ย่�งม�ค �เป/นำ 3 เหม,อนำเด�ม ด�งผิลก�ร

ท��ง�นำของโปรแกรม

Main: Before call function x=3My_func: x=2Main: After call function x=3 

Page 21: 259201 Computer Programming for Engineers

21

ตั�วแปรภ�ย่ใน และตั�วแปรภ�ย่นอก์

#include<stdio.h>

void my_func(); //prototype of my_funcint x;

void main(){

printf(“Main: Before call function x=%d\n”, x);

my_func(); //call my_funcprintf(“Main: After call function x=%d\

n”, x);}

void my_func(){

x=2;printf(“My_func: x=%d\n”, x);

ต้�วอย่ �งนำ�+แสดงถ6งต้�วแปรภ�ย่นำอกชั,-อ x ซี6-งเป/นำท�-ร2 �จ�กในำท'ก ๆ ฟั�งก�ชั�นำ

ของโปรแกรม (ฟั�งก�ชั�นำไม ต้�องประก�ศก1ส�ม�รถเร�ย่กต้�วแปร x ใชั�ได�) ด�งนำ�+นำ ด�งนำ�+นำเม,-อฟั�งก�ชั�นำ

my_func ถ2กเร�ย่กใชั�ซี6-งค �ในำต้�วแปร x ในำฟั�งก�ชั�นำ my_func ถ2กเปล�-ย่นำค �ให�เป/นำ 2 ด�งนำ�+นำค � x ท�-แสดงในำฟั�ง

main จ6งเปล�-ย่นำเป/นำ 2 เหม,อนำก�นำ ด�งผิลก�รท��ง�นำของโปรแกรม

Main: Before call function x=3My_func: x=2Main: After call function x=2