Quiz — Chapter 23: Parameter Passing Patterns
Multiple Choice
1. What happens when a parameter is passed BY REFERENCE?
A) A copy of the data is created and its address is passed B) The actual value is placed on the call stack C) The address of the caller's data item is passed directly D) The data is serialized and deserialized
2. Which parameter passing mode protects the caller's data from modification by the called program?
A) BY REFERENCE B) BY CONTENT C) BY VALUE D) Both B and C
3. What is the primary use case for BY VALUE parameter passing in COBOL?
A) Passing large group items efficiently B) Interoperability with C and other non-COBOL languages C) Protecting sensitive financial data D) Passing parameters to nested programs
4. Which of the following can be passed BY CONTENT but NOT BY VALUE?
A) PIC S9(9) COMP B) PIC X(50) (a 50-byte alphanumeric field) C) COMP-1 (single-precision float) D) USAGE POINTER
5. What does the OMITTED keyword do when used in place of a parameter?
A) Sets the parameter to spaces B) Sets the parameter to zeros C) Passes a null address for that parameter position D) Skips that parameter position entirely
6. In the standard IBM return code convention, what does return code 4 mean?
A) Success B) Warning — result valid but a notable condition exists C) Error — result is invalid D) Fatal — processing cannot continue
7. Why are IBM standard return codes multiples of 4?
A) COBOL COMP fields are 4 bytes B) It allows for future return codes between values C) Historical: assembler branch table entries are 4 bytes each D) It aligns with VSAM status codes
8. What is a COMMAREA in the context of COBOL program communication?
A) A comment area in the source code B) A single, comprehensive data structure carrying all information between programs C) A shared file used for inter-program communication D) A special register for storing return codes
9. What happens if a called program modifies a parameter that was passed BY CONTENT?
A) The caller's original data is modified B) A runtime error occurs C) Only the temporary copy is modified; the caller's data is unchanged D) The modification is ignored
10. When passing a literal value on a CALL statement, which passing mode must you use?
A) BY REFERENCE B) BY CONTENT C) BY VALUE D) Either BY CONTENT or BY VALUE, depending on the data type
True or False
11. BY REFERENCE is the default parameter passing mode in COBOL when no mode is specified.
12. A parameter passed BY VALUE can be a group item containing multiple fields.
13. The called program can distinguish between BY REFERENCE and BY CONTENT at runtime.
14. Return code 0 always means success in the IBM standard convention.
15. The RETURNING clause on CALL is commonly used to receive return codes from COBOL subprograms.
Short Answer
16. A developer passes an account number BY REFERENCE to a validation routine. The routine has a bug that moves spaces to the account number field when validation fails. After the call, the caller's account number is now spaces. How would you fix this using parameter passing modes? What is the trade-off?
17. Explain the difference between these two calls:
CALL 'CALC' USING BY CONTENT 100
CALL 'CALC' USING BY VALUE 100
18. Design a return code handling pattern for a batch job that processes 10,000 records. Some records may have warnings (RC=4) and some may have errors (RC=8). The job should continue processing all records but set the overall job return code to the highest severity encountered. Write the COBOL pseudocode.
19. When would you choose a COMMAREA (single structure) over separate parameters? When would separate parameters be better? Give a specific example for each.
20. A subprogram receives a table of 50 entries. The calling program only populated 23 entries. What pattern should you use to ensure the subprogram processes only the populated entries and does not access uninitialized storage?
Answer Key
1. C — The address of the caller's data item is passed directly.
2. D — Both BY CONTENT and BY VALUE protect the caller's data. BY CONTENT creates a copy; BY VALUE passes the value itself.
3. B — BY VALUE is primarily used for interoperability with C and other languages that use value-passing calling conventions.
4. B — Group items and large alphanumeric fields can be passed BY CONTENT but not BY VALUE. BY VALUE is restricted to elementary items that fit in registers.
5. C — OMITTED passes a null address for that parameter position. The called program must check for this before accessing the parameter.
6. B — Return code 4 means warning: the result is valid but a notable condition exists.
7. C — Historical: in assembler, return codes were used as branch table offsets, and each branch instruction is 4 bytes.
8. B — A COMMAREA is a single, comprehensive data structure carrying all information between programs, commonly used in CICS and adapted for batch.
9. C — Only the temporary copy is modified. The caller's data is unchanged because BY CONTENT passes the address of a copy.
10. D — Literals can be passed BY CONTENT (any type) or BY VALUE (if the literal matches a BY VALUE-compatible type). BY REFERENCE requires an addressable data item.
11. True — BY REFERENCE is the default when no mode is specified.
12. False — BY VALUE cannot be used with group items. Only elementary items of specific types are allowed.
13. False — The called program cannot distinguish the passing mode at runtime. Both BY REFERENCE and BY CONTENT provide an address; the difference is whether the address points to original or copied data.
14. True — Return code 0 always means success.
15. False — RETURNING is primarily used for non-COBOL (C) function return values. COBOL subprograms typically communicate results through BY REFERENCE parameters.
16. Change the CALL to pass the account number BY CONTENT: CALL 'VALIDATE' USING BY CONTENT WS-ACCOUNT-NUMBER .... This protects the caller's copy. The trade-off is that if the validation routine legitimately needs to modify the parameter (e.g., to normalize the format), it can no longer do so — you would need a separate output parameter.
17. BY CONTENT 100 creates a temporary data item containing 100 and passes its address. The called program receives a pointer to memory containing the value. BY VALUE 100 places the value 100 directly on the call stack (in a register). The called program receives the value, not a pointer. In the LINKAGE SECTION, BY CONTENT requires a normal declaration; BY VALUE requires PROCEDURE DIVISION USING BY VALUE LS-PARAM.
18.
01 WS-MAX-RC PIC S9(4) COMP VALUE 0.
01 WS-RECORD-RC PIC S9(4) COMP.
01 WS-RECORDS-OK PIC 9(7) VALUE 0.
01 WS-RECORDS-WARN PIC 9(7) VALUE 0.
01 WS-RECORDS-ERR PIC 9(7) VALUE 0.
PERFORM UNTIL END-OF-FILE
READ INPUT-FILE
CALL 'PROCESS' USING WS-RECORD WS-RECORD-RC
EVALUATE TRUE
WHEN WS-RECORD-RC = 0
ADD 1 TO WS-RECORDS-OK
WHEN WS-RECORD-RC = 4
ADD 1 TO WS-RECORDS-WARN
WHEN WS-RECORD-RC >= 8
ADD 1 TO WS-RECORDS-ERR
END-EVALUATE
IF WS-RECORD-RC > WS-MAX-RC
MOVE WS-RECORD-RC TO WS-MAX-RC
END-IF
END-PERFORM
MOVE WS-MAX-RC TO RETURN-CODE
19. Use a COMMAREA for tightly coupled programs in a pipeline (e.g., MedClaim's adjudication stages) where all programs work on the same claim data. Use separate parameters for loosely coupled utility programs (e.g., date validation) that serve many different callers with different data contexts — a date validation routine should not require callers to populate a massive structure just to validate one date.
20. Pass the actual count as a separate parameter (the length fields pattern): pass the count (23) BY CONTENT and the table BY REFERENCE. The subprogram validates the count, then uses PERFORM VARYING idx FROM 1 BY 1 UNTIL idx > LS-ACTUAL-COUNT to process only the populated entries.