Build on a helper
Write one function and use it inside another. An easy embedded C problem (Functions), run and graded in the browser.
easy · Functions · functions · calling
Write two functions.
squarereturnsxmultiplied by itself.sum_of_squaresreturns the square ofaplus the square ofb. It should callsquareto do the work.
Starter code
int square(int x) {
return 0;
}
int sum_of_squares(int a, int b) {
return 0;
}Example tests
The helper on its own
printf("%d\n", square(5));Expected output:
25Using the helper twice
printf("%d\n", sum_of_squares(3, 4));Expected output:
25Learn it first
- Functions · Splitting a program into named pieces, passing values in and getting results back.
More Functions problems
- Return true or false · Write a function that answers a yes-or-no question.
- Remember a value between calls · Keep state in a variable outside the functions.
- A function with memory · Use a static local variable to count calls.
- Declare before use · Fix code that calls functions the compiler has not seen yet.
- Leap year · Turn a rule with exceptions into a logical expression.
- Distance between two values · Reuse a helper to keep the main function simple.