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
Name a block of code
- A function is named code you can use again and again.
- Make one with
def name():and an indented body. - "Calling" the function by its name runs that code.
ไทย
ตั้งชื่อ blok โค้ด
- ฟังก์ชัน (function) คือโค้ดที่มีชื่อซึ่งคุณสามารถใช้งานซ้ำได้
- สร้างหนึ่งด้วย
def name():และ body ที่ย่อหน้า - การเรียกใช้ฟังก์ชันโดยใช้ชื่อของมัน จะทำให้โค้ดนั้น被执行。
English
Give it inputs (parameters)
- The names in the brackets are parameters — the function's inputs.
- When you call it, you pass in arguments (the real values).
- A function can take more than one parameter.
ไทย
ให้ข้อมูลเข้า (parameters)
- ชื่อในวงเล็บคือ พารามิเตอร์ — ข้อมูลเข้าของฟังก์ชัน
- เมื่อคุณเรียกมัน คุณส่ง อาร์กิวเมนต์ ( ARGUMENTS ) (ค่าจริง) เข้าไป
- ฟังก์ชันสามารถรับพารามิเตอร์มากกว่าหนึ่งตัว
def shout(word):
print(word.upper() + "!")
shout("hello")
English
Send back a result (return)
returnsends a value back to whoever called the function.- You can store that value or use it in an expression.
returnalso ends the function straight away.
ไทย
ส่งผลลัพธ์กลับ (return)
returnส่งค่า กลับ ไปยังผู้เรียกฟังก์ชัน- คุณสามารถเก็บค่านั้นไว้หรือใช้ในนิพจน์
returnยังหยุดฟังก์ชันทันที
def double(x):
return x * 2
result = double(5)
print(result)
English
return vs print
printonly shows something on the screen.returngives a value back that the rest of your code can use.- A function with no
returngives backNone.
ไทย
return vs print
printเพียงแค่ แสดง สิ่งใดสิ่งหนึ่งบนหน้าจอเท่านั้นreturnส่งค่ากลับ เพื่อให้โค้ดส่วนอื่นของคุณนำไปใช้ได้- ฟังก์ชันที่ไม่มี
returnจะส่งกลับNone
English
In Cambridge pseudocode
- A
FUNCTIONreturns a value; aPROCEDUREjust does an action.
ไทย
ใน伪代码 Cambridge
- A
FUNCTION/value; aPROCEDURE/just does an action.
FUNCTION Add(A : INTEGER, B : INTEGER) RETURNS INTEGER
RETURN A + B
ENDFUNCTION
PROCEDURE Greet(Name : STRING)
OUTPUT "Hello, ", Name
ENDPROCEDURE
English
Common mistakes
- A function returns
Noneunless you writereturn; printing is not the same as returning. - Run a function with
():greet(), notgreet. - Define a function before you call it.
ไทย
ข้อผิดพลาดที่พบบ่อย
- ฟังก์ชันจะส่งกลับ
Noneเว้นแต่คุณจะเขียนreturn; การพิมพ์ไม่ใช่การส่งกลับ - รันฟังก์ชันด้วย
():greet(), ไม่ใช่greet - กำหนดฟังก์ชันก่อนที่คุณจะเรียกมัน
English
Now you try
- Define each function, then the checks will call it for you.
- Use
returnto send the answer back (do notprintit).
ไทย
ลองดูเลย
- กำหนดฟังก์ชันแต่ละตัว แล้วการตรวจสอบจะเรียก它为为你
- ใช้
returnเพื่อส่งคำตอบกลับ (อย่าprint与之)
Explore · สำรวจ
Calling a function
A call pushes a frame with its arguments; return pops it.
Write a function add(a, b) that returns the sum of a and b.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write a function greet(name) that returns the string Hello, then the name then !. So greet("Sam") returns Hello, Sam!.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write a function biggest(a, b) that returns the larger of the two values. If they are equal, return either one.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write is_palindrome(word) that returns True when the word reads the same backwards (like racecar), otherwise False.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่