Need C# help: Trying to figure out how to pull values from a method call

Hi everyone! I’m working through an assignment for C#. I’m having trouble figuring out how to pull values from the arguments in a method call. Am I missing something obvious here?

Here is my code:

using System;

class MainClass {
static void Main(string[] args)
/Write a method called CaculateDifference(int firstNumber, int secondNumber).
This method will calculate and return the difference between the two numbers
* i.e. it will subtract the smaller one from the larger one and then return that value.
* Call the CaculateDifference() method four times from your program Main() method
* supplying different arguments each time. You must display the returned value for each call*/
{
int difference, firstNumber, secondNumber;
difference = CalculateDifference(2,75);
Console.WriteLine($“The difference between {firstNumber} and {secondNumber} is {difference}.”);
CalculateDifference(33,31);
Console.WriteLine($“The difference between {firstNumber} and {secondNumber} is {difference}.”);
CalculateDifference(100,100);
Console.WriteLine($“The difference between {firstNumber} and {secondNumber} is {difference}.”);
CalculateDifference(99,1);
Console.WriteLine($“The difference between {firstNumber} and {secondNumber} is {difference}.”);
}
static int CalculateDifference(int firstNumber, int secondNumber)
{
int difference;
if (firstNumber <= secondNumber)
{
difference = secondNumber - firstNumber;
} else
{
difference = firstNumber - secondNumber;
}
return difference;
}
}

What should be printed after the call is:

The difference between 3 and 5 is 2

The difference between 13 and 5 is 8

etc…

I would appreciate all help and advice!

I see no flaws in your difference() function. 

The problems are in your main program. Your WriteLine statements are printing the values of firstNumber, and secondNumber, but at no point do you _assign values_ to those variables!  Nor do you update the value of the difference variable after the first call.

So I’m guessing the output is currently (paraphrased):
diff of 0 and 0 is 73

diff of 0 and 0 is 73

diff of 0 and 0 is 73

diff of 0 and 0 is 73

Remember, a variable in a function (the parameters firstNumber, secondNumber) is not the same as a variable in your main program (firstNumber and secondNumber as defined near line 11). The same for ‘difference’. You’ll want to understand how ‘scope’ works.

1 Like

Arguments that you pass to the method are destroyed after its execution. It would be easier to output the message directly in the method.

static void CalculateDifference(int firstNumber, int secondNumber)
        {
            int difference;
            if (firstNumber <= secondNumber)
                difference = secondNumber - firstNumber;
            else
                difference = firstNumber - secondNumber;

            Console.WriteLine($"The difference between {firstNumber} and {secondNumber} is {difference}.");
        }

Assign the value in variable and on each call assigned value in variable difference = CalculateDifference(2,75,) This will always print the correct value.