0
0
C Sharp (C#)programming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Return values and void methods
Method Called
Execute Method Body
Is method void?
YesNo 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.