Lesson 76 +10 XP

Return Values

Return Values

A function can hand a result back to the caller with return.

A function that returns an int

int add(int a, int b) {
    return a + b;
}

Using the return value

int result = add(3, 4);
printf("%d", result);   // 7

The return type must match

  • int add(...) must return an int.
  • double area(...) returns a double.
  • void returns nothing (no return needed).

Early return

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

TL;DR

  • return value sends a result back to the caller.
  • The declared return type must match what you return.
  • Capture the result in a variable.
  • void functions return nothing.