Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Return values and void methods in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Return values and void methods
Method Called
↓
Execute Method Body
↓
Is method void?
Yes→No return value, end method
No↓
Return value to caller
↓
Continue execution after method call
When a method is called, it runs its code. If it returns a value, that value goes back to the caller. If it's void, it just finishes without returning anything.
Execution Sample
C Sharp (C#)
int Add(int a, int b) {
    return a + b;
}

void PrintSum(int x, int y) {
    Console.WriteLine(x + y);
}
Defines a method that returns the sum of two numbers and a void method that prints the sum.
Execution Table
StepActionMethodParametersReturn ValueOutput
1Call Add(3, 4)Adda=3, b=47
2Return 7 to callerAdd7
3Call PrintSum(5, 6)PrintSumx=5, y=6void
4Print 11 to consolePrintSumvoid11
5End of PrintSum methodPrintSumvoid
💡 Methods finish after returning a value or completing void execution.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
a-3--
b-4--
x--5-
y--6-
Return Value-7void-
Key Moments - 2 Insights
Why does the Add method return a value but PrintSum does not?
Add has a return statement that sends a value back (see step 2 in execution_table). PrintSum is declared void, so it does not return anything and just finishes after printing (steps 3-5).
What happens if you try to use the return value of a void method?
Void methods do not produce a value, so trying to use their return value will cause an error. The execution_table shows PrintSum returns 'void' and no value is passed back.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value of Add(3,4) at step 2?
Avoid
B7
C11
D3
💡 Hint
Check the 'Return Value' column at step 2 in the execution_table.
At which step does PrintSum output to the console?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Output' column for PrintSum in the execution_table.
If Add method did not have a return statement, what would happen?
AIt would cause a compile error
BIt would return 0 by default
CIt would behave like a void method
DIt would return null
💡 Hint
In C#, non-void methods must return a value; see the concept_snapshot for rules.
Concept Snapshot
Method with return value:
- Declared with a type (e.g., int)
- Uses 'return' to send value back
- Caller can use returned value

Void method:
- Declared with 'void'
- Does not return value
- Performs actions like printing

All methods end after return or finishing code.
Full Transcript
When you call a method, the program runs the code inside it. If the method is declared to return a value, it must use the 'return' keyword to send that value back to where it was called. For example, the Add method takes two numbers and returns their sum. The caller can then use this returned number. On the other hand, a void method does not return any value. It just runs its code and finishes. For example, PrintSum adds two numbers and prints the result but does not send anything back. Trying to get a return value from a void method causes an error. Also, if a method is declared to return a value but does not have a return statement, the program will not compile. This is because C# requires all non-void methods to return a value. Understanding these rules helps you write methods that either give back results or just perform actions.

Practice

(1/5)
1. Which statement correctly describes a void method in C#?
easy
A. It does not return any value.
B. It must return an integer value.
C. It always returns a string.
D. It returns a boolean value.

Solution

  1. Step 1: Understand the meaning of void in C#

    The keyword void means the method does not return any value.
  2. Step 2: Compare with other return types

    Methods with other return types like int, string, or bool must return a value of that type, but void methods do not return anything.
  3. Final Answer:

    It does not return any value. -> Option A
  4. Quick Check:

    Void means no return value [OK]
Hint: Void means no value comes back from the method [OK]
Common Mistakes:
  • Thinking void methods return a default value
  • Confusing void with returning null
  • Assuming void methods must return something
2. Which of the following is the correct syntax for a method that returns an integer value in C#?
easy
A. public void GetNumber() { return 5; }
B. public void GetNumber() { Console.WriteLine(5); return 5; }
C. public int GetNumber() { Console.WriteLine(5); }
D. public int GetNumber() { return 5; }

Solution

  1. Step 1: Identify method return type and return statement

    The method declares int return type, so it must return an integer value using return.
  2. Step 2: Check each option for correct syntax

    public int GetNumber() { return 5; } correctly declares int and returns 5. public void GetNumber() { return 5; } is void but tries to return a value (invalid). public int GetNumber() { Console.WriteLine(5); } returns nothing despite int return type. public void GetNumber() { Console.WriteLine(5); return 5; } is void but tries to return a value (invalid).
  3. Final Answer:

    public int GetNumber() { return 5; } -> Option D
  4. Quick Check:

    Return type matches returned value [OK]
Hint: Return type must match the returned value type [OK]
Common Mistakes:
  • Returning a value from a void method
  • Not returning a value from a non-void method
  • Using Console.WriteLine instead of return
3. What is the output of the following C# code?
public class Program {
  public static int Add(int a, int b) {
    return a + b;
  }
  public static void Main() {
    int result = Add(3, 4);
    Console.WriteLine(result);
  }
}
medium
A. 7
B. 34
C. 0
D. Compile error

Solution

  1. Step 1: Understand the Add method

    The method Add takes two integers and returns their sum.
  2. Step 2: Trace the Main method execution

    Main calls Add(3, 4), which returns 7. Then it prints 7 to the console.
  3. Final Answer:

    7 -> Option A
  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 void method to return value
4. Identify the error in this C# method:
public int Multiply(int x, int y) {
  int product = x * y;
}
medium
A. Incorrect method name.
B. Variables cannot be declared inside methods.
C. Missing return statement for non-void method.
D. Method parameters must be strings.

Solution

  1. Step 1: Check method return type and body

    The method declares int return type but does not return any value.
  2. Step 2: Identify missing return statement

    Since the method is not void, it must have a return statement returning an integer value like return product;.
  3. Final Answer:

    Missing return statement for non-void method. -> Option C
  4. Quick Check:

    Non-void methods must return a value [OK]
Hint: Non-void methods must always return a value [OK]
Common Mistakes:
  • Forgetting to return a value in non-void methods
  • Thinking variable declaration is invalid inside methods
  • Assuming parameters must be strings
5. You want to create a method that prints a greeting message but does not return any value. Which method signature and body is correct in C#?
hard
A. public string Greet() { Console.WriteLine("Hello!"); return "Hello!"; }
B. public void Greet() { Console.WriteLine("Hello!"); }
C. public void Greet() { return "Hello!"; }
D. public string Greet() { Console.WriteLine("Hello!"); }

Solution

  1. Step 1: Understand method purpose and return type

    The method should print a message and not return any value, so it should be void.
  2. Step 2: Check each option for correctness

    public void Greet() { Console.WriteLine("Hello!"); } correctly uses void and prints the message without returning anything. public string Greet() { Console.WriteLine("Hello!"); return "Hello!"; } returns a string, which is not needed. public void Greet() { return "Hello!"; } tries to return a string in a void method (invalid). public string Greet() { Console.WriteLine("Hello!"); } declares string return type but does not return anything (invalid).
  3. Final Answer:

    public void Greet() { Console.WriteLine("Hello!"); } -> Option B
  4. Quick Check:

    Void method prints only, no return [OK]
Hint: Void methods do actions, no return needed [OK]
Common Mistakes:
  • Returning a value from a void method
  • Declaring return type but not returning
  • Confusing printing with returning
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