Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a function named 'foo'.
C
void [1]() {
// function body
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than 'foo'.
✗ Incorrect
The function name should be 'foo' as requested.
2fill in blank
mediumComplete the code to call the function 'foo' inside 'main'.
C
int main() {
[1]();
return 0;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a function that is not defined or named differently.
✗ Incorrect
To call the function 'foo', write 'foo();' inside main.
3fill in blank
hardFix the error in the recursive function call to 'countdown'.
C
void countdown(int n) {
if (n == 0) return;
printf("%d\n", n);
[1](n - 1);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the function name in the recursive call.
✗ Incorrect
The function calls itself by its exact name 'countdown' for recursion.
4fill in blank
hardFill both blanks to create a function that prints numbers from 1 to n using recursion.
C
void print_numbers(int [1]) { if ([2] == 0) return; print_numbers([2] - 1); printf("%d\n", [2]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for parameter and condition.
✗ Incorrect
The parameter name is 'n' and the base case checks if 'n' is zero.
5fill in blank
hardFill all three blanks to create a recursive factorial function.
C
int factorial(int [1]) { if ([2] <= 1) return 1; return [2] * factorial([3] - 1); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing different variable names for the parameter and recursive call.
✗ Incorrect
The parameter is named 'n'. The base case checks 'n'. The recursive call uses 'n - 1'.