Programming Chapter 9: Subprograms
32 Questions
3 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

A subprogram can have multiple entry points.

False

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.

True

The two fundamental types of subprograms are classes and methods.

<p>False</p> Signup and view all the answers

Parameters passed to a subprogram are accessed through names that are local to the subprogram.

<p>True</p> Signup and view all the answers

Extensive access to non-local variables in a subprogram can enhance reliability.

<p>False</p> Signup and view all the answers

A subprogram header always specifies a list of parameters.

<p>False</p> Signup and view all the answers

Coroutines are an example of a subprogram type.

<p>True</p> 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.

<p>True</p> Signup and view all the answers

Functions are designed to produce side effects and modify their parameters.

<p>False</p> Signup and view all the answers

Local variables defined within a subprogram can either be static or stack dynamic.

<p>True</p> Signup and view all the answers

One disadvantage of stack dynamic local variables is that they can retain data values between calls.

<p>False</p> Signup and view all the answers

Static local variables allow a subprogram to be history sensitive, retaining their values across calls.

<p>True</p> Signup and view all the answers

All parameters for subprograms must be checked for their types.

<p>False</p> Signup and view all the answers

Subprograms in programming can appear inside other subprograms.

<p>True</p> Signup and view all the answers

Overloading of subprograms is not allowed in programming languages.

<p>False</p> Signup and view all the answers

What is a defining characteristic of a subprogram?

<p>It has a single entry point.</p> Signup and view all the answers

Which of the following best describes the purpose of a subprogram header?

<p>To specify the name and type of the subprogram.</p> Signup and view all the answers

Which method allows access to variables defined outside a subprogram?

<p>Direct access to non-local variables.</p> Signup and view all the answers

Which type of subprogram specifically returns a value to the caller?

<p>Function.</p> Signup and view all the answers

Accessing data through parameters in a subprogram is considered to be more flexible compared to which method?

<p>Direct access to non-local variables.</p> Signup and view all the answers

What is a potential downside of extensive access to non-local variables in a subprogram?

<p>Decreased reliability.</p> Signup and view all the answers

Which of the following statements is incorrect regarding subprograms?

<p>A subprogram call is simply an implicit requirements call.</p> Signup and view all the answers

What characteristic of a procedure differentiates it from a function?

<p>Procedures do not return a value to the caller.</p> Signup and view all the answers

What distinguishes a function from a procedure?

<p>Functions do not modify parameters or external variables.</p> Signup and view all the answers

What is an advantage of using stack dynamic local variables?

<p>They allow for support of recursion.</p> Signup and view all the answers

Which statement is true about parameters in subprograms?

<p>Parameter passing methods can affect how values are shared between subprograms.</p> Signup and view all the answers

What is a disadvantage of static local variables?

<p>They create a run-time overhead for allocation and deallocation.</p> Signup and view all the answers

Which question is NOT among the design issues for subprograms?

<p>Are local variables initialized at runtime?</p> Signup and view all the answers

How may procedures produce results in the calling program unit?

<p>By changing formal parameters passed into the procedure.</p> Signup and view all the answers

What is a characteristic of local variables defined inside subprograms?

<p>They are unable to maintain values between calls.</p> Signup and view all the answers

What is one of the disadvantages of using stack dynamic local variables?

<p>They acquire storage space during the program's execution.</p> 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.

Quiz Team

Related Documents

Chapter 9 Subprograms PDF

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.

More Like This

Nested Subprograms and Blocks Quiz
34 questions
Programming Chapter 9: Subprograms
106 questions
Programming Chapter 6: Subprograms
5 questions

Programming Chapter 6: Subprograms

UserFriendlySerpentine2341 avatar
UserFriendlySerpentine2341
Use Quizgecko on...
Browser
Browser