Functions · ฟังก์ชัน
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
A function returns one value
- A function is like a procedure, and it also hands one value back.
- The definition says the type of that value:
RETURNS INTEGER. RETURNsets the value. It is normally the last line of the function.
ไทย
Function ส่งคืนค่าหนึ่งค่า
- Function คล้ายกับ procedure และ它还ส่งคืนค่าหนึ่งค่า
- การนิยามระบุว่าชนิดของค่านั้น:
RETURNS INTEGER RETURNตั้งค่า. โดยปกติจะเป็นบรรทัดสุดท้ายของ function
FUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTION
OUTPUT Square(6)
English
Do not write CALL
- A function is used inside an expression, where its returned value is needed.
CALLis only for a procedure.- Parameters of a function are passed by value. The guide says not to use
BYREFon a function.
ไทย
ไม่ต้องเขียน CALL
- Function ถูกใช้ใน expression ที่ต้องการค่าที่ถูกส่งคืน
CALLใช้ได้เฉพาะกับ procedure เท่านั้น- Parameters ของ function ถูกส่ง by value. คู่มือระบุว่าห้ามใช้
BYREFกับ function
FUNCTION Max(Number1 : INTEGER, Number2 : INTEGER) RETURNS INTEGER
IF Number1 > Number2 THEN
RETURN Number1
ELSE
RETURN Number2
ENDIF
ENDFUNCTION
OUTPUT Max(10, 24)
English
Common mistakes
RETURNbelongs inside the function. Outside it, there is nothing to return to.- The call
Square(6)is not a complete statement on its own.OUTPUT Square(6)is.
ไทย
ข้อผิดพลาดที่พบบ่อย
RETURNอยู่ภายใน function. ข้างนอกไม่มีอะไรให้ส่งคืน- การเรียกใช้
Square(6)ไม่ใช่นำ statement ที่สมบูรณ์โดยตัวเอง.OUTPUT Square(6)เป็น
English
Now you try
- Write a function and use its result in
OUTPUT.
ไทย
ลองดูเลย
- เขียนฟังก์ชันและนำผลลัพธ์ไปใช้ใน
OUTPUT
Write Square so that OUTPUT Square(6) prints 36. · เขียน Square เพื่อให้ OUTPUT Square(6) พิมพ์ 36
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write Max to return the larger of two integers. OUTPUT Max(10, 24) should print 24. · เขียน Max เพื่อคืนค่าของจำนวนเต็มสองตัวใดตัวหนึ่งที่มีค่ามากกว่า OUTPUT Max(10, 24) ควรพิมพ์ 24
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่