0
0
C++programming~10 mins

Why functions are needed in C++ - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Agreet
Bmain
Cprint
DsayHello
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than 'greet'.
Confusing function name with main.
2fill in blank
medium

Complete 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'
Agreet
Bprint
CsayHello
Dhello
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a function that was not defined.
Forgetting the parentheses after the function name.
3fill in blank
hard

Fix 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'
Afloat
Bstring
Cint
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using a return type that requires a return statement.
Leaving the return type blank.
4fill in blank
hard

Fill 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'
Aint
B+
C-
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as return type when returning a value.
Using subtraction - instead of addition.
5fill in blank
hard

Fill 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'
AisPositive
B>
Ctrue
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using equality operator instead of greater than.
Returning false instead of true when condition is met.