← Back to C Course | Chapter 5: Functions | Lesson 3 of 8

C Return Values

A return value is the answer a function hands back when it finishes, like a vending machine giving you a snack. You can then use that answer in the rest of your program.
Syntax
c
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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting a return statement in a non-void function.
  2. Returning a pointer to a local variable, which no longer exists after the function ends.
  3. Trying to return two values with return a, b;, which returns only b.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.