0
0
Cprogramming~10 mins

Why functions are needed - 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]() {
    printf("Hello!\n");
}
Drag options to blanks, or click blank then click option'
Aprint
Bmain
Cgreet
Dhello
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'main' instead of the function name.
Using 'print' which is not a valid function name in C.
Using 'hello' which is not the intended function name.
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
Bhello
Cprint
Dmain
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'print()' which is not defined.
Calling 'hello()' which is not defined.
Calling 'main()' inside main causes recursion.
3fill in blank
hard

Fix the error in the function definition by completing the return type.

C
[1] greet() {
    printf("Hello!\n");
}
Drag options to blanks, or click blank then click option'
Avoid
Bint
Cchar
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' without a return statement causes warnings.
Using 'char' or 'float' which are incorrect for this function.
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 a value is returned.
Using '-' instead of '+' for addition.
5fill in blank
hard

Fill all three blanks to create a function that checks if a number is even and returns 1 if true, else 0.

C
[1] is_even(int num) {
    if (num [2] 2 == 0) {
        return [3];
    } else {
        return 0;
    }
}
Drag options to blanks, or click blank then click option'
Aint
B%
C1
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '%' to check remainder.
Returning 0 instead of 1 when number is even.