Nimbin[12]?C-Tutorial / tutorium_pe1_se1 / 09_rekursive-funktion.c

tutorium_pe1_se1 git · main

self-explaining C files

c tutorial · first commit 2025-11-15 · last commit 2025-11-30 (11 months ago) · synced 1 hour ago · upstream: git.ide3.de/hsnr/tutorium_pe1_se1

C 100%
git clone https://git.christianimmanuel.de/c-tutorial/tutorium_pe1_se1.gitwget https://git.christianimmanuel.de/c-tutorial/tutorium_pe1_se1/archive/tutorium_pe1_se1.tar.gz
09_rekursive-funktion.c 3.3 KB · 116 lines raw
#include <stdio.h>

/* Wir haben bereits funkionen kenne gelernt,
 * und Funktionen die andere Funktionen aufrufen.
 *
 * Eine rekursive ((bis zu bekannten werten) zurückgehend) Funktion
 * ist eine Funktion die sich selbst aufruft! also schnell geschrieben:
 *
 * void foo() { foo(); }
 *
 * hier ist allerdings das Problem dass diese Funktion niemals zum ende kommt!
 * foo ruft foo auf
 *     foo ruft foo auf
 *         foo ruft foo auf
 *             ...
 *
 * Somit ist schonmal klar was verdammt wichtig bei rekursiven Funktionen ist!
 *   wann breche ich ab?
 */

int global_a = 0;

void printGlobalAuntilItIsBigEnough() {
    if (global_a > 10) return;

    printf("i am the %d. (nth) call!\n", global_a);
    global_a++;

    printGlobalAuntilItIsBigEnough();
}
/* main ruft printGlobal auf {
 *    printGlobal ruft printGlobal auf {
 *      printGlobal ruft printGlobal auf {
 *          ...
 *          bedingung ist erreicht !
 *      } aufruf wird beendet
 *    } aufruf wird beendet
 * } aufruf wird beendet
 *
 *

/* Etwas spannender wird es wenn unsere rekursive Funktion etwas tatsächliches berechnen soll!
 * denn dann müssen wir auf die Rückgabe achten, also
 * auf dem weg wird eine Berechnung immer weiter geführt bis eine Bedingung erreicht wird.
 * Dann müssen wir über all die aufgerufenen Funktionen den wert wieder an den ursprünglichen Aufrufer zurück geben!
 */

int addNumber(int a) {
    if (a >= 10) return a; // das wird nur vom letzten Aufruf erfüllt!

    return addNumber(a+2); // das machen alle anderen
} // <- Erklärung siehe main


int fibonacci(int first, int second, int max) {
    if (second > max)
        return first;
    return fibonacci(second, first+second, max);
}

#define ARR_SIIZE 6
int find_index_of_array_item(int arr[ARR_SIIZE], int search_number, int current_index) {
    // haben wir das ende des arrays erreicht oder ||
    // ist die aktuelle stelle im array die gesuchte nummer ?
    // dann gib diesen index wert zurück!
    if (current_index == ARR_SIIZE || arr[current_index] == search_number)
        return current_index;

    return find_index_of_array_item(arr, search_number, ++current_index);
}

int main() {
    int arr[ARR_SIIZE] = { 10, 20, 30, 40, 50, 60 };

    printGlobalAuntilItIsBigEnough();

    int added = addNumber(0);
    printf("Added: %d\n", added);
    /* addNumber 0 {
     *    call to
     *    return addNumber(a+2) -> 2 {
     *        call to
     *        return addNumber (a+2) -> 4 {
     *           call to
     *           return addNumber (a+2) -> 6 {
     *              call to
     *              return addNumber (a+2) -> 10  {
     *                  return 10 because of (a >= 10)
     *              }
     *              return 10
     *           }
     *           return 10
     *        }
     *        return 10
     *     }
     *     return 10
     * }
     *
     *
     * nochmal stark vereinfacht diese endlose Schachtelung:
     * adNumber() { return adNumber() { return adNumber() { ... return a } } }
     */


    printf("Fibonacci < 20: %d\n", fibonacci(1, 1, 20));

    int search = 30;
    int index_of_searchnumber = find_index_of_array_item(arr, search, 0);
    if (index_of_searchnumber == ARR_SIIZE)
        printf("Cannot find number %d in array!\n", search);
    else
        printf("Index of number %d is: %d\n", search, index_of_searchnumber);


    return 0;
}