Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

return — ending a function

The return statement ends the current function call. It can deliver a result to the caller according to the function's return type.

Version: C23; repository compiler flag, production runtime not attested

Syntax

return;
return expression;

return ends execution of the entire current function, including from inside a loop. A function can contain multiple return statements.

return; is allowed only in a function returning void. The form return expression; requires another return type. The expression's value becomes the call's result; if the types differ, it is converted as if assigned to an object of the return type.

In the example, a negative argument triggers an early return. The calls use only values safe for -x; absolute does not generally handle the case where negation exceeds the range of int.

Examples

#include <stdio.h>

static int absolute(int x)
{
    if (x < 0) {
        return -x;
    }
    return x;
}

int main(void)
{
    printf("%d %d\n", absolute(-7), absolute(4));
    return 0;
}

Result

7 4

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources