#include <cs50.h> #include <stdio.h> 
void draw(int h);

int main(void)
    // Get height of pyramid
    int height = get_int("Height: ");

    // Draw pyramid

void draw(int h)
    // If nothing to draw
    if (h == 0)

    // Draw pyramid of height h - 1
    draw(h - 1);

    // Draw one more row of width h
    for (int i = 0; i < h; i++)

In this h is decreasing when draw(h-1) funtion recursively calls After this when for loop executes how h is increasing again?

1 Answer 1


When recursively calling a function, each call creates it's own set of variables. As each recursion calls the next, the value of the local version of h is one less than the previous call. This continues to the last call, so in the last call of draw(), the local variable of h will be zero.

At this point, the recursion reverses, coming out of the recursion calls. Each recursion will end, returning control to the one that called it, where the value of the local var h remains unchanged from before the next recursion was called.

Simply put, the local values of h remain unchanged as the recursions proceed forward and then return backward.

Any questions? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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