A similar question was asked previously about "when" the recursion happens in inheritance and I'm struggling to wrap my head around it.

How does the program ever reach parent[1] if create family is recursively called on parent[0]? wouldn't the call keep happening on parent[0] and reducing the generations until it the condition was no longer met?

person *create_family(int generations)
// TODO: Allocate memory for new person

    person *p = malloc(sizeof(person));

    // Generation with parent data
    // generations keeps track of where we are in the family tree
    if (generations > 1)

        // TODO: Recursively create blood type histories for parents
        p->parents[0] = create_family(generations -1);
        p->parents[1] = create_family(generations -1);

        // TODO: Randomly assign child alleles based on parents
        int randomIndex0 = rand() % 2;
        int randomIndex1 = rand() % 2;
        p->alleles[0] = p->parents[0]->alleles[randomIndex0];
        p->alleles[1] = p->parents[1]->alleles[randomIndex1];

    // Generation without parent data
        // TODO: Set parent pointers to NULL
        p->parents[0] = NULL;
        p->parents[1] = NULL;

        // TODO: Randomly assign alleles
        p->alleles[0] = random_allele();
        p->alleles[1] = random_allele();

    // TODO: Return newly created person
    return p;

1 Answer 1


I found my answer, once the recursive function calls for "parents [0]" reach the 3rd generation, it then "returns," and finishes the rest of the code under parents [0], which then recursively creates a family for parent [1].

PS. I was having a hard time understanding where the program returns to after parents[0] generation condition was met. So it returns to where it left off when it last called the function.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .