Search Knowledge

© 2026 LIBREUNI PROJECT

C Programming Mastery / Functions & Scope

Functional Decomposition and the Call Stack

The Function as an Abstraction

In C, a function is a named block of code that performs a specific task. They are the primary tool for Decomposition—breaking complex problems into manageable, testable units.

A C function consists of:

  1. Return Type: The type of value the function returns to the caller (or void).
  2. Name: A unique identifier.
  3. Parameters: A list of data types and names passed into the function.
  4. Body: The implementation.

Prototypes vs. Definitions

C is a single-pass compiler (conceptually). If you call a function before the compiler has seen its definition, it won’t know the types of the arguments or the return value.

  • Function Prototype (Declaration): Tells the compiler the function’s signature. Usually placed in header files or at the top of a .c file.
  • Function Definition: The actual implementation of the function.
// Prototype
int square(int x);

int main() {
    int res = square(5); // Compiler knows 'square' takes an int and returns an int
}

// Definition
int square(int x) {
    return x * x;
}

The Call Stack and Stack Frames

Every time a function is called, a new Stack Frame (or Activation Record) is pushed onto the Call Stack. This frame contains:

  • The function’s local variables.
  • The parameters passed by the caller.
  • The Return Address (where to jump back to when the function finishes).

When the function returns, its stack frame is “popped,” and its local memory is effectively reclaimed (this is why local variables are called Automatic variables).

Parameter Passing: Pass-by-Value

C follows a strict Pass-by-Value model. When you pass a variable to a function, the function receives a copy of the data.

To modify a variable from within a function, you must pass the address of the variable (using pointers). Even then, C is still passing the value of the address.

Runtime Environment

Interactive Lab

1#include <stdio.h>
2 
3void increment(int val) {
4 val = val + 1;
5 printf("Inside function: %d\n", val);
6}
7 
8int main() {
9 int count = 5;
10 increment(count);
11 printf("In main: %d (Unchanged!)\n", count);
12 return 0;
13}
System Console

Waiting for signal...

Recursion and Stack Overflow

A function that calls itself is Recursive. Each recursive call adds a new frame to the stack. If the recursion is too deep (or infinite), it will exhaust the stack memory, resulting in a Stack Overflow.

int factorial(int n) {
    if (n <= 1) return 1; // Base case
    return n * factorial(n - 1); // Recursive step
}
Interactive Lab

The Return Type

 print_message(void) {
    printf("Hello\n");
}

Variable Scope and Lifetime

  • Local Scope: Variables declared inside a block {}. They exist only while that block is executing.
  • Global Scope: Variables declared outside any function. They exist for the entire duration of the program.
  • Static Locals: Local variables that retain their value between function calls. They are stored in the Data/BSS segment rather than the stack.