C# Return Values
Write a method named twice that takes an int named n and sends back twice that number. The header starts with int, not void: static int twice(int n). Inside the braces write return n * 2;. In Main, write the result of the call: Console.WriteLine(twice(7));. The screen should show one line: 14. twice itself does not call WriteLine. It only hands 14 back. Main writes that number. That hand-back is what this page teaches. Keep the public class named Pine.
Hand a number back to Main
On the parameters page, greet and show wrote inside the method body. The call sent a value in, and the method wrote to the screen. Here the method still receives a value, but it also sends a value out. The word return marks that outgoing value. Main can store it or write it.
Open the Pine file. The class name stays Pine. This page does not rename the type. Delete any greet demo that only writes hi lena inside the method if that shape is still all you have in Main. Delete any ring(); leftover. Delete any ReadLine line. The file you keep has no wait for typing.
Outside Main, still inside the class braces, the header says int because the value that comes back is an int. Between the parentheses, int n is still a parameter — same idea as the last page. The body has one statement: return n * 2;. That multiplies n by 2 and sends the product back to whoever called twice. There is no Console.WriteLine inside twice on this page. Save the file before you run. The full first program is in the next box.
The file you keep
This is the full small program. twice computes. Main writes.
using System;
public class Pine
{
public static void Main()
{
Console.WriteLine(twice(7));
}
static int twice(int n)
{
return n * 2;
}
}
Read it from the top. using System; prepares short names. public class Pine names the type. public static void Main() is where the runtime starts. The call twice(7) sends 7 into n. static int twice(int n) holds return n * 2;. Console.WriteLine writes the value that came back. One line on the screen: 14.
Run it with dotnet run. The program writes one line and stops. It does not wait for you to type. If dotnet run waits, ReadLine is still in the file. Delete that line and keep Console.WriteLine(twice(7)); plus the twice method above. The screen shows 14. If the screen shows hi lena, a leftover greet call is still running. If the screen shows the letters twice, a writing line has quotes around that word: Console.WriteLine("twice");. You do not reinstall the .NET SDK for a screen that shows the wrong line.
Walk one full run in order. C# starts at Main. The call twice(7) sends 7 into n. Inside twice, return n * 2; computes 14 and hands that 14 back to Main. Then WriteLine writes 14. twice never wrote to the screen by itself. If you change the call to twice(3), the screen should show 6. The method body stays the same; only the argument changes, and the returned product changes with it. If the screen shows 7 instead of 14, you wrote n inside twice or wrote the argument in Main without calling twice — put the doubling on the return line and write the call’s result.
The important split is who talks to the screen. On this page, twice talks only to Main through return. Main talks to the screen through WriteLine. That is why the header can no longer say void: something must come back for Main to write. When you store the result in result first, you are still using that same hand-back; you only delay the write by one line.
Write the value Main received
You can also keep the returned value in a name inside Main before you write. Change only the number in the call if you want a different screen line. Leave return n * 2; alone.
using System;
public class Pine
{
public static void Main()
{
int result = twice(7);
Console.WriteLine(result);
}
static int twice(int n)
{
return n * 2;
}
}
This is the second file. It teaches the same return with a stored name. Run dotnet run. That still shows one line: 14. The call finishes first and fills result. Then WriteLine writes result. Both shapes — writing the call directly, or storing then writing — use the same return from twice. twice(0) should write 0. twice(10) should write 20. Each time Main writes whatever twice handed back. After you have seen the stored shape, put the kept one-line call file back if that is the shape you want to save: Console.WriteLine(twice(7)); in Main.
If twice still says void in the header but the body has return n * 2;, the build writes an error — void means nothing comes back, so a return with a value does not fit. If the header says int but the body has no return, the build writes an error too — an int method must send an int back on this beginner shape. If the body writes and never returns, you are back on the parameters page’s idea; this page wants the number in Main’s hands.
void versus a typed return
Read the two shapes side by side. A void method runs its body and ends. A method with a typed return must hand a value back with return. Main treats the call as that value when the return type is not void.
📊 void body versus return
| Shape | What the method does | What Main often does |
|---|---|---|
void show(int n) |
writes inside the body | calls show(9); and may write nothing else |
int twice(int n) |
return n * 2; |
writes or stores twice(7) |
A common mix-up is putting Console.WriteLine(n * 2); inside twice and leaving the header as int without a return. Then the build complains, or you change the header back to void and you no longer have a value for Main to write from the call. Another mix-up is writing return; with no value while the header says int. Keep return n * 2; so the type of the returned expression matches int. Writing Console.WriteLine("twice"); in Main writes the word twice; it does not call the method. Keep Console.WriteLine(twice(7)) when you want 14 on the screen.
A leftover greet demo that only writes hi lena inside the method can sit in the same file and confuse the screen. Clear Main down to the WriteLine that uses twice, and keep one method that returns. Do not nest twice inside Main’s braces. Methods sit inside the class, next to Main.
You can write static int twice(int n), return a computed value, and write that value from Main. The screen shows the number Main received, not a line the method wrote on its own. void is the wrong header when you need a value back. The next page is overloading, where more than one method can share a name when their parameter lists differ.
The screen holds what Main received
The file you keep writes 14 when Main writes twice(7). The word return is in the method body, and return is not on the screen as a printed label. Quotes around twice write letters, not the call’s result. Nesting twice inside Main is not the shape this lesson builds.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top