user319854
user319854

Reputation: 4106

C# work with functions

I'am new with C#. How works in C# functions?

My try:

        private void Form1_Load(object sender, EventArgs e)
    {
        MessageBox.Show(Convert.ToString(number_p(5)));            
    }

    public void number_p(int number)
    {
        int one = 1;
        number = number + one;
        return number;
    }

Error: return, why? Thanks

Upvotes: 0

Views: 196

Answers (3)

Mohnkuchenzentrale
Mohnkuchenzentrale

Reputation: 5885

You Method is of type "void" so there is no return value

If you want to return a number of type int you have to declare your method to be of type int instead of void

Maybe you should grab a book and read the very raw principles of c# first before posting here

Upvotes: 4

Josh
Josh

Reputation: 69262

Your function (typically referred to as a 'method' in C#) is defined as returning void. Change it to:

public int number_p(int number)

Upvotes: 1

jwismar
jwismar

Reputation: 12258

At first glance, it looks like the problem may be that your function is declared to return void (i.e. nothing). Try changing it to return int.

public int number_p(int number)
{
    int one = 1;
    number = number + one;
    return number;
}

Upvotes: 5

Related Questions