C: Advanced Functions

🗓 Last updated: 2026-11-30👤 Written by: Ivy✓ Reviewed by: Alex🏷 Category: C Tutorial

Recursion is like two mirrors facing each other -- images nest within themselves until the distant point of light vanishes. A function that calls itself solves problems through this power of progressive reduction.

Reference entries in this section: long auto static extern register

1. Recursion Principles

Recursion is a programming technique where a function calls itself directly or indirectly. Every recursion must have two elements:

  1. Recursive condition: the problem can be broken down into smaller subproblems of the same kind
  2. Base case (terminating condition): the smallest subproblem has a direct answer and no longer recurses
C
// Function definition
int factorial(int n) {
    // Conditional check
    if (n <= 1) {
        // Return result
        return 1;
    }
    // Return result
    return n * factorial(n - 1);
}

Take factorial(4) as an example, the execution process:

C
factorial(4)
= 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * 2 * factorial(1)
= 4 * 3 * 2 * 1
= 24
⚠️ Note: A recursion without a base case will call infinitely until the stack overflows and crashes. Always determine the base case first when writing recursion.


2. Classic Recursion Examples

(1) Factorial

The definition of n factorial: n! = n x (n-1)!, and 0! = 1.

C
// Function definition
long factorial(int n) {
    // Conditional check
    if (n <= 1) return 1;
    // Return result
    return n * factorial(n - 1);
}

Note that the long type has a limited range; factorial(20) is already close to the 64-bit limit.

(2) Fibonacci Sequence

Fibonacci definition: F(1)=1, F(2)=1, F(n)=F(n-1)+F(n-2).

C
// Function definition
long fib(int n) {
    // Conditional check
    if (n <= 2) return 1;
    // Return result
    return fib(n - 1) + fib(n - 2);
}
🔥 Common Mistake: Naive recursive Fibonacci is extremely inefficient! fib(40) requires hundreds of millions of calls because many subproblems are recomputed. In practice, use a loop or memoization.

(3) Tower of Hanoi

Move n disks from peg A to peg C, using peg B as auxiliary. Rule: a larger disk cannot be placed on a smaller one.

C
// Preprocessor directive
#include <stdio.h>

// Function definition
void hanoi(int n, char from, char mid, char to) {
    // Conditional check
    if (n == 1) {
        // I/O operation
        printf("%c -> %c\n", from, to);
        // Return result
        return;
    }
    hanoi(n - 1, from, to, mid);
    // I/O operation
    printf("%c -> %c\n", from, to);
    hanoi(n - 1, mid, from, to);
}

// Function definition
int main(void) {
    hanoi(3, 'A', 'B', 'C');
    // Return result
    return 0;
}
TEXT
A -> C
A -> B
C -> B
A -> C
B -> A
B -> C
A -> C

The idea: first move the top n-1 disks to the auxiliary peg, move the largest disk to the target peg, then move the n-1 disks from the auxiliary peg to the target peg. 3 disks require 7 moves; n disks require 2^n - 1 moves.

▶ Example

Recursively compute the sum of an integer's digits:

▶ Try it Yourself
TEXT
15
7

digit_sum(12345) = 5 + digit_sum(1234) = 5 + 4 + digit_sum(123) = ... = 5+4+3+2+1 = 15.


3. Variable Scope

Scope determines which regions of code can see and use a variable.

(1) Block Scope

Variables defined within {} are only visible inside that block and are destroyed when the block ends:

C
// Function definition
int main(void) {
    // Variable declaration
    int x = 10;
    {
        // Variable declaration
        int y = 20;
        // I/O operation
        printf("%d %d\n", x, y);
    }
    // I/O operation
    printf("%d\n", x);
    // Return result
    return 0;
}

y exists only in the inner block and cannot be accessed from the outer block.

(2) File Scope

Variables defined outside all functions have file scope and are visible from their definition to the end of the file. These are called global variables:

C
// Variable declaration
int count = 0;

// Function definition
void increment(void) {
    count++;
}

// Function definition
int main(void) {
    increment();
    increment();
    // I/O operation
    printf("%d\n", count);
    // Return result
    return 0;
}
⚠️ Note: Global variables are convenient but can be accidentally modified by any function, making debugging harder. Use local variables whenever possible instead of global variables.

(3) Name Shadowing

A variable in an inner scope shadows a variable with the same name in an outer scope:

C
// Variable declaration
int x = 100;

// Function definition
int main(void) {
    // Variable declaration
    int x = 10;
    // I/O operation
    printf("%d\n", x);
    // Return result
    return 0;
}

The output is 10; the inner x shadows the global x. The global x cannot be directly accessed inside the inner block.


4. Storage Classes

Storage classes determine a variable's lifetime and visibility. C has four storage class keywords: auto, static, extern, and register.

(1) auto

auto is the default storage class for local variables and is usually omitted. Variables are created when the block is entered and destroyed when the block is exited:

C
// Function definition
void func(void) {
    auto int x = 10;
}

This is equivalent to int x = 10;.

(2) static

When static modifies a local variable, the variable exists for the entire program runtime, but its scope remains limited to the function. Key property: it is initialized only once, and subsequent calls retain the previous value.

C
// Preprocessor directive
#include <stdio.h>

// Function definition
void counter(void) {
    static int count = 0;
    count++;
    // I/O operation
    printf("Call #%d\n", count);
}

// Function definition
int main(void) {
    counter();
    counter();
    counter();
    // Return result
    return 0;
}
TEXT
Call #1
Call #2
Call #3

Without static, count would be reinitialized to 0 each time, always printing "Call #1".

When static modifies a global variable or function, it restricts its visibility to the current file (internal linkage); other source files cannot access it via extern.

(3) extern

extern declares a global variable defined in another source file, telling the compiler "this variable exists, but is not defined in the current file":

file1.c:

C
// Variable declaration
int shared_data = 42;

file2.c:

TEXT
#include <stdio.h>

extern int shared_data;

int main(void) {
    printf("%d\n", shared_data);
    return 0;
}

extern does not allocate memory; it is only a declaration. Multi-file projects need extern to share global data.

(4) register

register suggests the compiler store the variable in a CPU register for faster access:

C
// Function definition
void fast_loop(void) {
    register int i;
    // For loop
    for (i = 0; i < 1000000; i++) {
    }
}

Modern compilers are very good at optimization and typically place frequently used variables in registers automatically, so the register hint has little effect. Note: you cannot take the address of a register variable (&), because registers do not have memory addresses.

▶ Example

Iterative Fibonacci using a loop (avoiding recursion's repeated computation):

▶ Try it Yourself
TEXT
F(1) = 1
F(2) = 1
F(3) = 2
F(4) = 3
F(5) = 5
F(6) = 8
F(7) = 13
F(8) = 21
F(9) = 34
F(10) = 55

This iterative version has O(n) time complexity, far better than the naive recursion's O(2^n).

▶ Example 3: Recursive Factorial

▶ Try it Yourself

输出:

TEXT
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320

Reference entries in this section: long auto static extern register

❓ FAQ

Q Which is better, recursion or loops?
A Loops are usually more efficient; recursion is more intuitive for certain problems. Prefer loops for simple cases; use recursion for naturally recursive structures like tree traversal.
Q Where in memory are static local variables stored?
A In the static storage area (data segment), in the same region as global variables, not on the stack.
Q What is the difference between an extern declaration and a variable definition?
A A definition allocates memory and may initialize; an extern declaration does not allocate memory, it only informs that the variable is defined elsewhere. A variable can only be defined once but declared multiple times.
Q Are register variables really faster?
A Modern compilers automatically perform register allocation optimization. Explicit register has almost no additional effect; this keyword is primarily of historical significance.

📖 Summary

📝 Exercises

  1. Write a recursive function void print_reverse(int n) that prints the digits of a positive integer in reverse order (e.g., 123 outputs 3 2 1).
  2. Write a function that uses a static variable to count how many times it has been called. Call it 5 times in main, then print the count.
  3. Implement binary search recursively: search for a target value in a sorted array; return the index if found, or -1 if not found.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏