Podcast
Questions and Answers
A subprogram can have multiple entry points.
A subprogram can have multiple entry points.
False
The caller remains active during the execution of the called subprogram.
The caller remains active during the execution of the called subprogram.
False
Control returns to the caller when the execution of the called subprogram is complete.
Control returns to the caller when the execution of the called subprogram is complete.
True
The two fundamental types of subprograms are classes and methods.
The two fundamental types of subprograms are classes and methods.
Signup and view all the answers
Parameters passed to a subprogram are accessed through names that are local to the subprogram.
Parameters passed to a subprogram are accessed through names that are local to the subprogram.
Signup and view all the answers
Extensive access to non-local variables in a subprogram can enhance reliability.
Extensive access to non-local variables in a subprogram can enhance reliability.
Signup and view all the answers
A subprogram header always specifies a list of parameters.
A subprogram header always specifies a list of parameters.
Signup and view all the answers
Coroutines are an example of a subprogram type.
Coroutines are an example of a subprogram type.
Signup and view all the answers
A procedure can modify variables that are not formal parameters but are visible in both itself and the calling program.
A procedure can modify variables that are not formal parameters but are visible in both itself and the calling program.
Signup and view all the answers
Functions are designed to produce side effects and modify their parameters.
Functions are designed to produce side effects and modify their parameters.
Signup and view all the answers
Local variables defined within a subprogram can either be static or stack dynamic.
Local variables defined within a subprogram can either be static or stack dynamic.
Signup and view all the answers
One disadvantage of stack dynamic local variables is that they can retain data values between calls.
One disadvantage of stack dynamic local variables is that they can retain data values between calls.
Signup and view all the answers
Static local variables allow a subprogram to be history sensitive, retaining their values across calls.
Static local variables allow a subprogram to be history sensitive, retaining their values across calls.
Signup and view all the answers
All parameters for subprograms must be checked for their types.
All parameters for subprograms must be checked for their types.
Signup and view all the answers
Subprograms in programming can appear inside other subprograms.
Subprograms in programming can appear inside other subprograms.
Signup and view all the answers
Overloading of subprograms is not allowed in programming languages.
Overloading of subprograms is not allowed in programming languages.
Signup and view all the answers
What is a defining characteristic of a subprogram?
What is a defining characteristic of a subprogram?
Signup and view all the answers
Which of the following best describes the purpose of a subprogram header?
Which of the following best describes the purpose of a subprogram header?
Signup and view all the answers
Which method allows access to variables defined outside a subprogram?
Which method allows access to variables defined outside a subprogram?
Signup and view all the answers
Which type of subprogram specifically returns a value to the caller?
Which type of subprogram specifically returns a value to the caller?
Signup and view all the answers
Accessing data through parameters in a subprogram is considered to be more flexible compared to which method?
Accessing data through parameters in a subprogram is considered to be more flexible compared to which method?
Signup and view all the answers
What is a potential downside of extensive access to non-local variables in a subprogram?
What is a potential downside of extensive access to non-local variables in a subprogram?
Signup and view all the answers
Which of the following statements is incorrect regarding subprograms?
Which of the following statements is incorrect regarding subprograms?
Signup and view all the answers
What characteristic of a procedure differentiates it from a function?
What characteristic of a procedure differentiates it from a function?
Signup and view all the answers
What distinguishes a function from a procedure?
What distinguishes a function from a procedure?
Signup and view all the answers
What is an advantage of using stack dynamic local variables?
What is an advantage of using stack dynamic local variables?
Signup and view all the answers
Which statement is true about parameters in subprograms?
Which statement is true about parameters in subprograms?
Signup and view all the answers
What is a disadvantage of static local variables?
What is a disadvantage of static local variables?
Signup and view all the answers
Which question is NOT among the design issues for subprograms?
Which question is NOT among the design issues for subprograms?
Signup and view all the answers
How may procedures produce results in the calling program unit?
How may procedures produce results in the calling program unit?
Signup and view all the answers
What is a characteristic of local variables defined inside subprograms?
What is a characteristic of local variables defined inside subprograms?
Signup and view all the answers
What is one of the disadvantages of using stack dynamic local variables?
What is one of the disadvantages of using stack dynamic local variables?
Signup and view all the answers
Study Notes
Chapter 9: Subprograms
- Subprograms are fundamental building blocks of programs, crucial for code reuse and efficiency.
- Reuse saves memory and coding time.
- Subprograms have a single entry point.
- The calling subprogram is suspended during the called subprogram's execution, ensuring only one subprogram runs at a time.
- Control returns to the caller after the called subprogram finishes.
Fundamentals of Subprograms
- Subprograms typically describe computations, accessed in two ways.
- Direct access to non-local variables: The subprogram directly uses variables from the surrounding program. Changes require reassignment. Overuse can reduce reliability.
- Parameter passing: The subprogram accesses data using parameters. Data is passed by name, improving flexibility. These parameters are categorized by how data flows during the call..
Basic Definitions
- A subprogram definition describes actions, and a subprogram call requests execution.
- A subprogram is active from call to completion.
- Two main types of subprograms exist: procedures and functions.
- A subprogram header is the first line of the definition and specifies the subprogram type, name, and parameters.
Procedures and Functions
- Procedures are user-defined, parameterized, computational statements.
- Procedures can change variables in the calling program, either directly or via parameters.
- Functions model mathematical functions. They should not have side effects (changing values outside their scope). Functions return a value.
Design Issues for Subprograms
- Subprogram parameter checking: Does the program verify parameter types?
- Local variable type: Are local variables static or dynamic?
- Parameter passing methods: How are parameters passed (value, reference)? Different parameter passing methods exist for data transfer.
- Subprogram nesting: Can subprograms be defined within other subprograms?
- Referencing environment of passed subprograms
- Overloading: Can subprograms with the same name but different parameters exist?
- Genericity: Can subprograms be generic (work with different data types)?
Local Referencing Environments
- Local variables are defined within a subprogram.
- Static local variables are allocated once and exist throughout the program.
- Stack dynamic local variables are allocated on the stack during subprogram execution and deallocated when execution finishes, supporting recursion and sharing storage.
Parameter Passing Methods
- Parameter passing methods influence how data is exchanged between subprograms.
- In mode: Data flows from actual to formal parameters.
- Out mode: Data flows from formal to actual parameters. (Parameters pass data to the caller)
- Inout mode: Data can flow both ways. (Parameters pass data both ways).
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore the vital concept of subprograms in programming through this quiz. Understand their significance in code reuse, execution control, and data handling via parameter passing. Test your knowledge on definitions and various aspects of subprograms as fundamental building blocks of effective programming.