7 Replies - 1326 Views - Last Post: 02 September 2011 - 07:16 AM Rate Topic: -----

#1 teahou   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 225
  • Joined: 01-April 11

Difference between a returned value and the returned result of a metho

Posted 30 August 2011 - 11:37 AM

Hi there. i am sitting at home learning C# using Rob Miles' 'C# Yellow Book'. I am fairly new to programming (I have done some VB6) and C# is my first go at OOP. I am a little confused on the concept of a 'returned' value and my googling has not helped too much. Here is an example of some code I am working with from Rob's book and it illustrates the area of my confusion:

    class GlazerCalc
    {
        static void Main(string[] args)
        {
            double width, height, woodLength, glassArea;
            string widthString, heightString;

            Console.Write("Enter the width: ");
            widthString = Console.ReadLine();
            width = double.Parse(widthString);

            Console.Write("Enter the height: ");
            heightString = Console.ReadLine();
            height = double.Parse(heightString);

            woodLength = 2 * (width + height) * 3.25;
            glassArea = 2 * (width * height);

            Console.WriteLine("The length of the wood is " +
                woodLength + " feet");

            Console.WriteLine( "The aera of the glass is " +
                glassArea + " square meters" );

            Console.ReadLine();

        }
    }



Now, the MAIN method has the 'void' keyword, which to my understand means this piece of code does not return a value. Now here is my confusion, if you run the program it returns. or displays, the calculated length and area which I interprete as returned values. So I obviously am not understanding the distinction.

So, if someone can either clarify this or shot me a good link, that would be super. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Difference between a returned value and the returned result of a metho

#2 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Difference between a returned value and the returned result of a metho

Posted 30 August 2011 - 12:19 PM

Hi,

You are right that void represents a lack of return type. So, Main() doesn't return a value.

I shall go through the contents of the Main() method, step by step:

1) In the first two lines, you just declare some variables.
2) You then call Console.Write() which is a method that does not return a value, but writes the string you pass in to the console.
3) You then call Console.ReadLine() which is a method that returns a string. The string it returns the the string the user enters in the console.
4) You then call double.Parse() which converts a string to a double data type, and returns that double.
5) Skipping ahead... you perform the relevant calculations, and call Console.WriteLine() which prints out the string you passed into it to the console.

At no point in that sequence does Main() return a value. The values that get printed to the console are a result of you calling Console.Write() and other methods etc.

To return from a method, you use the return statement. That is the single way to return a value from a method in C#.

For example:

int Add(int a, int B)/>{
   int total = a + b;
   return total;
}



That method adds the two passed in integers, and returns the resulting value. Now, when that method is called, an integer will be returned.

That is the return value. The value that the method ultimately returns.


What you are essentially talking about is the method's side effect (not in the purest sense of the term perhaps, but close enough :)), which is the effect the method has on its 'surrounding environment' when you call it. For example, when Main() is called in your program, it prints a load of strings to the console. That is a side effect of the method, but the method doesn't ultimately return any value :)

Take Add() again:

void Add(int a, int B)/>{
   int total = a + b;
   Console.WriteLine(total);
}



Now, add doesn't return any value, but as a side effect of calling it, a string gets printed to the console. Note that the first version with the return value is a much better method (design wise) than the second version.

Methods Reference

This post has been edited by [email protected]: 30 August 2011 - 01:58 PM

Was This Post Helpful? 3
  • +
  • -

#3 teahou   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 225
  • Joined: 01-April 11

Re: Difference between a returned value and the returned result of a metho

Posted 30 August 2011 - 12:33 PM

Ok, i understand. So if I create a method with a void return value, and that method gets called by MAIN, it will run the statements in the code block, producing whatever is in the code but no value will be returned to the MAIN method.

Now, if a method returns something, then it can not have the 'void' parameter, correct? So the following would NOT work, right?:

    class MyClass
    {
        static void Main()
        {
            declare some variables;

            do some stuff;

            return something;
        }
    }


Was This Post Helpful? 0
  • +
  • -

#4 karabasf   User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Difference between a returned value and the returned result of a metho

Posted 30 August 2011 - 12:35 PM

Ok, I am going to give this a shot. The static void Main[string[] arg) is a special method. Now, what you mean with returning, this method does not return anything, rather, it just displays (Hence the Console.Writeline).

Now, this sounds a bit fuzzy, so I will clarify it more. Take a look at the code below: (it's basically your rewritten code)

class GlazerCalc
{
    static void Main(string[] args)
    {
        double width, height, woodLength, glassArea;
        string widthString, heightString;

        Console.Write("Enter the width: ");
        widthString = Console.ReadLine();
        width = double.Parse(widthString);

        Console.Write("Enter the height: ");
        heightString = Console.ReadLine();
        height = double.Parse(heightString);

		//As you can see, I can assign woodLength a value, which is RETURNED from the GetWoodLength method
		double woodLength = GetWoodLength(height, width);
		
		//As you can see, I can assign area a value, which is RETURNED from the GetArea method
		double area = GetArea(height, width);

		//Now, I cannot assign a value, because the mehtod DisplayInformation is a VOID and thus does not return
		DisplayInformation(woodLength, area);
		
        Console.ReadLine();

    }

	//This method returns a double with the value 2 * (width+height)
	static double GetWoodLength(double height, double width)
	{
		return 2 * (height + width);
	}
	
	//This method returns a double with the value 2*width*height
	static double GetArea(double height, double width)
	{
		return 2*width*height;
	}
	
	//This method DISPLAYS the information
	static void DisplayInformation(double woodLength, double glassArea)
	{
		//Output the data according to composite formatting. 
		//Note: This is going to be usefull evenutally, so make sure you take a look at it
		Console.Writeline("The length of the wood is {0}", woodLength);
		Console.WriteLine("The area of the glass is {0} square meters", glassArea);
	}
}



As you can see, when a method returns a value, this means that I can assign that value (or better said, the output) to a type (in the discussed case: a double). Two examples were given in my example code, you can assign the values computed by the GetArea() or the GetWoodLength() method to a type (the doubles woodLength and Area respectively). You could also decide not to assign the output; this will per example compile:

class GlazerCalc
{
    static void Main(string[] args)
    {
        double width, height, woodLength, glassArea;
        string widthString, heightString;

        Console.Write("Enter the width: ");
        widthString = Console.ReadLine();
        width = double.Parse(widthString);

        Console.Write("Enter the height: ");
        heightString = Console.ReadLine();
        height = double.Parse(heightString);

		//As you can see, I can assign woodLength a value, which is RETURNED from the GetWoodLength method
		double woodLength = GetWoodLength(height, width);
		
        Console.ReadLine();

    }

	//This method returns a double with the value 2 * (width+height)
	static double GetWoodLength(double height, double width)
	{
		return 2 * (height + width);
	}
}



However, a type cannot be assigned to a void method, as this will not return a type. Per example, this will give a compiling error:
class GlazerCalc
{
    static void Main(string[] args)
    {
        double width, height, woodLength, glassArea;
        string widthString, heightString;

        Console.Write("Enter the width: ");
        widthString = Console.ReadLine();
        width = double.Parse(widthString);

        Console.Write("Enter the height: ");
        heightString = Console.ReadLine();
        height = double.Parse(heightString);

		//As you can see, I can assign woodLength a value, which is RETURNED from the GetWoodLength method
		double woodLength = GetWoodLength(height, width);
		
		//As you can see, I can assign area a value, which is RETURNED from the GetArea method
		double area = GetArea(height, width);

		//This won't work, because a void method does not return anything
		string Information = DisplayInformation(woodLength, area);
		
        Console.ReadLine();

    }

	//This method returns a double with the value 2 * (width+height)
	static double GetWoodLength(double height, double width)
	{
		return 2 * (height + width);
	}
	
	//This method returns a double with the value 2*width*height
	static double GetArea(double height, double width)
	{
		return 2*width*height;
	}
	
	//This method DISPLAYS the information
	static void DisplayInformation(double woodLength, double glassArea)
	{
		//Output the data according to composite formatting. 
		//Note: This is going to be usefull evenutally, so make sure you take a look at it
		Console.Writeline("The length of the wood is {0}", woodLength);
		Console.WriteLine("The area of the glass is {0} square meters", glassArea);
	}
}



Hopes this helps you out ^^

Edit: 2nd ninja on the day :shuriken:

Edit 2: Indeed, having a void method returning something is not possible, as it is a property of a void of not returning anything. If you would implement your example, you would get a compiling error.

Edit 3: Fix'd my post with Codings comments. Thanks mate. Guess I worked a bit too much with Matlab... (dont have a proper book for it, so my terminology is a bit messed up)

This post has been edited by karabasf: 30 August 2011 - 12:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Difference between a returned value and the returned result of a metho

Posted 30 August 2011 - 12:41 PM

Firstly, be careful with your terminology. 'void' is a type (well, an alias for a type anyway), not a parameter. A parameter is the thing that goes in between the parentheses () of the method, of which allow users to pass in values to the method (parameters = input; return value = output; body = code to use input to get output).

I obviously know what you meant, but be careful not to confuse yourself :)

Anyway, yes you are exactly right. A method that has a 'void' return type cannot return a value. It is as clear cut as that. Your code in that example wouldn't compile.

In the same way, methods with a non void return type MUST return a value of the correct type, in all circumstances.

Finally, it is worth noting that you can do something like this:

static void Main()
{
   //some code here
   return;
   //more code here
}


'
The return statement in that is NOT returning a value. It is just saying 'leave that method now'. So, you can use return statements to leave void methods early. In the above code, '//more code here' would not get run, as we leave the method when we hit the return statement :)

This post has been edited by [email protected]: 30 August 2011 - 12:53 PM

Was This Post Helpful? 2
  • +
  • -

#6 AmbientTech   User is offline

  • I'm an @sshole :)

Reputation: 4
  • View blog
  • Posts: 2,651
  • Joined: 11-August 11

Re: Difference between a returned value and the returned result of a metho

Posted 02 September 2011 - 06:47 AM

This has been educational, as I was not completely clear on how that worked, either. I am also learning from a book and the books I've found don't always explain WHY something works, just HOW to do it.
But I do have a question as a result of what was posted.
At CS-360, in your earlier example you posted:

Quote

1 void Add(int a, int B){

2 int total = a + b;

3 Console.WriteLine(total);

4 }

My question regarding this is, as I understood it (or thought I did) C# is case sesitive. So in that example, wouldn't you get a compile error because you declared int B but executed with b? Are these not the same? I thought that if you declare int B you have to execute with B, but could be mistaken on that point.
Was This Post Helpful? 0
  • +
  • -

#7 karabasf   User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Difference between a returned value and the returned result of a metho

Posted 02 September 2011 - 07:01 AM

View PostAmbientTech, on 02 September 2011 - 03:47 PM, said:

My question regarding this is, as I understood it (or thought I did) C# is case sesitive. So in that example, wouldn't you get a compile error because you declared int B but executed with b? Are these not the same? I thought that if you declare int B you have to execute with B, but could be mistaken on that point.


yes C# is case sensitive, so

void Add(int a, int B)/>
{
  int total = a + b;
}



wouldn't work. But I heard that there's a bug around this forum capitalizing letters (per example 'B' in the post), so...

Edit:
Indeed, it even capitalized mine...

This post has been edited by karabasf: 02 September 2011 - 07:01 AM

Was This Post Helpful? 0
  • +
  • -

#8 AmbientTech   User is offline

  • I'm an @sshole :)

Reputation: 4
  • View blog
  • Posts: 2,651
  • Joined: 11-August 11

Re: Difference between a returned value and the returned result of a metho

Posted 02 September 2011 - 07:16 AM

Aahhh. I hadn't heard of that issue with caps, but that explains an awful lot. Thanks for the info.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1