โค้ดนี้เป็นโค้ดของภาษา java นะครับ
public static void cal1 (int n) {
if (n == 0) return;
system.out.println(n);
cal1(n-1);
system.out.println(")" + n);
}

โดยส่งค่าไปที่ cal1(3) ครับและสั่งให้แสดงผล
ผลที่ได้จากการแสดงก็คือ
3
2
1
)1
)2
)3
อยากรบกวนช่วยอธิบายการทำงานของฟังก์ชั่นนี้หน่อย
ครับ ทำไมพอ return แล้วถึงกลับมาเป็น
)1
)2
)3
อีกรอบนึง

อีกอันนึงนะครับ
public static void main(string[]args){
system.out.println(sum(10));
}
public static void sum5 (int n) {
if (n == 0) return 0;
return sum5(n - 1)+n
}
แล้วได้ค่าเป็น 55 ครับอยากทราบว่าการทำงานของฟังก์ชั่นนี้นำค่า n ไปเก็บเซฟไว้ที่ตัวแปรไหนครับในส่วนของ
การเรียกใช้ฟังก์ชั่นครับทำไมจู่ๆค่าที่ได้ถึงเป็น 55 ตอนนี้งงจริงๆ จนปัญญาแล้วครับ
ขอบคุณล่วงหน้าสำหรับทุกคำตอบครับ

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

มันเป็น method แบบ recursive มันจะต้องทำงานไปเรื่อยๆ จนกว่าจะจบครับ แล้วมันถึงจะ return ย้อนกลับ
ตัวอย่าง sum(3) ก็พอน๊อ เดียวยาว
sum(3) n > 0, จากนั้น return sum(2) + 3 << ตัวนี้จะต้องเป็นตัวที่ return แต่ๆ sum(2) ยังไม่ถูกแก้เพราะฉนั้น มันจะทดไว้ก่อนใน stack ไปเรียก
sum(2), n > 0 จากนั้น return sum(1) + 2 << เหมือนเดิม ต้องแก้ sum(1) ก่อน ยัดไว้ใน stack อีกตามเคย ไปเรียก
sum(1), n > 0, จากนั้น return sum(0) + 1 << อีกตามเคย sum(0) มันคืออะไรๆๆ เอาไปฝากไว้กับเธอ
sum(0),n = 0 ก็ return 0 สินะ
เสร็จก็ไปเอา ที่ฝากไว้กลับมา sum(0) + 1 = 0 +1 = 1 จบ return 1 ไป
ไปเรียกตัวต่อไปอีก sum(1)+2 = 1 + 2 = 3 จบๆๆ return 3
หมดยัง ไม่หมดจัดต่อ sum(2) +3 = 3+3 = 6 return 6
หมดยัง หมดแล้วนิ ^3^ จบข่าว return สุดท้าย คือ 6

ช้อด้านบนก็เหมือนกันครับ ต้องหัดไล่ไปทีละ step
** recursive อาจจะดูยากๆ แต่พอเข้าใจแล้วจะสนุกสนาน แก้ปัญหา ได้หลายๆอย่าง

อยากรบกวนช่วยอธิบายการทำงานของฟังก์ชั่นนี้หน่อย
ครับ ทำไมพอ return แล้วถึงกลับมาเป็น
)1
)2
)3
อีกรอบนึง

เพราะมันติดทำงาน cal1(n-1); อยู่
มันจะ exec system.out.println(")" + n); หลังจากที่ return ออกจาก cal1(n-1); แล้ว ตามด้านบนอธิบาย

แนะนำให้ set breakpoint แล้ว debug step ๆ ทีละบันทัดดูครับ

[Q]อยากทราบว่าการทำงานของฟังก์ชั่นนี้นำค่า n ไปเก็บเซฟไว้ที่ตัวแปรไหนครับในส่วนของ
การเรียกใช้ฟังก์ชั่นครับ
[A]ค่าที่ได้จะเก็บไว้ใน stack ,ควรระวังในการเรียกใช้ recursive function เพราะออกเกิด stack overflow ได้

cal1(n-1);
บรรทัดนี้ค่า n ถูกหักลบแล้วส่งเข้าไป

แต่ไม่ได้หมายความว่าค่า n มันเปลี่ยนไป ดังนั้น
system.out.println(")" + n);

จะยังมีค่า n เท่าเดิมในตอนส่งเข้ามา

พูดง่ายๆ โค้ดทำให้มองว่าค่า n ถูกลดทีล่ะ 1 (ถ้าอ่านด้วยตาจะโดนหลอก)

ในความเป็นจริงค่า n ไม่ได้ลดลงเลย แต่ทุกครั้งที่มีการเรียก cal1 ซ้อนเข้าไป ตัวแปรใหม่ที่ถูกส่งเข้าไปมันจะน้อยกว่า n ลงไปทีล่ะ 1 และโค้ดจะหยุดเมื่อค่าที่รับเข้ามากลายเป็น 0

จากนั้นมันก็เริ่มคืนค่า และทำการแสดง )n ออกมา ซึ่งมันจะเหมือนทำย้อนกลับ มันเลยแสดง )1 )2 )3 ครับ

ถ้าไม่ debug เป็นระยะ ก็แนะนำให้เขียน flow ลงกระดาษดูครับ

ส่วนโจทย์สอง อันนี้คือมันทำงานแบบ recursive แบบเหมือนข้อแรกนั้นล่ะ

ค่าส่ง 10 เข้าไป มันจะโดนหักลบ 1 แล้ว recursive ไปเรื่อยๆ จบกระทั่งในรอบที่ 11 (10 ถึง 0 โดยทำงาน 11 ครั้ง) ค่าจะกลายเป็น 0 แล้วถูก return กลับมา

ทุกๆ รอบ return กลับมาจะถูกบวกด้วย 5 เมื่อคืนพ้น recursive ครบจำนวน 11 ครั้ง มันก็เลยออกเป็น 55 ครับ

เก็ทไหม?