CS7036 error on C# calling a function

Asked

Viewed 549 times

3

I just got into the matter of functions and methods and I’ve come across a problem that I can’t solve at all because I don’t understand why.

The point is:

"Create a method that takes an integer number and tells if it is positive (true) or negative (false) through a return with Boolean."

My code went like this:

class Program
    {
        static bool PositivoOuNegativo (int Numero, bool Resultado)
        {
            if (Numero >= 0)
            {
                Resultado = true;
            }
            else
            {
                Resultado = false;
            }
            return Resultado;
        }
        static void Main(string[] args)
        {
            int numeroTeste;
            bool ResultadoTeste;
            Console.Write("Digite um número: ");
            numeroTeste = Convert.ToInt32(Console.ReadLine());
            ResultadoTeste = PositivoOuNegativo(numeroTeste);

            Console.ReadKey();

        }
    } 

But the error is appearing:

CS7036 There is no argument provided that matches the parameter formal required "Result" of "Program.Positivoounegative(int, bool)".

Where I’m going wrong and how to proceed?

  • you set the function to receive 2 arguments, int Numero, bool Resultado but must have only one, the result is returned by the function at the end of it

1 answer

2


If you pay close attention to the error message already has clear what the problem is, just missing the solution. And someone naive could say that he has to call the method by passing two arguments and not just one as he is doing. But this two-parameter method makes no sense and is a method that could be much simpler. Taking advantage and fixing other problems would be this:

using static System.Console;

class Program {
    static bool ÉPositivo(int numero) => numero >= 0;
    static void Main() {
        Write("Digite um número: ");
        if (!int.TryParse(ReadLine(), out var numero)) return;
        WriteLine(ÉPositivo(numero));
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Note that the method already returns if it is positive then it has no reason to have this second parameter, the result is passed by the return. And I changed the name of the method because it doesn’t say whether it’s positive or negative, it says whether it’s positive or not. And as a boolean expression already gives the expected result has no reason to use if, this is excess code making unnecessary obviousness, the result of the expression is already the return. I used the simplified syntax that does not require using the return.

I also simplified the data request and made the conversion the right way not to break the application if the person enters something invalid. I closed, but I could put an error message there.

Dropped from 23 to 6 lines, very easy to follow, no?

It would be nice to see this: Why does the if only work with the return of a method?.

  • I get it, I can not declare two methods and then when calling the function only use one, right? I tidied and simplified here. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.