Function prototype บอก compiler เกี่ยวกับจำนวน arguments, datatype ของ parameters, และ return type ช่วยตรวจสอบ function signature ก่อนเรียกใช้
ถ้าไม่มี prototype: compiler อาจ compile ได้แต่มี warning และอาจให้ผลลัพธ์ผิดปกติ
ถ้าฟังก์ชันถูกเรียกก่อน define (define อยู่หลัง) → ต้องมี prototype มิฉะนั้น compiler ไม่รู้จัก signature
ตัวอย่างไม่มี Prototype (มี warning)
#include <stdio.h>
int main() {
function(50);
}
void function(int x) {
printf("The value of x is: %d", x);
}ผลลัพธ์พร้อม Warning
The value of x is: 50 [Warning] conflicting types for 'function' [Note] previous implicit declaration of 'function' was here
ตัวอย่างมี Prototype (ไม่มี warning)
#include <stdio.h>
void function(int); // prototype
int main() {
function(50);
}
void function(int x) {
printf("The value of x is: %d", x);
}ผลลัพธ์
The value of x is: 50