Transcript Function

ฟังก์ ชัน (Function)

ฟังก์ชัน (Function) :
คือโปรแกรมย่อยที่สามารถ
ทางานใดงานหนึ่ งได้ มี ชื่อ
เฉพาะตัว สามารถเรี ยกใช้
ได้ เมื่อคอมพิวเตอร์ทางาน
เสร็ จ แล้ว จะต้องย้อนกลับ
มาท างานยัง จุ ด ที่ เ รี ยกใช้
ฟังก์ชนั นั้น
# include “stdio.h”
main( ) {
…………;
…………;
}
cola( ) {
…………;
…………;
}
pepsi( ) {
…………;
…………;
}
Function main
Function cola
Function pepsi
1
ตัวอย่ างฟังก์ ชันภาษาซี
#include "stdio.h"
main( ){
printf("Main Hello\n");
cola( );
pepsi( );
getch( );
}
void cola( ){
printf("Cola Hello\n");
}
void pepsi( ){
printf("Pepsi Hello\n");
}
ผลลัพธ์ คือ
Main Hello
Cola Hello
Pepsi Hello
การทางานของโปรแกรม
1. โปรแกรมจะเริ่ ม ต้น รั น ที่ ฟั ง ก์ ชัน
main( ) ก่อนอันดับแรก
2. จากนั้นฟั งก์ชนั main( ) จะมีการ
เรี ยกใช้ฟังก์ชนั cola( ) และ pepsi( )
ตามลาดับ
2
ฟังก์ ชันในภาษาซีและการประกาศโพรโตไทป์

สามารถแบ่งฟังก์ชนั ในภาษาซีตามที่มาของฟังก์ชนั ได้เป็ น 2 ประเภท คือ
1. ฟังก์ชนั มาตรฐาน (standard function) เป็ นฟังก์ชนั ที่ถกู สร้างขึ้น และเก็บไว้ในไลบรารี
ในการใช้งานจะต้องเรี ยกใช้ คาสัง่ #include เปิ ดไฟล์ข้ ึนมาก่อนจึงสามารถใช้ได้
เช่น คาสั่ง/ฟังก์ชนั printf ( ) , scanf ( ) , getchar( ) , getche( ) เป็ นต้น
2. ฟังก์ชนั ที่ผเู ้ ขียนโปรแกรมสร้างขึ้นเอง ซึ่ งผูเ้ ขียนสร้างขึ้นเพื่อทางานอย่างใดอย่างหนึ่ ง

การสร้างฟังก์ชนั มีรูปแบบดังนี้
Type function_name (parameter-list)
{
statement ;
[return];
}
3
ตัวอย่ างฟังก์ ชันภาษาซี
void Show_A( ) {
printf (“AAA\n”);
printf (“BBB\n”);
}
ฟังก์ชนั Show_A มีการประกาศตัวแปร
i ภายในฟังก์ชนั เมื่อคอมพิวเตอร์ เรี ยกใช้
ฟังก์ชนั นี้จะมีการพิมพ์ตวั A จานวน 20 ตัว
void Show_A(int A ) {
int i ;
for (i =1 ; i <= A ; i++)
printf (“A”);
}
ฟังก์ชนั Show_A จะไม่มีการส่ งค่ากลับ
เพราะมีการประกาศคาว่า void และไม่มี
การส่ งผ่านค่าพารามิเตอร์
void Show_A( ) {
int i ;
for (i =1 ; i <= 20 ; i++)
printf (“A”);
}
ฟังก์ชนั นี้จะมีการรับข้อมูลที่เป็ นตัวเลข
จานวนเต็มเข้าไป ผ่านทางพารามิเตอร์ A
เมื่อโปรแกรมเรี ยกใช้ฟังกชันนี้ จะพิมพ์ตวั
A จานวนเท่ากับค่าที่ผา่ นเข้าไปในฟั งก์ชนั
4
ตัวอย่ างฟังก์ ชันภาษาซี
int ADD_AB(int A , int B) {
int x;
x=A+B;
return x ;
}
ผลลัพธ์ คือ
This Total is 15
ฟังก์ชนั นี้จะใช้ในการบวกเลขจานวน
เต็มที่ผา่ นเข้าไปทางพารามิเตอร์ A และ B
เมื่อโปรแกรมเรี ยกใช้ฟังก์ชนั นี้จะมีการ
คืนค่าผลบวกที่เป็ นตัวเลขจานวนเต็มออกมา
# include “stdio.h”
main( ) {
int Total ;
Total = ADD_AB (5 , 10) ;
printf (“This Total is %d”,Total);
}
int ADD_AB(int A , int B) {
int x;
x=A+B;
return x ;
}
5
ตัวอย่ างฟังก์ ชันภาษาซี
โปรแกรมที่ 6.1
#include “stdio.h”
#include “conio.h”
void line( ) {
int j;
for (j=1 ; j<=26 ; j++) {
printf(“-”);
}
printf(“\n”);
}
main( ){
printf(“\n”);
line( );
printf(“- I LOVE YOU -\n”);
line( );
}
โปรแกรมตัวอย่างพิเศษ
#include “stdio.h”
#include “conio.h”
int add(int a , int b) {
int c;
c=a+b;
return c ;
}
main( ){
int x , y ;
printf(“Input Number1 2:”);
scanf(“%d %d”,&x,&y);
printf(“%d+%d = %d\n”x,y,add(x,y));
}
6
โพรโตไทป์ (prototypes)

การเขียนโปรแกรมที่มีการสร้างฟังก์ชนั จะมีการเขียนชื่อฟังก์ชนั รวมไว้ที่ส่วนหัว
ของโปรแกรมซึ่งเรี ยกว่า โพรโตไทป์ (prototypes)
# include “stdio.h”
int multiply (int multiplier , int multiplicand);
main( ){
int product ;
...
product = multiply(6,7) ;
prototype
...
}
int multiply (int x , int y) {
return x * y ;
}
7
ตัวอย่ างฟังก์ ชัน
โปรแกรมที่ 6.4
#include “stdio.h”
double volume(double s1,double s2,double s3);
main( ){
double vol ;
vol = volume(12.2,5.67,9.03);
printf(“Volume :%f”,vol);
}
double volume(double s1,double s2,double s3)
{
return s1 * s2 * s3;
}
โปรแกรมที่ 6.8
#include “stdio.h”
void Show_text(int num);
main( ){
Show_text(5);
Show_text(15);
}
void Show_text(int num)
{
int i;
for(i=1 ; i<num ; i++){
printf(“*”);
}
}
8
ตัวอย่ างฟังก์ ชัน
main( )
1
4
PrintHello( )
2
3
InputNum( )
#include “stdio.h”
int PrintHello( );
int InputNum( );
main( ){
printf(“Bye Bye: %d”,PrintHello( ));
}
int PrintHello( ){
int input;
printf(“Hello Pls Enter num:”);
input = InputNum( );
return input + 100;
}
int InputNum( ){
int aaa;
scanf(“%d”,&aaa);
return aaa;
}
9
ตัวแปรทั่วไปและตัวแปรเฉพาะที่

ตัวแปรทัว่ ไป (global variables) เป็ นตัวแปรที่ทุกส่ วนของโปรแกรมสามารถ
เรี ยกใช้ได้ การประกาศตัวแปรประเภทนี้จะประกาศไว้นอกฟังก์ชนั ต่าง
โปรแกรมที่ 6.11
#include “stdio.h”
int a;
void Ex( ) {
a = 5;
printf(“%d\n”,a);
}
main( ) {
a = 3;
printf(“%d\n”,a);
Ex( );
printf(“%d\n”,a);
}
ผลลัพธ์
Global variable
3
5
5
การทางานของโปรแกรมนั้นจะมี
ตัวแปร a เป็ นตัวแปรแบบทัว่ ไป
ซึ่งทุกส่ วนของโปรแกรมสามารถ
เรี ยกใช้ และแก้ไขค่าของตัวแปร
ได้
10
ตัวแปรทั่วไปและตัวแปรเฉพาะที่
ตัวแปรเฉพาะที่ (local variables) เป็ นตัวแปรที่ใช้งานภายในฟังกชัน การประกาศตัวแปร
ประเภทนี้จะประกาศภายในฟั งก์ชนั นั้น ซึ่ งจะทาให้ฟังก์ชนั ต่าง มีตวั แปรชื่อเดียวกันได้
โปรแกรมที่ 6.12
ผลลัพธ์

#include “stdio.h”
int a;
void Ex( ) {
int b=10;
a = 5;
printf(“%d\n”,b);
}
main( ) {
a = 3;
printf(“%d\n”,a);
Ex( );
printf(“%d\n”,a);
}
Global variable
3
10
5
local variable
11
ตัวแปรทั่วไปและตัวแปรเฉพาะที่
โปรแกรมที่ 6.13
#include “stdio.h”
int count;
void func1( );
void func2( );
main( ) {
count = 100;
func1( );
}
void func1( ) {
func2( );
printf(“count is %d”,count);
}
void func2( ) {
int count;
for (count = 1;count<10;count++) {
printf(“.”);
}
}
โปรแกรมตัวอย่างพิเศษ
#include “stdio.h”
int count;
void func1( );
void func2( );
main( ) {
count = 100;
func1( );
}
void func1( ) {
func2( );
printf(“count is %d”,count);
}
void func2( ) {
for (count = 1;count<10;count++) {
printf(“.”);
}
}
12
ฟังก์ ชันมาตรฐาน


จากที่กล่าวมาถ้าแบ่งประเภทของฟังก์ชนั ตามที่มาแล้ว สามารถแบ่งได้เป็ น
ฟังก์ชนั ที่ผใู ้ ช้สร้างขึ้น และฟังก์ชนั มาตรฐาน
ฟังก์ชันมาตรฐาน คือ ฟังก์ชนั ที่เทอร์โบซีสร้างไว้ให้แล้ว ซึ่งเราสามารถเรี ยกใช้
ฟังก์ชนั ต่าง เหล่านั้นได้เลยทันที แต่ก่อนที่จะเรี ยกใช้ได้เราจะต้องทาการ
include ไฟล์ที่เก็บฟังก์ชนั มาตรฐานนั้นไว้ในส่ วนหัวของโปรแกรมก่อนจึงจะ
เรี ยกใช้ฟังก์ชนั นั้นได้
ฟังก์ชนั ที่สร้างขึ้นเอง
ฟังก์ชนั มาตรฐาน
#include “stdio.h”
void func1( );
main( ) {
count = 100;
func1( );
getch( );
}
void func1( ) {
func2( );
printf(“count is %d”,count);
}
13
ฟังก์ ชันมาตรฐาน



ตัวอย่างของฟังก์ชนั มาตรฐานเช่น กลุ่มฟังก์ชนั ทางคณิ ตศาสตร์ ที่อยูใ่ นไฟล์ math.h เช่น
 int abs(int num) เป็ นฟั งก์ชน
ั ที่จะทาหน้าที่คืนค่าสมบูรณ์ของค่า num
 double pow(double base,double exp) เป็ นฟั งก์ชน
ั ที่จะทาหน้าที่คืนค่าของเลขยกกาลัง
 double sqrt(double num) จะให้ค่ารากของ num ถ้า num เป็ นค่าลบจะ error
ตัวอย่างของฟังก์ชนั มาตรฐานที่เกี่ยวกับ String จะอยูใ่ นไฟล์ string.h
 int strcmp(str1,str2) ใช้เปรี ยบเทียบข้อความ 2 ข้อความว่าเหมือนกันหรื อไม่ถา้ เหมือนกันจะ
return ค่า 0 ออกมา แต่ถา้ ข้อความไม่เหมือนกันจะให้ค่า บวก และลบออกมา
 void strcpy(str1,str2) ใช้สาหรับ copy ข้อความจาก str2 มาไว้ใน str1
 int strlen(str) ใช้สาหรับหาความยาวของ string
 void strcat(str1,str2) ใช้สาหรับรวมข้อความจาก str1 และ str2 เข้าด้วยกันโดยจะเก็บไว้ที่ str1
ตัวอย่างของฟังก์ชนั มาตรฐานที่อยูใ่ นไฟล์ stdlib.h เช่น
 int random(int num) เป็ นฟั งก์ชน
ั ที่ใช้ในการสุ่ มตัวเลขไม่เกินตัวเลข num แต่ก่อนใช้ฟังก์ชนั
random ควรจะใช้ฟังก์ชนั void randomize(void) ก่อนเพื่อให้การรันโปรแกรมไม่ได้ค่าเดิ14ม
ตัวอย่ างการใช้ งานฟังก์ ชันมาตรฐาน
#include “stdio.h”
#include “math.h”
main( ){
printf(“%d”,abs(-5 ));
}
#include “stdio.h”
#include “math.h”
main( ){
printf(“%f”,pow(5,2 ));
}
#include "stdio.h"
#include "stdlib.h"
main( ) {
int i, a=5;
randomize();
for (i=1;i<=30;i++){
printf(“%d",random(a)+1);}
}
#include “stdio.h”
#include “string.h”
main( ){
char str1[20],str2[20];
printf(“Enter a string: “);
gets(str1);
strcpy(str1,str2);
printf(“\n%s”,str1);
}
#include “stdio.h”
#include “string.h”
main( ){
char str[20];
printf(“Enter a string: “);
gets(str);
if ((strcmp(str,”hello”) = = 0) {
printf(“\nRight”);
}
}
15