Return Statements

Lesson 17
Author : Afrixi
Last Updated : January, 2023
C - Programming Language
This course covers the basics of programming in C.

In C, a return statement is used to exit a function and return a value back to the calling code. A return statement is written as follows:

return expression;

Here, expression is the value that is returned by the function. The type of the expression must match the return type specified in the function declaration.

For example, consider the following function that calculates the sum of two integers:

int sum(int a, int b) {
  int result = a + b;
  return result;
}

The return result; statement in this function returns the sum of the two integers back to the calling code.

If a function has a return type of void, it means that the function does not return a value. In this case, a return statement without an expression is used to exit the function, like this:

void print_hello() {
  printf("Hello!\n");
  return;
}

Here, the return; statement is used to exit the print_hello() function without returning a value.

It is important to note that a return statement can only appear within a function. Attempting to use a return statement outside of a function will result in a compile-time error.