简体   繁体   中英

Function with parameters in c#?

I am trying to create a function called "compNumbers" that takes 2 double numbers. If the two numbers are equal, they should return 0, if the first parameter is greater than the second, it should return 1, if second parameter is greater than the first, it should return -1

Here's my attempt on the question:

static double compNumbers(double x, double y)
{
    if (x == y)
    {
        return 0;
    }
    else if (x > y)
    {
        return 1;
    }
    else if (y > x)
    {
        return -1;
    }
}
static void Main(string[] args)
{
    double a = 0, b = 0;
    compNumbers(a,b);
    Console.ReadKey();
}

The error I'm experiencing is:

The description says "Program.compNumbers(double,double): not all code paths return a value".

Also, since I'm new to programming, where would I put the 'input, processing, output' code? I'm confused on where to start...

Thanks for helping!

For example, if x or y has value double.NaN , all your conditions will return false , so you will reach the end of function and you need to return somethig:

static double compNumbers(double x, double y)
{
    if (x == y)
    {
        return 0;
    }
    else if (x > y)
    {
        return 1;
    }
    else if (y > x)
    {
        return -1;
    }
    return double.NaN
}

More about double.NaN value: https://msdn.microsoft.com/en-us/library/system.double.nan(v=vs.110).aspx

To compare double values correct use special method CompareTo :

x.CompareTo(y);
static double compNumbers(double x, double y)
{
    return x.CompareTo(y);
}

The issue you're experiencing is that there's no return if all of the else if's are false.

Since the only other option if "x doesn't equal y" and "x isn't larger than y" is "y must be larger than x", I'd remove the last else if and save a few lines of code by just returning -1.

static double compNumbers(double x, double y)
{
    if (x == y)
    {
        return 0;
    }
    else if (x > y)
    {
        return 1;
    }

    return -1;
}

First, in this case, you don't have to declare a method as double because you only want an int in return!!!

Second, you have to return something when all the conditions are false!!

static int? CompNumbers(double x, double y) //int? is nothing more than nullable int
    {
        if (x == y)
        {
            return 0;
        }
        else if (x > y)
        {
            return 1;
        }
        else if (y > x)
        {
            return -1;
        }
        return null; //this is the return value you have give !!
    }
    static void Main(string[] args)
    {
        double a = Convert.ToDouble(Console.ReadLine());//taking input from user 
        double b = Convert.ToDouble(Console.ReadLine());//taking input from user
        int? result = CompNumbers(a, b);//assigning result to the return value of CompNumbers   and  int? is nothing more than nullable int
        if (result == 0)
        {
            Console.WriteLine("The Method returned 0");
        }
        else if (result == 1)
        {
            Console.WriteLine("The Method returned 1");
        }
        else if(result == -1)
        {
            Console.WriteLine("The Method returned -1");
        }
        Console.ReadKey();
    }

this might be helpful !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM