Simvik

Distance between two values

Reuse a helper to keep the main function simple. An easy embedded C problem (Functions), run and graded in the browser.

easy · Functions · functions · if

Write two functions.

  • absolute returns x without its sign, so both 5 and -5 give 5.
  • distance returns how far apart a and b are. The answer is never negative, whichever is larger.

Starter code

int absolute(int x) {
  return x;
}

int distance(int a, int b) {
  return a - b;
}

Example tests

Second value larger

printf("%d\n", distance(3, 10));

Expected output:

7

First value larger

printf("%d\n", distance(10, 3));

Expected output:

7

Learn it first

  • Functions · Splitting a program into named pieces, passing values in and getting results back.

More Functions problems