4.ฟังก์ชันในภาษาซี4. ฟังก์ชันในภาษาซีฟังก์ชันในภาษาซี
ฟังก์ชันในภาษาซี แบ่งออกได้เป็น 2 ประเภท
คือ
ฟังก์ชันมาตรฐาน (Standard Function)
เป็นฟังก์ชันที่ตัว Compiler มีมาให้อยู่แล้ว
เช่นฟังก์ชันการคำานวณทางคณิตศาสตร์
ฟังก์ชันการรับและแสดงผล เหล่านี้เรียกว่า
Library Function ในการใช้งานจะต้อง
เรียกใช้ Include Directives จึงสามารถใช้
งานฟังก์ชันนั้นๆ ได้
ฟังก์ชันที่ผู้เขียนโปรแกรมสร้างขึ้น
6. โครงสร้างของฟังก์ชันโครงสร้างของฟังก์ชัน
ฟังก์ชันมาตรฐานและฟังก์ชันที่ผู้ใช้สร้างขึ้น มี
โครงสร้าง ดังนี้
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) ;
}
7. โครงสร้างของฟังก์ชันโครงสร้างของฟังก์ชัน
ฟังก์ชันมาตรฐานและฟังก์ชันที่ผู้ใช้สร้างขึ้น มี
โครงสร้าง ดังนี้
type para-1, type para-2, ... , type para-n
หรือ parameter เป็นตัวแปรที่ต้องส่งเข้ามาเพื่อ
นำามาประมวลผลเมื่อมีการเรียกใช้ฟังก์ชันพร้อมทั้ง
กำาหนดชนิดของตัวแปรด้วย type หากมีตัวแปร
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) ;
}
8. โครงสร้างของฟังก์ชันโครงสร้างของฟังก์ชัน
ฟังก์ชันมาตรฐานและฟังก์ชันที่ผู้ใช้สร้างขึ้น มี
โครงสร้าง ดังนี้
ชุดคำาสั่งทั้งหมด จะต้องรวมอยู่ในวงเล็บปีกกา { }
local variable-declarations เป็นการสร้าง
ตัวแปรเพื่อใช้งานภายในฟังก์ชัน
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) ;
}
9. โครงสร้างของฟังก์ชันโครงสร้างของฟังก์ชัน
ฟังก์ชันมาตรฐานและฟังก์ชันที่ผู้ใช้สร้างขึ้น มี
โครงสร้าง ดังนี้
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) ;
}
10. ฟังก์ชันประเภทต่างๆฟังก์ชันประเภทต่างๆ
ฟังก์ชันสามารถแบ่งออกเป็น 3 ประเภท ตาม
การรับ-ส่งค่าข้อมูล
ฟังก์ชันประเภทไม่มีการรับส่งค่าใดๆ เป็นแบบ
ที่ง่ายที่สุด คือไม่ต้องส่งพารามิเตอร์ให้กับฟังก์ชัน
และฟังก์ชันไม่ส่งค่าอะไรคืนออกมา โดยปกติใช้
แสดงข้อความที่มีการใช้งานบ่อยๆ
ฟังก์ชันแบบส่งค่าผ่านทางเดียว สามารถเป็น
ได้ 2 แบบคือ ฟังก์ชันที่มีการรับค่าเข้ามาทำางานใน
ฟังก์ชันอย่างเดียว หรือมีการคืนค่าออกมาอย่าง
เดียว
ฟังก์ชันแบบส่งค่าสองทาง ฟังก์ชันที่มีการรับค่า
13. ตัวอย่าง 1 โปรแกรมที่ยังไม่ได้
สร้างฟังก์ชัน
#include <stdio.h>
#include <conio.h>
StarLine( )
printf("n");
void main()
{
StarLine();
printf("StarLine Printingn");
StarLine();
getch();
}
โปรแกรม
ผลการ
ทำางาน ?
StarLine Printing
ชื่อที่ต้องการ
เรียกใช้งาน
คำาสั่งเดิม
ที่ใช้
ชื่อของฟังก์ชัน
14. ตัวอย่าง 1 ฟังก์ชันที่ไม่มีการ
รับส่งค่า
#include <stdio.h>
#include <conio.h>
void StarLine(void)
{
printf("n");
}
void main()
{
StarLine();
printf("StarLine Printingn");
StarLine();
getch();
}
โปรแกรม
ผลการ
ทำางาน ?
StarLine Printing
ไม่มีการรับ
ส่งค่า
การเรียก
ใช้งาน
15. ตัวอย่าง 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 Printingn");
StarLine(17);
getch();
}
โปรแกรม
ผลการ
ทำางาน ?**
StarLine Printing
**
มีการรับค่า
n
การเรียก
ใช้
17. ตัวอย่าง 3 ฟังก์ชัน Pass by
Value
#include <stdio.h>
#include <conio.h>
void NumChange(int number)
{
printf("Before change : %dn",number);
number += 5;
printf("After change : %dn",number);
}
void main()
{
int number = 15;
NumChange(number);
printf("After entering the function : %dn",number);
getch();
}
โปรแกรม
ผลการ
Before change : 15
After change : 20
After entering the function : 15
มีการรับค่า
number
Pass by Value
18. ตัวอย่าง 4 ฟังก์ชัน Pass by
Reference
#include <stdio.h>
#include <conio.h>
void NumChange(int &number2)
{
printf("Before change : %dn",number2);
number2 += 5;
printf("After change : %dn",number2);
}
void main()
{
int number = 15;
NumChange(number);
printf("After entering the function : %dn",number);
getch();
}
โปรแกรม
ผลการ
Before change : 15
After change : 20
After entering the function : 20
มีการรับค่า
number2
Pass by Reference
19. ตัวอย่าง 5 ฟังก์ชัน Pass by
Reference
#include <stdio.h>
#include <conio.h>
void NumChange(int *number2)
{
printf("Before change : %dn",*number2);
*number2 += 5;
printf("After change : %dn",*number2);
}
void main()
{
int number = 15;
NumChange(&number);
printf("After entering the function : %dn",number);
getch();
}
โปรแกรม
ผลการ
Before change : 15
After change : 20
After entering the function : 20
มีการรับค่า
number2
Pass by Reference
20. ตัวอย่าง 6 ฟังก์ชันที่รับค่าเป็น
อาร์เรย์
#include <stdio.h>
#include <conio.h>
void Dollar(float baht[], int num)
{
int i;
printf("Before changing : %.02fn", baht[0]);
for(i=0; i<num; i++)
baht[i] *= 40.0;
printf("After changing : %.02fn", baht[0]);
}
void main()
{
float money[3] = {200.0, 400.0, 600.0};
Dollar(money, 3);
printf("After entering function: %.02fn", money[0]);
getch();
}
โปรแกรม
ผลการ
ทำางาน ?
Before changing : 200.00
After changing : 8000.00
After entering function: 8000.00
21. ตัวอย่าง 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.2fn", area);
getch();
}
โปรแกรม
ผลการ
Enter Radius: 5Enter Radius: 5
Area is 78.50
มีการรับส่ง
ค่า
ส่งค่ากลับ
เรียกใช้
ฟังก์ชัน
และมี
ตัวแปรรับ
ค่า
23. การเขียนการเขียน 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
คือ หัวของ
ฟังก์ชันที่สร้างไว้
25. #include <stdio.h>
#include <conio.h>
void StarLine(void);
void main()
{
StarLine();
printf("StarLine Printingn");
StarLine();
getch();
}
void StarLine(void)
{
printf("n");
}
โปรแกรม
ผลการ
ทำางาน ?
StarLine Printing
Prototype
Function
ตัวอย่าง 8 การเขียน
Prototype ของฟังก์ชัน
26. ตัวอย่าง 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
28. ตัวอย่าง 10 ตัวแปร Gobal
#include <stdio.h>
#include <conio.h>
int num = 19;
void increase()
{
num = num + 5;
}
void main()
{
printf("num = %dn", num);
increase();
printf("num = %dn", num);
getch();
}
โปรแกรม
ผลการ
num = 19num = 19
num = 24
Gobal variable
29. ตัวอย่าง 11 ตัวแปร Local
#include <stdio.h>
#include <conio.h>
void new_value()
{
int x;
x = 100;
}
void main()
{
int x = 59;
printf("x = %dn", x);
new_value();
printf("x = %dn", x);
getch();
}
โปรแกรม
ผลการ
num = 59num = 59
num = 59
Local variable
Local variable
31. ฟังก์ชันการคำานวณทางฟังก์ชันการคำานวณทาง
คณิตศาสตร์คณิตศาสตร์ –– 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 เป็นเลขจำานวนจริง
32. ตัวอย่าง 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.4fn", y);
printf("cos(pi/4) = %7.4fn", cos(x2));
printf("tan(pi/6) = %7.4fn", tan(M_PI/6));
printf("sqrt(2) = %7.4fn", sqrt(2));
printf("2.5^3 = %7.4fn", pow(2.5,3));
printf("loge(e) = %7.4fn", log(M_E));
printf("log(100) = %7.4fn", log10(100));
printf("e^3 = %7.4fn", exp(3));
printf("abs(-7) = %7.4fn", fabs(-7));
getch();
}
โปรแกรม
ผลการ
sin(pi) = 0.0000
cos(pi/4) = 0.7071
tan(pi/6) = 0.5774
sqrt(2) = 1.4142
2.5^3 = 15.6250
loge(e) = 1.0000
log(100) = 2.0000
e^3 = 20.0855
abs(-7) = 7.0000
34. ตัวอย่าง 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 = %sn", str1);
printf("str3 = %sn", str3);
getch();
}
โปรแกรม
ผลการ
str1 = Current Date 18 August 2006
str3 = Current Date
35. ตัวอย่าง 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 1n");
printf("Length of buffer 1 : %dn", strlen(buf1));
getch();
}
โปรแกรม
ผลการ
buffer 1 : "Nanometer"
buffer 2 : "Nanometer"
buffer 2 is equal to buffer 1
Length of buffer 1 : 9
36. ตัวอย่าง 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("%sn",string);
for(i=0; i<length; i++)
string[i] = tolower(string[i]);
printf("%sn",string);
getch();
}
โปรแกรม
ผลการ
ทำางาน ?THIS IS A STRING
this is a string
38. ตัวอย่าง 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("%sn", line1);
printf("%sn", line2);
printf("%sn", line3);
gotoxy(14,1);
clreol();
gotoxy(1,2);
insline();
getch();
}
โปรแกรม
ผลการ
ทำางาน ?Ferguson want
Henry pleading with Cole to stay
Mourinho not giving up on Gallas
39. ฟังก์ชันฟังก์ชัน 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 bytesn", sizeof(age));
printf("size of grade = %d bytesn", sizeof(grade));
printf("size of song = %d bytesn", sizeof(song));
printf("size of double = %d bytesn", sizeof(double));
getch();
}
ตัวอย่าง 17 โปรแกรมใช้
งานฟังก์ชัน sizeof(x)
ผลการ
size of age = 2 bytes
size of grade = 4 bytes
size of song = 15 bytes
size of double = 8 bytes