Skip to content

4. Control Structures and Subprograms

1 Introduction

  • Branching or structural execution is the opposite of sequential execution and spaghetti code.
  • Program structures:
    • Sequence: line by line execution.
    • Selection: branching.
    • Repetition: looping. preTest: while, for loop (number of iterations is known). postTest: do-while.
  • Subprograms:
    • Have a single entry point.
    • When a subprogram is called, the caller is suspended and control is transferred to the called subprogram, the state of the caller is saved.
    • When the subprogram terminates, control is transferred back to the caller.
    • It has two types: functions and procedures.
    • Issues:
      • What mode of parameter passing is used (call by value, call by reference).
      • Should parameters be type checked?
      • Can a parameter be a subprogram name?
      • Subprogram overloading.
      • Recursion. Static variables are not allowed in recursive subprograms.
  • Procedures:
    • Have no return value.
    • Never modify the parameters they receive.
    • They can not be used in expressions as there is no return value to be used.
  • Functions:
    • Has mathematical meaning.
    • It is a procedure that returns a value.
    • Never modify the parameters they receive.
    • Because they return a value, they have to be used in expressions.
  • Recursion:
    • Functional programming languages are based on recursion, as they lack the looping or repetition control structure.
    • Stack is essential to preserve state while new instances of the subprogram are called.

References


  1. UoPeople. (2023). CS4402: Comparative Programming Languages. Lecture Notes Unit 4.