Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a function named add that returns an int.
C++
int [1](int a, int b); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than 'add'.
Omitting the function name.
✗ Incorrect
The function declaration requires the function name after the return type. Here, the function name is
add.2fill in blank
mediumComplete the code to define the function add that returns the sum of two integers.
C++
int add(int a, int b) {
return a [1] b;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting the return statement.
✗ Incorrect
The function should return the sum, so the operator is
+.3fill in blank
hardFix the error in the function definition to correctly declare the return type.
C++
[1] add(int a, int b) { return a + b; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
void as return type when the function returns a value.Using floating-point types when integers are returned.
✗ Incorrect
The function returns an integer sum, so the return type must be
int.4fill in blank
hardFill both blanks to declare and define a function named multiply that returns the product of two int parameters.
C++
int [1](int x, int y) { return x [2] y; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition operator instead of multiplication.
Using wrong function name.
✗ Incorrect
The function name is
multiply and the operator for multiplication is *.5fill in blank
hardFill all three blanks to declare and define a function named maxValue that returns the greater of two int values.
C++
int [1](int a, int b) { if (a [2] b) { return a; } else { return [3]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than operator instead of greater than.
Returning the wrong variable in the else block.
✗ Incorrect
The function name is
maxValue. The condition checks if a is greater than b using >. If not, it returns b.