Dynamic control flow is a type of control flow that is in opposition to static control flow. In static control flow, the address of the destination is always known, i.e., if you follow a program you always know where it ends up. The opposite is true of dynamic control flow.
This is extraordinarily useful, in particular in CPSC 213, dynamic control flow is used to implement:
- Procedure returns
- Dynamic dispatch in polymorphism
- Generic operations/functions (like in CPSC 110!)
- Jump tables
Procedure Returns
Since a compiler can’t look at a procedure and know what procedure called it, we (or the assembler) need to provide that information for the procedure. The process for that is explained in the procedure article. In short, we give it the address of the procedure that called it on the stack.
Dynamic Dispatch
Dynamic dispatch is the process of selecting which implementation of a polymorphic function to call at runtime. It is only possible with dynamic control flow and enables polymorphism in general.