Functions, parameters, and return values · ฟังก์ชัน, พารามิเตอร์, และค่าที่ส่งกลับ
Why functions
- A function is a named piece of work. You write it once and call it whenever you need it.
- Functions keep programs short and clear, and let you build big jobs from small ones.
- You have already used
printf— a function from<stdio.h>. Now you write your own.
เหตุผลที่ต้องใช้ฟังก์ชัน
- ฟังก์ชัน คือชุดคำสั่งที่มีชื่อ คุณสามารถเขียนไว้หนึ่งครั้งแล้ว เรียกใช้ เมื่อต้องการ
- ฟังก์ชันช่วยให้โปรแกรมสั้นและเข้าใจง่าย รวมถึงช่วยสร้างงานใหญ่จากส่วนย่อยๆ ได้
- คุณเคยใช้
printfไปแล้ว — ซึ่งเป็นฟังก์ชันจาก<stdio.h>ตอนนี้คุณมาเขียนของตัวเอง
Parameters and return values
- The values you pass in are the parameters. The value you hand back is the return value.
int max2(int a, int b)takes twointparameters and returns oneint.return x;ends the function right away and givesxback to the caller.
พารามิเตอร์และค่าที่ส่งกลับ
- ค่าที่คุณส่งเข้าไปคือ พารามิเตอร์ ส่วนค่าที่คุณส่งออกมาคือ ค่าที่ส่งกลับ (return value)
int max2(int a, int b)รับพารามิเตอร์intจำนวนสองตัว และส่งกลับค่าintตัวหนึ่งreturn x;จะหยุดการทำงานของฟังก์ชันทันทีและส่งxกลับไปยังผู้เรียกใช้
void functions, and building big from small
- A function that returns nothing has the return type
void. It does work but hands back no value. - One function can call another.
max3can callmax2twice to do its job. - Small functions that each do one thing are easier to read and to fix.
ฟังก์ชันที่คืนค่าเป็น void และการสร้างสิ่งใหญ่จากสิ่งเล็ก
- ฟังก์ชันที่ไม่คืนค่าอะไรเลยจะมี return type เป็น
voidมันทำงานได้แต่ไม่ส่งค่ากลับคืน - ฟังก์ชันหนึ่งสามารถเรียกอีกฟังก์ชันได้
max3สามารถเรียกmax2ได้สองครั้งเพื่อทำงานให้สำเร็จ - ฟังก์ชันเล็กๆ ที่ทำเพียงอย่างเดียวอ่านง่ายและแก้ไขได้ง่ายกว่า
C copies your arguments
- When you call a function, C gives it a copy of each argument.
- So changing a parameter inside the function does not change the caller's variable.
- Keep this in mind — in a later lesson, pointers let a function reach the original.
C คัดลอกอาร์กิวเมนต์ของคุณ
- เมื่อคุณเรียกฟังก์ชัน C จะส่ง คัดลอก ของแต่ละอาร์กิวเมนต์ให้
- ดังนั้นการเปลี่ยนแปลงพารามิเตอร์ภายในฟังก์ชันจะ ไม่ เปลี่ยนตัวแปรของผู้เรียกใช้
- จำไว้ว่า — ในบทเรียนถัดไป ** pointers (พอยเตอร์)** จะช่วยให้ฟังก์ชันเข้าถึงข้อมูลต้นฉบับได้
#include <stdio.h>
void try_double(int x) {
x = x * 2; // changes only the COPY
}
int main(void) {
int n = 5;
try_double(n);
printf("%d\n", n); // still 5 — the original did not change
return 0;
}
Common mistakes
- A function used before it is defined needs a prototype above
main. - Arguments pass by value — pass a pointer to change the caller's variable.
ข้อผิดพลาดที่พบบ่อย
- ฟังก์ชันที่ถูกเรียกใช้ก่อนการนิยามจำเป็นต้องมี prototype ด้านบน
main - อาร์กิวเมนต์ส่งด้วยค่า (pass by value) — ส่ง pointer เพื่อเปลี่ยนตัวแปรของผู้เรียกใช้
Now you try
- Write each function with the exact name and parameters shown, and
returnthe right value. - You may call one of your functions from another.
- Do not write a
main— the checker provides one.
ลองดูเลย
- เขียนฟังก์ชันแต่ละตัวตามชื่อและพารามิเตอร์ที่แสดงไว้ และ
returnค่าที่ถูกต้อง - คุณสามารถเรียกใช้ฟังก์ชันหนึ่งของคุณจากฟังก์ชันอื่นได้
- อย่า เขียน
main— ตัวตรวจสอบจะจัดเตรียมให้
Functions: call and return · ฟังก์ชัน: การเรียกใช้และการส่งกลับ
A call pushes a frame with its parameters; return pops it. · การเรียกใช้จะดันเฟรมที่มีพารามิเตอร์ของมันเข้าไป; return จะดึงออก
Write int max2(int a, int b) returning the larger of two numbers, and int max3(int a, int b, int c) returning the largest of three. Make max3 call max2. Do not write a main. · เขียน int max2(int a, int b) ที่ส่งกลับค่ามากกว่าของสองจำนวน, และ int max3(int a, int b, int c) ที่ส่งกลับค่ามากที่สุดของสามจำนวน. ทำให้ max3 เรียกใช้ max2. ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete int power(int base, int exp) so it returns base raised to exp (assume exp >= 0). Use a loop. power(2, 10) is 1024. Do not write a main. · เติมเต็ม int power(int base, int exp) ให้ส่งกลับ base ยกกำลัง exp (สมมติว่า exp >= 0). ใช้ลูป. power(2, 10) คือ 1024. ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete int clamp(int x, int lo, int hi) so it keeps x within [lo, hi]: return lo if x is below lo, hi if x is above hi, otherwise x. Do not write a main. · เติมเต็ม int clamp(int x, int lo, int hi) ให้รักษา x ไว้ภายใน [lo, hi]: ส่งกลับ lo หาก x ต่ำกว่า lo, ส่งกลับ hi หาก x สูงกว่า hi, มิฉะนั้นส่งกลับ x. ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่