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
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;
}
}