[Solved] What does it mean when a function does not return a value?


The potential effects of a function include:

  • Returning a value.
  • Modifying an object, including either objects the function can access through pointers or objects with external linkage.
  • Accessing a volatile object, as above.1
  • Modifying a file (a stream).

So, if a function does not return a value, it can still modify an object, as in:

void SetState(SomeStructure *S, int Value)
{
    S->State = Value;
}

or it can modify a stream:

void PrintValue(FILE *Stream, int Value)
{
    fprintf(Stream, "%d", Value);
}

You can tell the compiler that a function does not return a value by declaring it with return type void. However, the C standard also permits functions with non-void return types not to return a value. For example, you can have a function that sets or gets a value depending upon a command parameter:

int AccessState(int Command, SomeStructure *S,...)
{
    switch (Command)
    {
        case 0: // Get the state.
            return S->Value;
        case 1: // Set the state from the optional int parameter following S.
             va_list ap;
             va_start(ap, S);
             S->Value = va_arg(S, int);
             va_end(ap);
             break;
    }
}

In this case, there is no way to tell the compiler that the function sometimes does not return a value, but you do not need to. However, you should tell humans that use and read the code that the function sometimes does and sometimes does not return a value with clear documentation in comments or elsewhere.

Footnote

1 Accessing a volatile object is one way for C programs to do things outside the C standard, such as sending commands or data to physical devices.

solved What does it mean when a function does not return a value?