C Return Values
In this page:
return_type function_name(parameters) {
return value;
}
void function_name() {
return; // optional, returns nothing
}
The return Statement
The return statement immediately ends a function's execution and, if the function has a non-void return type, sends a value back to whatever code called it -- any statements after return in that function never run.
Example: The return Statement
// Include standard input/output (printf, scanf)
#include <stdio.h>
int getFive() {
// Send 5 back to the caller
return 5;
// Print formatted text to the screen
printf("never runs");
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", getFive());
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Returning Integers
Declaring a function's return type as int means it hands back a whole number to its caller, which is the standard choice for functions computing counts, indexes, or results of integer arithmetic.
Example: Returning Integers
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", add(2, 3));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Returning Floating-Point Numbers
When a calculation needs fractional precision, declaring the function's return type as float or double lets it hand back a decimal result instead of being forced to round to a whole number.
Example: Returning Floating-Point Numbers
// Include standard input/output (printf, scanf)
#include <stdio.h>
double divide(int a, int b) {
// Send (double)a / b back to the caller
return (double)a / b;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%.2f", divide(7, 2));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Returning Characters
A function can return char to hand back a single character result -- useful for functions that classify input or compute something like a grade letter based on a numeric score.
Example: Returning Characters
// Include standard input/output (printf, scanf)
#include <stdio.h>
char getGrade(int score) {
// Send (score >= 90) ? 'A' : 'B' back to the caller
return (score >= 90) ? 'A' : 'B';
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%c", getGrade(95));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using Returned Values in Expressions
A function's returned value can be used directly inside a larger expression -- like total = add(3, 4) * 2; -- without first storing it in an intermediate variable, since the call itself evaluates to that returned value.
Example: Using Returned Values in Expressions
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
// Program execution starts in main()
int main() {
// Declare total and set it to add(3, 4) * 2
int total = add(3, 4) * 2;
// Print formatted text to the screen
printf("%d", total);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting a
returnstatement in a non-void function. - Returning a pointer to a local variable, which no longer exists after the function ends.
- Trying to return two values with
return a, b;, which returns onlyb.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: