- ฟังก์ชั่นคือชื่อเรียกของโค๊ดที่ไม่ขึ้นอยู่กับส่วนใดๆของโปรแกรม ทำงานด้วยตัวเอง และอาจจะส่งค่าคืนกับผู้เรียกใช้งานหรือไม่ก็ได้
- การประกาศฟังก์ชั่น
return_type function_name(parameter_type1 name_1 , parameter_Type2 name_2,….parameter_type2 name n);
- Function definition
return_type function_name(parameter_type1 name_1 , parameter_Type2 name_2,….parameter_type2 name n)
{
// statement
// statement
……
// statement
};
- ควรตั้งชื่อฟังก์ชั่นให้มีความหมายชัดเจน
- ดูว่าการเรียกใช้ฟังก์ชั่นแต่ละครับ มีการส่งผ่านอาร์กิวเมนต์ถูกต้องหรือไม่
- กำหนดอาร์กิวเมนต์ที่จำเป็นจริงๆเท่านั้น
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
float add(float num1,float num2); | |
int main(void) | |
{ | |
float a; | |
a = add(4.5,6.4); | |
printf("%f",a); | |
return 0; | |
} | |
float add(float a,float b) | |
{ | |
float ans; | |
ans = a + b; | |
return ans; | |
} |