Software Architecture

Modularity, Functions & Scope

Modular design and Call Stack management in C, JavaScript, Python, and Java.

SLIDE 1 / 6

1. Principles of Modularity

Modularity divides complex systems into independent modules/subprograms with single responsibilities.

Reusability: Write once, invoke anywhere.
Abstraction: Hide implementation details behind clean interfaces.
MODULE ARCHITECTURE
[Main Application]
├── [Math Module] ├── [Auth Module] └── [UI Renderer]

2. Functions vs Procedures

  • Pure Function: Computes and returns a value without side effects.
  • Procedure (Void): Executes actions (I/O, mutations) and returns void (or None/undefined).
modularity.c
// Función (Retorna int)
int sumar(int a, int b) {
    return a + b;
}

// Procedimiento (Efecto secundario)
void imprimir_reporte() {
    printf("Proceso completado\n");
}

3. Scope Rules

Scope defines variable visibility and lifetime within the program.

C / Java: Block scope {...} and global/class scope.
JavaScript: Block scope (let/const), function scope (var), and Lexical Closures.
Python: LEGB Rule (Local, Enclosing, Global, Built-in).
scope.js
let globalVar = "Global";

function testScope() {
    let localVar = "Local";
    if (true) {
        let blockVar = "Block";
    }
    // blockVar no es accesible aquí
}

4. Signatures & First-Class Functions

  • Java: Strict overloading (same name, different parameter types).
  • C: Unique prototypes. No native overloading.
  • JS / Python: First-class functions (passed as arguments/higher-order).
Overloading.java
public class MathUtils {
    public static int add(int a, int b) { return a + b; }
    public static double add(double a, double b) { return a + b; }
}

5. The Call Stack & Recursion

Each function call creates a Stack Frame with parameters and local variables. On return, frame is popped (LIFO).

Recursión: Function calling itself. Requires base case to prevent StackOverflow.
recursion.py
def factorial(n):
    if n <= 1:
        return 1 # Caso base
    return n * factorial(n - 1)

print(factorial(3)) # 3 * 2 * 1 = 6

6. Call Stack Simulator

Simulate stack frame push/pop during execution:

C Execution Trace
Paso 1: main() es empujado al Call Stack.
[CALL STACK: main()]