Loading lessons...
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.voidreturns nothing (noreturnneeded).
Early return
int bigger(int a, int b) {
if (a > b) return a;
return b;
}
TL;DR
return valuesends a result back to the caller.- The declared return type must match what you return.
- Capture the result in a variable.
voidfunctions return nothing.