Bird
Raised Fist0
C++programming~10 mins

Return values in C++ - Interactive Code Practice

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

Complete the code to return the sum of two integers.

C++
int add(int a, int b) {
    return [1];
}
Drag options to blanks, or click blank then click option'
Aa * b
Ba - b
Cb - a
Da + b
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Returning one of the parameters alone.
2fill in blank
medium

Complete the code to return the length of a string.

C++
#include <string>

int getLength(const std::string& str) {
    return [1];
}
Drag options to blanks, or click blank then click option'
Astr.size()
Bstr.length
Cstr.count()
Dstr.length()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count()' which is not a string method.
Using 'length' without parentheses.
3fill in blank
hard

Fix the error in the function to correctly return the maximum of two numbers.

C++
int max(int x, int y) {
    if (x > y) {
        return [1];
    } else {
        return y;
    }
}
Drag options to blanks, or click blank then click option'
Ax
Bx > y
Cy
Dy > x
Attempts:
3 left
💡 Hint
Common Mistakes
Returning y inside the if block.
Returning a boolean expression instead of a variable.
4fill in blank
hard

Fill both blanks to create a function that returns true if a number is even.

C++
bool isEven(int num) {
    return num [1] 2 [2] 0;
}
Drag options to blanks, or click blank then click option'
A%
B==
C!=
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' instead of '%'.
Using '!=' instead of '=='.
5fill in blank
hard

Fill all three blanks to create a function that returns a new string with the first character capitalized.

C++
#include <string>
#include <cctype>

std::string capitalize(const std::string& s) {
    if (s.empty()) return s;
    std::string result = s;
    result[[1]] = std::toupper(s[[2]]);
    return [3];
}
Drag options to blanks, or click blank then click option'
A0
B1
Cresult
Ds
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 instead of 0 for the first character.
Returning the original string 's' instead of the modified 'result'.

Practice

(1/5)
1. What does the return statement do in a C++ function?
easy
A. It sends a value back to the place where the function was called.
B. It prints a value to the screen.
C. It declares a new variable inside the function.
D. It ends the program immediately.

Solution

  1. Step 1: Understand the purpose of return

    The return statement is used to send a value from a function back to the caller.
  2. Step 2: Differentiate from other statements

    Printing to screen or declaring variables are different actions; return specifically sends a value back.
  3. Final Answer:

    It sends a value back to the place where the function was called. -> Option A
  4. Quick Check:

    Return sends value back [OK]
Hint: Remember: return sends value back to caller [OK]
Common Mistakes:
  • Confusing return with printing output
  • Thinking return declares variables
  • Believing return ends the whole program
2. Which of the following is the correct syntax to return an integer value 5 from a function in C++?
easy
A. return = 5;
B. return(5)
C. return 5;
D. return: 5;

Solution

  1. Step 1: Recall correct return syntax

    In C++, the correct way to return a value is return value; with a semicolon.
  2. Step 2: Check each option

    return 5; uses return 5; which is correct. return(5) misses semicolon. Options C and D use invalid syntax.
  3. Final Answer:

    return 5; -> Option C
  4. Quick Check:

    Correct return syntax ends with semicolon [OK]
Hint: Use 'return value;' with semicolon [OK]
Common Mistakes:
  • Omitting semicolon after return
  • Using '=' with return
  • Using colon ':' instead of semicolon
3. What will be the output of the following code?
int add() {
  return 2 + 3;
}

int main() {
  int result = add();
  std::cout << result;
  return 0;
}
medium
A. 23
B. 5
C. 0
D. Compilation error

Solution

  1. Step 1: Analyze the add() function

    The function returns the sum of 2 + 3, which is 5.
  2. Step 2: Check main() output

    Main calls add(), stores 5 in result, then prints it using std::cout.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    2 + 3 = 5 [OK]
Hint: Add inside return, then print result [OK]
Common Mistakes:
  • Thinking return concatenates numbers
  • Confusing return value with print output
  • Expecting zero without calculation
4. Identify the error in this function and choose the correct fix:
int multiply(int a, int b) {
  a * b;
}
medium
A. Add return before a * b;
B. Change function return type to void
C. Add semicolon after function declaration
D. Replace a * b; with return a + b;

Solution

  1. Step 1: Check function body

    The function calculates a * b; but does not return it.
  2. Step 2: Fix missing return

    To return the product, add return before a * b;.
  3. Final Answer:

    Add return before a * b; -> Option A
  4. Quick Check:

    Function must return value if return type is int [OK]
Hint: Return the calculated value explicitly [OK]
Common Mistakes:
  • Forgetting to use return statement
  • Changing return type unnecessarily
  • Confusing multiplication with addition
5. You want a function that returns the larger of two integers. Which of these implementations correctly returns the larger value?
hard
A. int max(int x, int y) { return; }
B. int max(int x, int y) { if (x > y) return y; else return x; }
C. int max(int x, int y) { return x + y; }
D. int max(int x, int y) { if (x > y) return x; else return y; }

Solution

  1. Step 1: Understand the goal

    The function should return the larger of x and y.
  2. Step 2: Check each option logic

    int max(int x, int y) { if (x > y) return x; else return y; } returns x if x > y, else y, which is correct. int max(int x, int y) { if (x > y) return y; else return x; } reverses logic. int max(int x, int y) { return x + y; } returns sum, not max. int max(int x, int y) { return; } returns nothing.
  3. Final Answer:

    int max(int x, int y) { if (x > y) return x; else return y; } -> Option D
  4. Quick Check:

    Return larger value using if-else [OK]
Hint: Return x if greater, else y [OK]
Common Mistakes:
  • Swapping return values in if-else
  • Returning sum instead of max
  • Missing return value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard