So I am attempting to run this code, but for some reason my final print function is not printing to the terminal. Can you help me figure out what I am doing wrong?

#include <cs50.h>
#include <stdio.h>

int main(void)
    int start_pop = get_int("Please enter starting population size: ");
    int end_pop = get_int("Please enter ending population size: ");

    int years = 0;
    int new_pop = start_pop;

        new_pop = new_pop + new_pop / 3 - new_pop / 4;
    while (new_pop < end_pop);
        return new_pop;
    return years;
    printf("Years: %i", years);

hint: what happens in the code when it reaches the lines of code that say return?

hint2: what happens to new_pop, if new_pop is less than end_pop, and new_pop never changes?


The return statement terminates the execution of the current function and returns a value to the calling function. Here, what this means is it is terminating the main function. Once your code gets to return years; it is ending because you are calling it within main and there is nothing else to do outside of main.

You don't need a return command here because you are not getting a value for years within a function nested in main. You are simply running a do-while loop. This value is already updating within main which is the calling function.

If you remove return years; your code should print the value of years just fine. Does this make sense?

More on the return command here: https://dotnettutorials.net/lesson/return-statement-in-c/

You must log in to answer this question.

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