Bird
Raised Fist0
Cprogramming~20 mins

Return values in C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Return Value Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this C function call?

Consider the following C code snippet. What will be printed when printf("%d", add(3, 4)); is executed?

C
int add(int a, int b) {
    return a + b;
}

int main() {
    printf("%d", add(3, 4));
    return 0;
}
ACompilation error
B34
C0
D7
Attempts:
2 left
💡 Hint

Remember that the return statement sends back the sum of the two numbers.

❓ Predict Output
intermediate
2:00remaining
What value does this function return?

What is the return value of the function mystery when called with mystery(5)?

C
int mystery(int x) {
    if (x > 0) {
        return x * 2;
    } else {
        return -1;
    }
}
A10
B-1
C0
DUndefined behavior
Attempts:
2 left
💡 Hint

Check the condition and what happens when x is positive.

❓ Predict Output
advanced
2:00remaining
What is the output of this recursive function?

What will be printed when printf("%d", factorial(4)); is executed?

C
int factorial(int n) {
    if (n == 0) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

int main() {
    printf("%d", factorial(4));
    return 0;
}
A24
B10
C0
DStack overflow error
Attempts:
2 left
💡 Hint

Recall that factorial of 4 is 4 * 3 * 2 * 1.

❓ Predict Output
advanced
2:00remaining
What error does this function cause?

What happens when int result = no_return(); is executed given the function below?

C
int no_return() {
    int x = 5;
    x += 3;
    // Missing return statement
}

int main() {
    int result = no_return();
    printf("%d", result);
    return 0;
}
APrints 8
BCompilation warning or undefined behavior
CPrints 0
DRuntime error: segmentation fault
Attempts:
2 left
💡 Hint

Functions declared to return a value must have a return statement.

🧠 Conceptual
expert
2:00remaining
What is the value of x after this function call?

Given the code below, what is the value of x after x = update(10); is executed?

C
int update(int a) {
    a = a + 5;
    return a;
}

int main() {
    int x = 0;
    x = update(10);
    return 0;
}
A0
B10
C15
DCompilation error
Attempts:
2 left
💡 Hint

The function returns the updated value of a.

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 on the screen.
C. It declares a new variable inside the function.
D. It pauses the program execution.

Solution

  1. Step 1: Understand the purpose of return

    The return statement sends a value from the function back to the caller.
  2. Step 2: Differentiate from other actions

    Printing, declaring variables, or pausing are not what return does.
  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: Return sends value back to caller, not print or pause [OK]
Common Mistakes:
  • Thinking return prints output
  • Confusing return with variable declaration
  • Assuming return pauses program
2. Which of the following is the correct syntax for a function that returns an integer in C?
easy
A. int myFunction() { return 5; }
B. void myFunction() { return 5; }
C. int myFunction() { print(5); }
D. myFunction() int { return 5; }

Solution

  1. Step 1: Check function return type and return statement

    The function declares int return type and returns an integer value 5.
  2. Step 2: Identify incorrect options

    void myFunction() { return 5; } returns a value but declares void (no return). int myFunction() { print(5); } uses print which is invalid in C. myFunction() int { return 5; } has wrong syntax.
  3. Final Answer:

    int myFunction() { return 5; } -> Option A
  4. Quick Check:

    Return type matches returned value [OK]
Hint: Return type must match returned value type [OK]
Common Mistakes:
  • Using void but returning a value
  • Using print instead of return
  • Wrong function syntax
3. What is the output of this C program?
int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(3, 4);
    printf("%d", result);
    return 0;
}
medium
A. 34
B. 7
C. 0
D. Error

Solution

  1. Step 1: Understand the add function

    The function adds two integers and returns their sum.
  2. Step 2: Calculate the sum of 3 and 4

    3 + 4 equals 7, which is stored in result and printed.
  3. Final Answer:

    7 -> Option B
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Add returns sum; printed result is sum [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Confusing return with print
  • Expecting zero or error output
4. Identify the error in this C function:
int multiply(int x, int y) {
    int product = x * y;
}
medium
A. Variables x and y are not declared.
B. Incorrect multiplication operator.
C. Function name should be void.
D. Missing return statement to return the product.

Solution

  1. Step 1: Check function return type and body

    The function declares it returns an int but does not return any value.
  2. Step 2: Identify missing return statement

    It calculates product but never returns it, causing undefined behavior.
  3. Final Answer:

    Missing return statement to return the product. -> Option D
  4. Quick Check:

    Return missing in non-void function [OK]
Hint: Non-void functions must return a value [OK]
Common Mistakes:
  • Forgetting to return value in non-void function
  • Thinking multiplication operator is wrong
  • Assuming variables are undeclared
5. You want to write a function that returns the maximum of three integers. Which of the following implementations correctly returns the maximum value?
hard
A. int maxOfThree(int a, int b, int c) { if (a > b && a > c) return a; else if (b > a && b > c) return b; else return c; }
B. int maxOfThree(int a, int b, int c) { return a > b > c ? a : b > c ? b : c; }
C. int maxOfThree(int a, int b, int c) { int max = a; if (b > max) max = b; if (c > max) max = c; return max; }
D. int maxOfThree(int a, int b, int c) { if (a > b) return a; else if (b > c) return b; else return c; }

Solution

  1. Step 1: Analyze int maxOfThree(int a, int b, int c) { if (a > b && a > c) return a; else if (b > a && b > c) return b; else return c; } logic

    This version correctly checks if b is greater than both a and c in the else-if condition, fixing the missing cases in the original version.
  2. Step 2: Analyze int maxOfThree(int a, int b, int c) { int max = a; if (b > max) max = b; if (c > max) max = c; return max; } logic

    This compares all values step-by-step and returns the correct maximum.
  3. Step 3: Analyze options C and D

    int maxOfThree(int a, int b, int c) { return a > b > c ? a : b > c ? b : c; } uses invalid chained comparison. int maxOfThree(int a, int b, int c) { if (a > b) return a; else if (b > c) return b; else return c; } misses some cases like when a is not greater than b but b is not greater than c.
  4. Final Answer:

    int maxOfThree(int a, int b, int c) { int max = a; if (b > max) max = b; if (c > max) max = c; return max; } -> Option C
  5. Quick Check:

    Stepwise max update returns correct max [OK]
Hint: Compare stepwise and return max variable [OK]
Common Mistakes:
  • Using chained comparisons incorrectly
  • Missing some comparison cases
  • Returning before checking all values
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