return Ends the Function

return does two things at once: it gives back a value, and it ends the function right away. The rest of the function doesn't run:

#include <stdio.h>

int add(int a, int b) {
    return a + b;
    printf("This line never runs.\n");
}

int main(void) {
    printf("%d\n", add(2, 3));
    return 0;
}

This program prints:

5

The line after return never runs, because the function has already ended.

That is handy together with conditions. This function returns the larger of two numbers:

#include <stdio.h>

int max(int a, int b) {
    if (a > b) {
        return a;
    }
    return b;
}

int main(void) {
    printf("%d\n", max(7, 3));
    printf("%d\n", max(2, 9));
    return 0;
}

This program prints:

7
9

For max(7, 3), the condition 7 > 3 is true, so the function returns 7 and ends right there: the last line doesn't run. For max(2, 9), the condition is false, the body of the if is skipped, and the function gets to return b;, so it returns 9. You don't need an else: as soon as a return runs, the rest of the function is skipped anyway. (With else, the function would work the same.)

When there are several conditions, their order matters, because the first return that runs wins. This function gives a player 3, 2 or 1 stars for their points:

#include <stdio.h>

int stars(int points) {
    if (points >= 90) {
        return 3;
    }
    if (points >= 50) {
        return 2;
    }
    return 1;
}

int main(void) {
    printf("%d\n", stars(95));
    printf("%d\n", stars(60));
    printf("%d\n", stars(20));
    return 0;
}

This program prints:

3
2
1

For 95 points, the first condition is true, so the function returns 3. For 60 points, the first condition is false and the second one is true: 2. For 20 points, neither condition is true, so the function gets all the way to the last line and returns 1. If the function checked points >= 50 first, 95 points would get only 2 stars, because 95 is at least 50 too.

Watch out for the last return. A function that isn't void must return a value, whichever way the conditions turn out. Without the line return 1;, the function would have nothing to return for 20 points, and the program wouldn't compile: error: non-void function does not return a value in all control paths. The end of the message, in all control paths, means in every case: the compiler noticed that one way through the function reaches its end without a return.

In a void function, you can write return; without a value. The function then simply ends. That is useful when the function should stop early because it can't do its job:

#include <stdio.h>

void print_division(int a, int b) {
    if (b == 0) {
        printf("You can't divide by zero.\n");
        return;
    }
    printf("%d\n", a / b);
}

int main(void) {
    print_division(10, 2);
    print_division(10, 0);
    return 0;
}

This program prints:

5
You can't divide by zero.

Instructions

The function ticket_price returns the price of a zoo ticket for a visitor of the given age. So far, it returns 200 for everybody. Above the line return 200;, add conditions: children under 6 get in for free (0), and visitors under 18 pay 100. The program then prints 0, 100 and 200.

Seniors aged 65 and over pay 100 too. Add a condition for them to the function, and at the end of main, before return 0;, print ticket_price(70). The program prints one more 100.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

7/12