Prototype มาตรฐานของ main() ใน C++
ฟังก์ชัน main() เป็น global function ใช้เริ่มการทำงานของโปรแกรม ทุกโปรแกรมต้องมี main() อาร์กิวเมนต์ command line argc และ argv เป็น optional
Prototype มาตรฐาน
int main() { body }
หรือ
int main(int argc, char *argv[]) { body }
- argc: จำนวนอาร์กิวเมนต์ที่ส่งเข้ามา
- argv: pointer ไปยัง element แรกของอาเรย์
ตัวอย่าง
#include <iostream>
using namespace std;
int sum(int x, int y) {
int s = x + y;
cout << "The sum of numbers: " << s;
}
int main() {
sum(28, 8);
return 0;
}
ผลลัพธ์
The sum of numbers: 36
ในตัวอย่าง โค้ดคำนวณผลรวมอยู่ใน sum() และ main() เรียกใช้ sum(28, 8)