Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a function named greet.
C++
void [1]() { std::cout << "Hello!" << std::endl; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than 'greet'.
Confusing function name with main.
✗ Incorrect
The function name should be greet as requested.
2fill in blank
mediumComplete the code to call the function greet 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 was not defined.
Forgetting the parentheses after the function name.
✗ Incorrect
To call the function defined as greet, use greet();.
3fill in blank
hardFix the error in the function definition by completing the return type.
C++
[1] greet() { std::cout << "Hello!" << std::endl; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a return type that requires a return statement.
Leaving the return type blank.
✗ Incorrect
The function does not return a value, so its return type should be void.
4fill in blank
hardFill both blanks to create a function that adds two numbers and returns the result.
C++
[1] add(int a, int b) { return a [2] b; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
void as return type when returning a value.Using subtraction
- instead of addition.✗ Incorrect
The function returns an integer, so the return type is int. To add two numbers, use the + operator.
5fill in blank
hardFill all three blanks to create a function that checks if a number is positive.
C++
bool [1](int num) { if (num [2] 0) { return [3]; } else { return false; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using equality operator instead of greater than.
Returning false instead of true when condition is met.
✗ Incorrect
The function name is isPositive. To check if a number is greater than zero, use >. If true, return true.