Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

Functions ใน C/C++ (3.5)


Functions เหมือนเครื่องจักรที่ทำฟังก์ชันบางอย่างและผลิตผลลัพธ์บางประเภท เหมือนเครื่องจักรที่รับอินพุต ประมวลผล แล้วผลิตเอาต์พุต ฟังก์ชันก็รับค่า เข้ามาดำเนินการกับค่าเหล่านั้น แล้วผลิตเอาต์พุท โปรแกรมเมอร์ต้องเรียกฟังก์ชันถึงจะเริ่มทำงาน

Functions อาจมีชื่อต่างกันในภาษาต่างๆ แต่มีลักษณะร่วม 2 จุดคือ

  • มีลำดับคำสั่งที่ต้องประมวลผล

  • คำสั่งเหล่านั้นถูกระบุโดยชื่อ ซึ่งอ้างอิงถึงฟังก์ชัน

ทำไมต้องใช้ Functions?

  • Reusability − เมื่อต้องการฟังก์ชันเดียวกันหลายที่ ดีกว่าคือสร้างฟังก์ชันครั้งเดียว เรียกหลายครั้ง แทนการประกาศซ้ำๆ Reusability เป็นฟีเจอร์/ข้อได้เปรียบใหญ่ที่สุดของฟังก์ชัน

  • Modularity to code − ฟังก์ชันทำให้โค้ดสะอาด อ่านง่าย แทนที่จะเขียนหลายบรรทัดใน main() ให้ประกาศฟังก์ชันแทน

  • Ease in modification − ถ้ามีการเปลี่ยนแปลงในอนาคต แก้ที่ฟังก์ชันเดียว แทนที่จะแก้หลายที่ ลด redundancy

  • Provides Abstraction − จากชื่อฟังก์ชันที่เกี่ยวข้อง สามารถทราบได้ว่าฟังก์ชันนั้นทำอะไร โดยไม่ต้องรู้ว่าทำอย่างไร ตัวอย่างใน C มี maths.h มี pow() ใช้คำนวณเลขยกกำลังโดยไม่ต้องรู้รายละเอียด

Function Declaration และ Definition

 Function declaration คือกระบวนการบอก compiler ว่า return type และชื่อฟังก์ชันคืออะไร

Syntax

 Without body

Return_type function_name(parameter list which is optional);

   With body

Return_type function_name(parameter list which is optional) { //body of the function } 

Explanation

  • return_type − บอก compiler ว่าจะคืนค่าอะไร ถ้า void คือไม่คืนค่า ถ้า int คือคืน integer ต้องมี return statement

void dummy(){ 
    //since the return type of function is void it won't return anything to the caller and hence it won't contain return statement. 
} 
Int dummy(){ 
    //since the return type of function is int it will return integer value to the caller and it is mandatory that it will contain return statement as it is returning integer value. 
    return integer_value; 
} 
float dummy(){ 
    //since the return type of function is float it will return floating value to the caller and it is mandatory that it will contain return statement as it is returning floating value. 
    return float_value; 
}

  

  • function name − ชื่อฟังก์ชันตั้งได้ตามต้องการ

  • parameters list(optional) − เมื่อฟังก์ชันต้องดำเนินการกับค่าที่ส่งมา ต้องสร้าง parameters

Function definition  ประกอบด้วยฟังก์ชันการทำงานที่ฟังก์ชันควรทำเมื่อถูกเรียก

ตัวอย่าง

 

#include<iostream>
using namespace std;
//function that calculates the greatest value amongst two parameters
int greatest(int val_1, int val_2) //return type of function is integer value{
    //body of the function(Definition)
    if(val_1>val_2){
        return val_1;
    }
    else{
        return val_2;
    }
}
int main(){
    int val_1=10, val_2=20;
    //calling the function and storing the integer value returned by a function in the integer variable
    int highest = greatest(val_1,val_2);
    //printing the greatest value
    cout<<"The greatest value is : "<<highest;
    //as the return type of main is int, it must have return statement for the compiler
    return 0;
} 

ผลลัพธ์

The output for the above code will be −

The greatest value is : 20

Function Parameters

   

  • Parameters เป็น optional ถ้าไม่มี parameter ฟังก์ชันก็ยังทำงานได้

  • Variables ที่ประกาศใน function definition เพื่อรับค่าที่ caller ส่งมา เรียกว่า Parameters. int greatest(int val_1, int val_2)

int greatest(int val_1, int val_2)

    Functions ใน C/C++ (3.5)

  • Variables ที่ caller ส่งมา เรียกว่า Arguments.

int highest = greatest(val_1,val_2);

 Functions ใน C/C++ (3.5)


  • Actual Parameters vs Formal Parameters-

    Actual parameters คือข้อมูลที่ส่งให้ฟังก์ชัน เช่น ตัวอย่าง 10 และ 20 คือ actual parameters

    Formal parameters  คือข้อมูลที่ฟังก์ชันรับมา เช่น ตัวอย่าง val_1 และ val_2 คือ formal parameters.

จุดสำคัญเกี่ยวกับฟังก์ชัน main()

  • ทุกโปรแกรมมี entry point ที่เริ่มต้นการทำงาน เช่น ใน C และ C++ มีฟังก์ชัน main()

  • ถ้า main() return type เป็น void หมายถึงไม่คืนค่าให้ compiler ถ้าเป็น int คือคืนค่า เช่น "return 0" แสดงจบโปรแกรม

  • ใน C − return type ของ main() เป็น void และ int เท่านั้น

  • ใน C++ − return type ของ main() เป็น int เท่านั้น