MODULE 03: MODULAR FUNCTIONS, CALL
STACK ARCHITECTURE, RECURSION &
STORAGE CLASSES
Course: B.Sc. / B.Tech. Computer Science | Standard: ISO/IEC 9899 (C99 / C11)
1. Procedural Decomposition & Modular Software Design
In systems engineering, complex algorithms are partitioned into cohesive, reusable
subroutines called functions. Modular decomposition achieves three architectural
objectives:
Abstraction & Encapsulation: Callers interact strictly through a documented function
signature without depending on internal implementation details.
Elimination of Code Duplication: Shared computational routines are compiled once in
the text segment and invoked from multiple call sites.
Isolated Verification: Individual functions can be unit-tested independently before
system integration.
1.1 Function Prototypes & Type Checking
In ANSI C89/C99/C11, every function must be declared via a function prototype prior to
its first invocation. The prototype specifies the return type and parameter list:
// Prototype declarations in header file (math_utils.h)
int is_prime(int candidate);
void swap_integers(int *first, int *second);
Void Parameter List Distinction: In C, int func(void); explicitly declares a function
accepting zero arguments, whereas int func(); declares an obsolete K&R-style
prototype accepting an unspecified number of arguments.
2. Process Memory Layout & Call Stack Mechanics
When the operating system loads a compiled C executable into virtual memory, the
address space is segmented into five distinct regions:
±----------------------------------+ High Virtual Address (0x7fffffffffff)
| CALL STACK | Grows downward; stores local auto variables,
| (Activation Records / Frames) | parameters, and return addresses
±----------------------------------+
|||
,|v|
| (Free Space) |
|^|
|||
±----------------------------------+
| HEAP | Grows upward; dynamic allocation (malloc/free)
±----------------------------------+
| BSS SEGMENT (.bss) | Uninitialized global & static variables (zeroed)
±----------------------------------+
| DATA SEGMENT (.data) | Initialized global & static variables
±----------------------------------+
| TEXT SEGMENT (.text) | Read-only compiled machine instructions
±----------------------------------+ Low Virtual Address (0x000000400000)
2.1 Anatomy of a Stack Frame (Activation Record)
When caller() invokes callee(a, b) , the CPU executes the following low-level sequence:
Arguments a and b are placed into CPU registers (System V AMD64 ABI: %rdi , %rsi ,
%rdx , %rcx , %r8 , %r9 ) or pushed onto the stack.
The CALL instruction pushes the Return Address (the instruction immediately
following the call) onto the stack and jumps to callee .
The function prologue saves the caller’s Base Pointer ( %rbp ) and decrements the Stack
Pointer ( %rsp ) to reserve space for local auto variables.
Upon encountering return , the epilogue restores %rsp and %rbp , and the RET
instruction pops the return address back into the Instruction Pointer ( %rip ).
3. Parameter Passing: Value vs. Pointer Indirection
#include <stdio.h>
// Pass-by-Value: Operates on local copies; caller variables unaffected
void attempt_swap_by_value(int x, int y) {
int temp = x;
x = y;
y = temp;
}
// Pass-by-Address: Dereferences pointers to mutate caller memory directly
void swap_by_address(int *px, int *py) {
int temp = *px;
*px = *py;
*py = temp;
}