liveBook · Manning

Encapsulation

As explained in Modern C, Third Edition

Encapsulation is the practice of containing implementation details and associated state within a program unit while exposing a callable interface.

Encapsulation contains a function’s implementation details and local state within its call. Local variables and parameters are visible only inside the function, receive fresh storage on each call, and disappear when execution leaves the function. section 7.3

Recursive function calls

In gcd2, each recursive call receives a new pair of parameters and its own local remainder. An active caller’s locals remain separate from those of the callee, so recursive calls do not create identifier conflicts. section 7.3

inline size_t gcd2(size_t a, size_t b) [[__unsequenced__]] {  1 
  assert(a <= b);               2  
  if (!a) return b;    3  
  size_t r = b % a;            4  
  return gcd2(r, a);     
}

Inspect the compact gcd2 implementation to see encapsulation operating alongside assertions, local variables, and recursive calls. section 7.3

How Modern C, Third Edition covers it

How deep it goes
Explains visibility, lifetime, fresh initialization, and cleanup of locals during nested and recursive calls.
Examples
Demonstrates independent call state through the recursive gcd2 implementation and its recursive call hierarchy.
What it assumes
Assumes familiarity with C functions and arguments while establishing local-variable scope and lifetime through examples.
What you can do after
Evaluate how function calls isolate identifiers and state across ordinary, nested, and recursive execution.
How it connects
Connects encapsulation to functions, local variables, parameters, interfaces, and recursion.

Also explained in

Compare how 14 books explain encapsulation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.