The return statement in C

    The return statement ends the current function and returns control in the point of invocation. It could also return value in that point. Here is the syntax and examples:

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!

Syntax

Returning control from function that does not return value:
retur
n; 

Returning control from function that returns value:
return
<value>;

The return value could be any valid expression that returns a value:

  • a constant
  • a variable
  • a calculation, for instance (a + b) * c
  • call to another function that returns a value

    The value must be of the same (or compatible) type that the function was defined. For example, an int function can’t return a float value.

How the return statement works?

    First you need to understand how the function call works. When you call a function two things happen

  1. The execution of the current function is paused.
  2. The function that you call executes.

    This is what we call a transfer of control. When you call a function the control of the program is transferred from the calling function to the called function.

    The return statement returns the control to the previous routine. That function will continue its execution from the point where it was paused.

    On the contrary, a function that calls the return statement is finished. It is not in a paused state and it cannot resume.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
void main()
{
    int sum = sumDigits();
    printf("%d\n", sum);
    return;
}

int sumDigits()
{
    int sum = 0;
    int digit;
    for(digit = 0; digit <= 9; ++digit)
    {
        sum += digit;
    }
    return sum;
}

    The function main is called when our program is started. Then it calls the routine sumDigits. At this point “main” is paused and sumDigits starts. The second function will do its calculations and reach the return sum; statement. Here it ends and the control is transferred back in main. Since sumDigits returns a value(the value of the sum variable), a value will appear in the place where the function was called.

    The sum of the digits is 0+1+2…+9= 45. This value will appear in the place of invocation and the variable sum will take this value. Then we print that value and return from the main function, which ends our program.

Using the return statement in void functions

    Note that in the example above we didn’t need to use the return statement in the main function. If a function is defined as “void” it does not need to return a value. Such functions return control automatically when they reach the end of their body.

Still, we can use “return;” to end their execution from any point of their body.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
void printIfFound(int array[], int size, int value)
{
    int i;
    for(i = 0; i < size; ++i)
    {
        if(array[i] == value)
        {
            printf("The value %d was found in index %d\n", value, i);
            return;
       }
    }
    printf("The value %d was not found.\n", value);
}

    Here we use the return keyword to interrupt the function printIfFound. This accomplishes two things:

  1. Stops the execution of unnecessary for iterations
  2. Prevents the function from printing the "not found" message when we find the value.

Returning a value

    If a function is defined to return a value it must use the return statement. It must also make sure that each possible outcome returns a value. This could get tricky if your routine has nested constructions and you are not careful.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
int linearSearch(int array[], int size, int value)
{
    int i;
    for(i = 0; i < size; ++i)
    {
        if(array[i] == value)
        {
            return i;
       }
    }
    return -1;
}  

    If you forget the return -1; you’ve got a problem. In C many compilers will allow such “broken” code to compile, but it will not work properly. Such bugs could be hellish to find and fix. For this reason you need to plan very carefully your branches. See good practices below for guidance.

You can download and test the examples from here.

Good practices

  • Most of the time a routine should use only one return statement at the end of the function’s body.
  • To accomplish this end your loops with a break statement and
  • Keep the result in a local variable
  • An exception would be if you need to exit from nested loops and the use of multiple breaks and conditions makes code harder to read.

Here are the above examples, changed to follow the guidelines: 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
void printIfFound(int array[], int size, int value)
{
    int found = -1;
    int i;
    for(i = 0; i < size; ++i)
    {
        if(array[i] == value)
        {
            found = i;
            break;
       }
    }
    if(found != -1)
        printf("The value %d was found in index %d\n", value, found);
    else
        printf("The value %d was not found.\n", value);
}
Return statement - good practices

See also:

Previous keyword: register

Next keyword: short

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!


› register

Did this help? Support me with your vote ;-)

Did this help?