-5

I'm new to c#, but I'm trying to create a basic program that can use a function that adds to numbers (this is just practice I know it's inefficient.

        {
            int AddNumbers(int num1, int num2) //Here's where the error comes
            {
                int result = num1 + num2;
                return result;
            }

            int num1 = 10;

            int num2 = 20;

            AddNumbers(num1, num2);

However, when I try it, it says that "A local parameter named 'num1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter". I assume that this is because I declared the variables while calling the function, but I don't know how to fix it.

Thanks for any help!

EDIT: Just to be clear, the numbers after the functions are the number I would like to be added in the function

derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • 1
    Use different names. Either for the local function parameters or for your local variables – haim770 Feb 23 '20 at 15:57
  • 1
    You have a mismatched number of curly braces (missing a `{`) which would clarify the picture – Ňɏssa Pøngjǣrdenlarp Feb 23 '20 at 16:03
  • @haim770 Again, what would I need to change and to what? – Thomas Wade Feb 23 '20 at 16:06
  • This is somewhat besides the point, but I would wager a guess that the cleanest thing you could do here is to declare your `AddNumbers` function outside whatever that scope is you provide here (probably `Main` function). Of course, the suggestions by others would also work. – Marchyello Feb 23 '20 at 16:07
  • @ThomasWade did below answer helped you? – Manjuboyz Feb 23 '20 at 16:16
  • 1
    It´s a bit hard to give you a good answer, because your formatting is bad. Is `AddNumbers` a [local function](https://learn.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/local-functions) that is contained in another function? – MakePeaceGreatAgain Feb 23 '20 at 16:21
  • 1
    Since he said `I'm new to c#` and `create a basic program` I assume he is having issues with scope which is why it ended up being a local unintended function. – Prix Feb 23 '20 at 16:40

3 Answers3

0

Local methods have access to every thing in the parent block. That is why you cannot define the same variable name more than once. Try this instead.

{
    int num1;
    int num2;

    int AddNumbers(int left, int right) => left + right;

    AddNumbers(num1, num2);
}

There is a guide for this as well found here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions

Darkmere
  • 60
  • 4
0

I guess you were trying to make a method for add Numbers. If you want to make a method, you need make method OUTSIDE of the Main code.

public static int AddNumbers(int num1, int num2)
{
    int sum;
    sum = num1 + num2;
    return sum;
}
static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    Console.WriteLine($"a = {a}, b = {b}");
//First way to control the string: put $ infront of the string, and write code in the {})
    Console.WriteLine("a + b = {0}", AddNumbers(a, b));
//Second way to control the string: put {} and number (start with 0) and write code after comma
}

If you write as this, you will get an answer as

a = 10, b = 20
a + b = 30

For public static int AddNumbers(int num1, int num2) You don't have to think about public static yet. At the position of int, you can put another variables such as string, long, double. For that case, you have to return (various you put); at the end. If you put void, you don't have to put return and the thing you wanted will be done in the method.

Jaeho Song
  • 63
  • 1
  • 10
-3

Welcome to SO.

as you know you cannot have same variable name used in a method here is what you need

     {
        int AddNumbers(int num1, int num2) //Here's where the error comes
        {
            int result = num1 + num2;
            return result;
        }

        int num2 = 10;

        int num3 = 20;

        AddNumbers(num2, num3);

    }

you can have something like this:

class Program
{
    int p = 0;
  public  static void Main(string[] args)
    {
        int num1 = 10;
        int num2 = 20;           
     int num =  Method1(num1,num2);
     Console.WriteLine(num);
    }

    public static int Method1(int num1, int num2)
    {
        p = num1 + num2;
        return p;
    }      
}
Manjuboyz
  • 6,978
  • 3
  • 21
  • 43
  • Would like to know why the answer was down voted! – Manjuboyz Feb 23 '20 at 16:13
  • 1
    Why are you creating an instance of Program only to call the Method1? Why not make Method1 static so you can freely call it? using a field + creating an instance of Program is certainly not the way – Prix Feb 23 '20 at 16:22
  • @Prix I was not trying to optimize the code here, I was just creating a snippet on fly, if that is why it was down voted then I have modified the snippet. thanks. – Manjuboyz Feb 23 '20 at 16:25
  • 1
    There is nothing about optimizing code in my comment, you're straight up giving the OP a bad example by creating a local field to hold a local sum that is calculated and returned within the method, on top of that you're creating a new instance of the class just for the purpose of calling it. – Prix Feb 23 '20 at 16:27
  • 2
    @Manjuboyz `Program obj = new Program();` I still see this in your code, you haven't actually fixed things. And prix is right, this isn't just about optimisation. – Andrew Feb 23 '20 at 17:26
  • 1
    A static method is called by using the class name directly, in this case that would be `Program.Method1()` (which is a horrible name for a method - they should be meaningful for what they do). Since you are calling it from within the class itself, you can skip the class name and just call `Method1()' You never need to instantiate the Program class – Andrew Feb 23 '20 at 18:22
  • My bad, I missed that! have modified now. – Manjuboyz Feb 24 '20 at 03:18
  • @Manjuboyz you should still rename `Method1` to `AddNumbers` or something meaningful - it's bad practice to have method names that don't mean anything about what they do – Andrew Feb 29 '20 at 17:10