recursion

0001-01-01

Recursion is a computer programming technique that involves a function calling itself, breaking a task down into smaller and more manageable pieces.

A classic example of recursion is calculating the Fibonacci sequence.

#include <stdio.h>
#include <stdlib.h>

void fib(int a, int b, int max)
{
    int result = a + b;

    if (result == 1) printf("0 1 ");
    if (result < max) {
	printf("%d ", result);
	fib(b, result, max); // <---- this is where the recursion be
    } else {
	printf("\n");
    }
}

int main(int argc, char *argv[])
{
    if (argc > 1)
	fib(0, 1, atoi(argv[1]));
    else
	fib(0, 1, 1000);
}