# Beginner question: Confusion over the meaning of "void"

• ### Question

• As I understand it, void is used when a method doesn't return a value. However, in my tutorial I'm instructed to use void for a method that I assume returns a value:

public void CalculateAnnualFee()
{
annualFee = 100 + 12 * 30;
}

I assume I don't properly understand the concept, so could someone explain what I'm missing, please? Thanks in advance, and my apologies for asking so many questions on the forum. You've all been extremely helpful.

Saturday, October 14, 2017 12:31 AM

• One of the advantages of using a return type/value from a method is that
it allows use of the value in compound statements - those where more than
one operation are chained or linked together to form more compact code.
Consider these two examples of making  a calculation and displaying the
result:

```    class Program
{

static decimal annualFee = 0; // needed for CalculateAnnualFee()
static void Main(string[] args)
{
// without a return
CalculateAnnualFee();
Console.WriteLine(annualFee);

// with a return
Console.WriteLine(CalculateAnnualFee2());

}

public static void CalculateAnnualFee()
{
annualFee = 100 + 12 * 30;
}

public static decimal CalculateAnnualFee2()
{
return 100 + 12 * 30;
}

}
```

- Wayne

• Marked as answer by Sunday, October 15, 2017 1:45 AM
Saturday, October 14, 2017 2:08 PM
• if you mean "returns control", that is different meaning from what you have in your mind.

"the process flows" is the correct translation for it, I guess.

Saturday, October 14, 2017 2:31 PM
• where is the "return" statement ?

I can not find it.

A return statement is optional for a function whose return type is "void".  As explained by the link referenced in my prior post -

"The `return` statement terminates execution of the method in which it appears and returns control to the calling method. It can also return an optional value. If the method is a `void` type, the `return` statement can be omitted. "

• Marked as answer by Sunday, October 15, 2017 1:44 AM
Saturday, October 14, 2017 2:44 PM
• Your assumption that the method should return a value is the issue. The method you show calculates a value named annualFee. Since annualFee, is not declared within the method or within it's argument space then it must have a scope that is outside of that method in order to be referenced by the method.

Ibrahim Malluf MCP http://www.malluf.com

• Edited by Saturday, October 14, 2017 5:50 PM
• Marked as answer by Sunday, October 15, 2017 1:44 AM
Saturday, October 14, 2017 5:47 PM

### All replies

• You can use like

```public void A(out long areturn){
areturn=100+12*30;
return;
}

Or

public void A(ref long areturn){
areturn=100+12*30;
return;
}```

Saturday, October 14, 2017 12:35 AM
• Hi. Thanks for the response. I'm not looking for alternative solutions, as I don't understand those concepts. I'm very much a beginner and just trying to understand what void means.

My tutorial says void is used when a method doesn't return a value. However, the method I posted above does return a value. Perhaps I'm misunderstanding what it means to "return a value" in C#?

Saturday, October 14, 2017 12:39 AM
• Can you link any of the "tutorial" that tell you to use void for returns a value ?

That sounds pretty not correct to me.

Point is simple "Void does not return anything".

Saturday, October 14, 2017 12:00 PM
• See return (C# Reference) and its example CalculateArea function.  Note the the return value of the function can be assigned to a variable.

This cannot be done with a function whose return type is void.

Saturday, October 14, 2017 1:25 PM

• My tutorial says void is used when a method doesn't return a value. However, the method I posted above does return a value. Perhaps I'm misunderstanding what it means to "return a value" in C#?

What "return" usually refers to in the context of methods is what value
and type the method passes back after it is executed - the type at the
left of the method name.

double MyMethod(); // returns a double

double dbl = MyMethod(); // dbl now holds the value returned by MyMethod

The fact that a method stores values in variables during the execution of
the method has nothing to do with what the method returns. A method may
alter variables passed to it via its arguments, or variables defined
elsewhere - such as class member variables - and not *return* anything.
That's what a void return means - the method does not pass back anything
that can be assigned, or compared, or used in any way.

Return Values
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods#return-values

"Methods can return a value to the caller. If the return type, the type
listed before the method name, is not void, the method can return the
value by using the return keyword. A statement with the return keyword
followed by a value that matches the return type will return that value
to the method caller."

Methods (C# Programming Guide)
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods

- Wayne

Saturday, October 14, 2017 1:47 PM
• One of the advantages of using a return type/value from a method is that
it allows use of the value in compound statements - those where more than
one operation are chained or linked together to form more compact code.
Consider these two examples of making  a calculation and displaying the
result:

```    class Program
{

static decimal annualFee = 0; // needed for CalculateAnnualFee()
static void Main(string[] args)
{
// without a return
CalculateAnnualFee();
Console.WriteLine(annualFee);

// with a return
Console.WriteLine(CalculateAnnualFee2());

}

public static void CalculateAnnualFee()
{
annualFee = 100 + 12 * 30;
}

public static decimal CalculateAnnualFee2()
{
return 100 + 12 * 30;
}

}
```

- Wayne

• Marked as answer by Sunday, October 15, 2017 1:45 AM
Saturday, October 14, 2017 2:08 PM
• where is the "return" statement ?

I can not find it.

Saturday, October 14, 2017 2:29 PM
• if you mean "returns control", that is different meaning from what you have in your mind.

"the process flows" is the correct translation for it, I guess.

Saturday, October 14, 2017 2:31 PM
• where is the "return" statement ?

I can not find it.

A return statement is optional for a function whose return type is "void".  As explained by the link referenced in my prior post -

"The `return` statement terminates execution of the method in which it appears and returns control to the calling method. It can also return an optional value. If the method is a `void` type, the `return` statement can be omitted. "

• Marked as answer by Sunday, October 15, 2017 1:44 AM
Saturday, October 14, 2017 2:44 PM
• you do not have to reply me.

i do not have any confusion with "return"

regards

Saturday, October 14, 2017 3:29 PM
• Your assumption that the method should return a value is the issue. The method you show calculates a value named annualFee. Since annualFee, is not declared within the method or within it's argument space then it must have a scope that is outside of that method in order to be referenced by the method.

Ibrahim Malluf MCP http://www.malluf.com

• Edited by Saturday, October 14, 2017 5:50 PM
• Marked as answer by Sunday, October 15, 2017 1:44 AM
Saturday, October 14, 2017 5:47 PM
• Thanks for all the responses. You've clarified a few things for me. I'm still not entirely sure when it's appropriate to use void, but hopefully I'll get that later. Thanks again.
Sunday, October 15, 2017 1:44 AM