Checkpoint 24.1.1.
What is the output of
countdown(3) in ListingΒ 24.1.2 ?
- 3 2 1 Blastoff!
- 1 2 3 Blastoff!
- Blastoff! 3 2 1
- Blastoff! 1 2 3
countdown. If it is called with an n value of 0, it displays the word Blastoff!. Otherwise, it displays the number and then makes a recursive call to countdown, passing n - 1 as the argument.
countdown(3) from main?countdown begins with n == 3, and since n is not 0, it displays the value 3, and then invokes countdown(2)
countdown(2) begins, and since n is not 0, it displays the value 2, and then invokes countdown(1)
countdown(1) begins, and since n is not 0, it displays the value 1, and then invokes countdown(0)
countdown(0) begins, and since n is 0, it displays the word Blastoff!.countdown(3) in ListingΒ 24.1.2 ?
countdown prints both before and after the recursive call.
countdown(3) from main?countdown begins with n == 3, and since n is not 0, it displays the start message, and then invokes countdown(2)
countdown(2) begins, and since n is not 0, it displays the start message, and then invokes countdown(1)
countdown(1) begins, and since n is not 0, it displays the start message, and then invokes countdown(0)
countdown(0) begins, and since n is 0, it displays the word Blastoff!. It returns to where it was called (countdown(1)).countdown(1) execution resumes after the recursive call and displays the finish message. It then returns to where it was called (countdown(2)).countdown(2) execution resumes after the recursive call and displays the finish message. It then returns to where it was called (countdown(3)).countdown(3) execution resumes after the recursive call and displays the finish message. It returns to where it was called (main).