Declare before use
Fix code that calls functions the compiler has not seen yet. An easy embedded C problem (Functions), run and graded in the browser.
easy · Functions · prototypes · debugging
The compiler reads a file from top to bottom. A function must be declared before the line that calls it.
This code does not compile. Fix it so that triple_plus_one(x) returns three times x, plus one. Keep all three functions.
Starter code
int triple_plus_one(int x) {
return add_one(triple(x));
}
int triple(int x) {
return x * 3;
}
int add_one(int x) {
return x + 1;
}Example tests
Two
printf("%d\n", triple_plus_one(2));Expected output:
7Zero
printf("%d\n", triple_plus_one(0));Expected output:
1Learn it first
- Functions · Splitting a program into named pieces, passing values in and getting results back.
More Functions problems
- Build on a helper · Write one function and use it inside another.
- 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.
- Leap year · Turn a rule with exceptions into a logical expression.
- Distance between two values · Reuse a helper to keep the main function simple.