Siva Teja
Siva Teja

Reputation: 3

METHOD concept program in c# is not working

using System;
class Methodcalling
{

    public int Values(int num1, int num2)
    {

        if (num1 > num2)
        {
            Console.WriteLine("num2 is large ");
        }
        else
        {
            Console.WriteLine("num1 is big");
        }
    }
    static void Main(string[] args)
    {
        int a, b;
        Methodcalling m = new Methodcalling();


        Console.WriteLine("enter a no.:");
        a = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("enter a no.:");
        b = Convert.ToInt32(Console.ReadLine());

        int result = m.Values(a, b);

    }

}

ERROR:'Methodcalling.Values(int, int)': not all code paths return a value

In this way its showing error in VISUAL STUDIO 2013.

Upvotes: 1

Views: 56

Answers (2)

SIkandar
SIkandar

Reputation: 31

You have to return an int value from the method as the method's signature defines a return type of int, and you are also calling

int result = m.Values(a, b);

which suggest that the method would return an integer but you have no return statement in your method.

You can modify the method like this if you don't want any integer to be returned.

using System;

class Methodcalling {

    public void Values(int num1, int num2) {

        if (num1 > num2)
        {
            Console.WriteLine("num2 is large ");
        }
        else
        {
            Console.WriteLine("num1 is big");
        }
    } 

    static void Main(string[] args) {
            int a, b;
            Methodcalling m = new Methodcalling();

            Console.WriteLine("enter a no.:");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter a no.:");
            b = Convert.ToInt32(Console.ReadLine());

            m.Values(a, b);

    }

}

Upvotes: 1

Prashanth Benny
Prashanth Benny

Reputation: 1609

This error is because you have declared the function to return an int, but your function does not return anything.

Have a look at this link here for a reference on the function declaration in c#.

For a relief from the error, try adding return 0; to the end of the function.

public int Values(int num1, int num2)
{

    if (num1 > num2)
    {
        Console.WriteLine("num2 is large ");
    }
    else
    {
        Console.WriteLine("num1 is big");
    }

    return 0; // Return zero to the calling function.
}

Now you could call this function like this to capture the return value.

int TheReturnValue = Values(num1 , num2); //You will get 0 as value for TheReturnValue variable.

or change the int to void as in public void Values(int num1, int num2) if you don't want to return any values.

here is the usage:

public void Values(int num1, int num2)
{

    if (num1 > num2)
    {
        Console.WriteLine("num2 is large ");
    }
    else
    {
        Console.WriteLine("num1 is big");
    }
}

and you could call the function(void Return Type) like this:

 m.Values(a, b); //without assigning it to a variable

Since you are only writing to console using this function, void datatype would be the better option.

I hope this helps.

Upvotes: 4

Related Questions