3

Could you explain how should I avoid this warning: "Unreachable code detected" in Visual Studio 2010 Express ? I'm learning C# from a manual. It's an exercise for creating a simple method. I'm entering the example precisely as written in the book. Thanks.

public class Multiply 
{
    //Multiplies argument by 4 

    public static int MultiplyBy4(int aNumber)
    {
        return 4 * aNumber;
        //Shows ways of passing various arguments to a method public static void Main
        int x = 7;
        int y = 20;
        int z = -3;
        int result = 0;

        result = MultiplyBy4(x);
        Console.WriteLine("Passsing a variable, x : {0}", result);

        result = MultiplyBy4(y + 2);
        Console.WriteLine("Passing an expression, Y + 2: {0}", result);

        result = 5 + MultiplyBy4(z);
        Console.WriteLine("Using MultiplyBy4 in an expression: {0}", result);
        Console.ReadLine();
    }
 }

I don't understand the manual commentary: "Shows ways of passing various arguments to a method public static void Main" after creating the method with a parameter and a return value. How do I make the method MultiplyBy4 recognize x, y, z as being "aNumber" ? Maybe this is a retarded questions but I'm stuck here. Thanks.

Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
  • It looks like they expect you to comment out the various sections, one at a time and try each in turn (not all at once). The return `4 * aNumber` statement ends the method so the following code is never run. Start by commenting that out. – iCollect.it Ltd Jan 13 '14 at 14:09
  • 2
    I think you need another manual. This is a horrible example. – John Kraft Jan 13 '14 at 14:13
  • I tried commenting out, it doesn't do anything. I agree this is horrible. It was going ok until here. Now it's complete darkness. – user3190322 Jan 16 '14 at 10:02
  • I solved the warning by writing the example as Carsten Heine showed below. It doesn't seem so bad now. – user3190322 Jan 16 '14 at 11:00

5 Answers5

8

The comment line is messed up and contains the definition of the Main method. The code should read:

    public class Multiply 
{
        //Multiplies argument by 4 
        public static int MultiplyBy4(int aNumber)
        {
            return 4 * aNumber;
        }

        //Shows ways of passing various arguments to a method
        public static void Main(string[] args)
        {
                int x = 7;
                int y = 20;
                int z = -3;
                int result = 0;

                result = MultiplyBy4(x);
                Console.WriteLine("Passsing a variable, x : {0}", result);

                result = MultiplyBy4(y + 2);
                Console.WriteLine("Passing an expression, Y + 2: {0}", result);

                result = 5 + MultiplyBy4(z);
                Console.WriteLine("Using MultiplyBy4 in an expression: {0}", result);
                Console.ReadLine();
            }
 }
  • Nice catch but this makes the question off-topic. – Steve Jan 13 '14 at 14:16
  • I wrote it down as you said but now I have 4 red errors. I want to shoot myself in the head. – user3190322 Jan 16 '14 at 10:04
  • You were right. This is how the method works, I understand it now. I opened a new project and wrote everything from scratch. Seems that I had an issue with too many or too little {{{}}. The console displays all results at once. Thank you. – user3190322 Jan 16 '14 at 10:56
4

The return statement is exiting the function here.

So the declarations of x,y,z and result, and the Console.Write will never run...

cubitouch
  • 1,929
  • 15
  • 28
  • Ok, and how do I fix it and make it work ? If I remove the return, I don"t have the method. Knowing the cause of the warning doesn't give me the solution, since my knowledge is very limited at this point. – user3190322 Jan 13 '14 at 14:16
  • Everything behind the return should be in the Main (or at least in the main function of your program) IMHO, if I understand well your exercise. – cubitouch Jan 13 '14 at 14:17
  • I put everything in the Main, it all got red. I think it would take 6 months to get to the bottom of this. I'm moving forward. I'll adopt a methodless programming style. – user3190322 Jan 16 '14 at 10:10
  • You were right. I had some trouble with the curly parentheses (I'm lost at this chapter). I started a new project and it worked. Thanks. – user3190322 Jan 16 '14 at 11:02
0

Any statements after return statement is unreachable. It will never gets executed and the compiler informs you.

It is a compile-time warning if a statement cannot be executed because it is unreachable

Unreachable Statements MSDN

Dineshkumar
  • 4,165
  • 5
  • 29
  • 43
0

This is because the there is no way that the code will runs each line of your function, as you exit the function before some code you have in your function:

   public class Multiply 
  {
//Multiplies argument by 4 

           public static int MultiplyBy4(int aNumber)
            {
                return 4 * aNumber; // <---- You exit the function MultiplyBy4 here

               // Everything beneath this line will never get executed
               // --------------------------------------

//Shows ways of passing various arguments to a method public static void Main
                int x = 7;
                int y = 20;
                int z = -3;
                int result = 0;

                result = MultiplyBy4(x);
                Console.WriteLine("Passsing a variable, x : {0}", result);

                result = MultiplyBy4(y + 2);
                Console.WriteLine("Passing an expression, Y + 2: {0}", result);

                result = 5 + MultiplyBy4(z);
                Console.WriteLine("Using MultiplyBy4 in an expression: {0}", result);
                Console.ReadLine();
            }
 }

This is what the compiler detects and warns you from it, as in most cases this is a coding mistake.

Also your code would cause a StackOverflow (how ironic as we are on SO :P) if you would move the return statement and exit the function at the end of the function. You are causing a never ending recursion by calling MultiplyBy4(z);

This would cause the processor to go at 100% and fill the part of the memory that holds functions calls completely.

RononDex
  • 4,143
  • 22
  • 39
0

As cubitouch said the return statement stops execution in the function.

You can have multiple return statements if you have them in if-clauses. But at least one return statement has to be in the function that can be reached from everywhere (e. g. none of the conditions are met)

Generally return statements are the last instructions in functions.

Stefan Schmid
  • 1,012
  • 10
  • 28