Control passing to a subroutine and back

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Control passing to a subroutine and back

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. After a subroutine finishes its last statement, where does the program continue?
A On the line that follows the call
B At the very first line of the program
2. Which statement is true when CALL ShowMenu() runs?
A The main program skips ShowMenu and carries on
B Control passes to ShowMenu and its statements run in order
3. The main program is: 1 OUTPUT "A" / 2 CALL Show() / 3 OUTPUT "C". Show() outputs "B". What is the output order?
A A, C, B
B A, B, C
4. Which line runs straight after Greet() finishes? 1 CALL Greet() / 2 OUTPUT "Done"
A Line 2, OUTPUT "Done"
B Line 1, CALL Greet() again
5. Procedure A contains the statement CALL B(). When B finishes, where does control go?
A Back to the line in A after CALL B()
B Straight back to the main program

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1CALL ShowMenu() APROCEDURE Start() CALL ShowMenu() OUTPUT "Done" ENDPROCEDURE
2ENDPROCEDURE BControl passes to ShowMenu, which runs its statements in order, then control returns to the next line
3One procedure can call another, and control returns to the first procedure when the second one ends CThe statement after the call does not run first. Control passes into ShowMenu, and the next line runs only after ShowMenu ends.
4After a subroutine ends, the program continues with the line that follows the call DCALL ShowMenu() OUTPUT "Menu closed"
5CALL ShowMenu() OUTPUT "Menu closed" // Output order: Menu closed, then the menu lines EThe last line of the procedure: control returns to the line after the CALL
6// After ENDPROCEDURE, the program stops CALL ShowMenu() OUTPUT "Menu closed" FThe program does not stop at ENDPROCEDURE. Control returns to the line after the CALL and the program carries on.

Definitions

CALL ShowMenu()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
____ ShowMenu() OUTPUT "Done"
2.
PROCEDURE ShowMenu() OUTPUT "Menu" ____________
3.
PROCEDURE Start() ____ Setup() ENDPROCEDURE
4.
OUTPUT "Start" CALL Show() ______ "End"
5.
_________ ShowMenu() OUTPUT "Menu" ENDPROCEDURE

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A OUTPUT "Start"
B CALL ShowMenu
C OUTPUT "End"
Question 2
A OUTPUT "Start"
B RUN Menu()
C OUTPUT "End"
Question 3
A PROCEDURE ShowMenu()
B OUTPUT "1 Play"
C ENDPROCEDURE
D CALL ShowMeni()
Question 4
A PROCEDURE Hello()
B OUTPUT "Hi"
C ENDPROCEDURE
D OUTPUT Hello()
E OUTPUT "Done"
Question 5
A PROCEDURE Inner()
B OUTPUT "Inner"
C ENDFUNCTION
D PROCEDURE Outer()
E CALL Inner()
F ENDPROCEDURE

Answer Key

Control passing to a subroutine and back
Section A: Quick Questions
Q1
A — On the line that follows the call
Q2
B — Control passes to ShowMenu and its statements run in order
Q3
B — A, B, C
Q4
A — Line 2, OUTPUT "Done"
Q5
A — Back to the line in A after CALL B()
Section B: Match the Pairs
Q1
CALL ShowMenu() → Control passes to ShowMenu, which runs its statements in order, then control returns to the next line
Q2
ENDPROCEDURE → The last line of the procedure: control returns to the line after the CALL
Q3
One procedure can call another, and control returns to the first procedure when the second one ends → PROCEDURE Start() CALL ShowMenu() OUTPUT "Done" ENDPROCEDURE
Q4
After a subroutine ends, the program continues with the line that follows the call → CALL ShowMenu() OUTPUT "Menu closed"
Q5
CALL ShowMenu() OUTPUT "Menu closed" // Output order: Menu closed, then the menu lines → The statement after the call does not run first. Control passes into ShowMenu, and the next line runs only after ShowMenu ends.
Q6
// After ENDPROCEDURE, the program stops CALL ShowMenu() OUTPUT "Menu closed" → The program does not stop at ENDPROCEDURE. Control returns to the line after the CALL and the program carries on.
Section C: Definitions
Q1
CALL ShowMenu() — Control passes to the subroutine, which runs its statements in order. After its last statement, control returns to the line that follows the call.
Section D: Fill in the Blanks
Q1
CALL ShowMenu() OUTPUT "Done"
Q2
PROCEDURE ShowMenu() OUTPUT "Menu" ENDPROCEDURE
Q3
PROCEDURE Start() CALL Setup() ENDPROCEDURE
Q4
OUTPUT "Start" CALL Show() OUTPUT "End"
Q5
PROCEDURE ShowMenu() OUTPUT "Menu" ENDPROCEDURE
Section E: Spot the Error
Q1
B — A call needs brackets after the subroutine name. It should be CALL ShowMenu() so control passes to ShowMenu and then returns to the next line.
Q2
B — RUN is not the keyword for calling a procedure. It should be CALL Menu(). After Menu finishes, control returns to OUTPUT "End".
Q3
D — The name in the call must match the procedure name exactly. ShowMeni does not exist, so control cannot pass to ShowMenu. It should be CALL ShowMenu().
Q4
D — Hello is a procedure, so it does not give back a value to output. It must be called with CALL Hello(). Then control returns to OUTPUT "Done".
Q5
C — A procedure must finish with ENDPROCEDURE, not ENDFUNCTION. ENDPROCEDURE marks the end where control returns to the line after the call.