tmw
tmw

Reputation: 35

Console Application Code Error

In my code,

            int x;
            int y;
            x = 7;



            if (x == y)
            {
                Console.WriteLine("The numbers are the same!");

            }
            else
            {
                Console.WriteLine("The numbers are different.");
            }
            Console.ReadLine();


            for (int i = 0; i < y; i--)
            {
                Console.WriteLine("{0} sheep!", i);
            }
            Console.ReadLine();


            string[] colors = new string[y];
            colors[0] = "green";
            colors[1] = "yellow";
            colors[y] = "red";


            Console.WriteLine("Your new code is {0}.", Code(x, y));
            Console.ReadLine();

        }   

            static int Code(int myX, int myY)
            {
                int answer = myX * myX - myY;
            }
    }
}

there is an error that states:

'ConsoleApplication1.Program.Code(int, int)': not all code paths return a value'.

I am not sure what is wrong with the code. Solutions?

Upvotes: 3

Views: 157

Answers (3)

tnw
tnw

Reputation: 13877

Pretty straight forward. Your function:

static int Code(int myX, int myY)
{
    int answer = myX * myX - myY;
}

Requires that you return an integer. I think you meant to do this:

static int Code(int myX, int myY)
{
    return myX * myX - myY;
}

Upvotes: 10

Joe
Joe

Reputation: 538

You need to return the value of 'answer', as otherwise, as the error stated, the code does not return a value.

Note: Whenever you use an 'int' or 'string' in the way that you were using it, you must always return a value

static int Code(int myX, int myY)
    {
        int answer = myX * myX - myY;
        return answer;
    }

Upvotes: 3

Inisheer
Inisheer

Reputation: 20794

static int Code(int myX, int myY)
{
     int answer = myX * myX - myY;
}

Your function doesn't return a result (just as the error states). It should be:

static int Code(int myX, int myY)
{
    int answer = myX * myX - myY;
    return answer;
}

Upvotes: 2

Related Questions