Home

return Statement

If a function does not return a result value (e.g. void Foo()), a return statement without a value may be used to exit the function before the normal end of its code block. If a function does return a result value, (e.g. int Foo()), a return statement with a value is required at the end of its code block. One or more return statements with return values are also allowed earlier in the code block.

void loop(void)
{
  int x = SomeFunction();
  if (x < 0) return;  // Ignore negative numbers
  DoSomethingNatural(x)
}

int Multiply(int x, int y)
{
  return x*y;
}

float Invert(float x)
{
  // Approximate 1/0 (∞ i.e. infinity) as
  // maximium possible single precision floating point value

  if (x == 0.0) return FLT_MAX;

  return 1.0/x;
}

Home


Questions or comments to phil@munts.net