Jesse
Jesse

Reputation: 1

How to send variables from Main to another method, then return a value back to Main method?

I want to send the variables temperature and windSpeed to the ComputeWindChill method to find the windchill. Then return windchill back to the Main method to display the temperature, wind speed, and the temperature that includes the wind chill.

public class Program
{

    public static void Main(string[] args)
    {

        Random rnd = new Random();

        int temperature = rnd.Next(0,50);
        int windSpeed = rnd.Next(4,30);

        Console.Write("Temperature: {0}", temperature);
        Console.WriteLine();
        Console.Write("Wind Speed: {0}", windSpeed);
        Console.WriteLine();
        Console.Write("Temperature (including windchill): {0}", ComputeWindChill.windChill);

        ComputeWindChill(temperature);
        ComputeWindChill(windSpeed);
        } // end Main

        public double ComputeWindChill(int temperature, int windSpeed, double windChill)
        {
              windChill = 35.74 + 0.6215 * temperature - 35.75 * Math.Pow(windSpeed,0.16) +
                     0.4275 * temperature * Math.Pow(windSpeed,0.16);

        return windChill; 
    }
} // end class

Upvotes: 0

Views: 492

Answers (5)

user7648033
user7648033

Reputation:

You can use "out" for getting back windChill value after calling method

public class Program
{
public static void Main(string[] args)
{
Random rnd = new Random();
int temperature = rnd.Next(0,50);
int windSpeed = rnd.Next(4,30);

Console.Write("Temperature: {0}", temperature);
Console.WriteLine();
Console.Write("Wind Speed: {0}", windSpeed);
Console.WriteLine();

// Not sure what you are trying to do here?
Console.Write("Temperature (including windchill): {0}",ComputeWindChill.windChill);

double windChill=0;

ComputeWindChill(temperature,windSpeed,windChill);

ComputeWindChill(windSpeed);
} // end Main

public static double ComputeWindChill(int temperature,int windSpeed,out double windChill)
{
      windChill = 35.74 + 0.6215 * temperature - 35.75 * Math.Pow(windSpeed,0.16) +
             0.4275 * temperature * Math.Pow(windSpeed,0.16);

return windChill; 
}
}

Upvotes: 1

Chandini
Chandini

Reputation: 550

You need to first understand how to define a function. For example take your function. In your function the temperature and windspeed are the arguments you wish to send to the ComputeWindChill function. And the you need a double value windspeed to be returned from the function.

So you define your function as

public static double ComputeWindChill(int temperature, int windSpeed)
{
    return 35.74 + 0.6215 * temperature - 35.75 * Math.Pow(windSpeed,0.16) +
     0.4275 * temperature * Math.Pow(windSpeed,0.16);
}

and while calling the above function, you should pass the arguments to the function, all at the same time and store the expected return value from the function.

double windChill=ComputeWindChill(temperature,windSpeed);

Upvotes: 0

Harry
Harry

Reputation: 4050

 public static void Main(string[] args)
 {

 Random rnd = new Random();

 int temperature = rnd.Next(0,50);
 int windSpeed = rnd.Next(4,30);

 Console.Write("Temperature: {0}", temperature);
 Console.WriteLine();
 Console.Write("Wind Speed: {0}", windSpeed);
 Console.WriteLine();

 //here you make a new double called windchill which will be equal to what your calculate windchill method returns 
 double windChill = ComputeWindChill(temperature,windSpeed)

 Console.Write("Temperature (including windchill): {0}", windChill);

 } // end Main

 //I removed your third parameter called windchill here because you do not know it yet, you are calculating it and make this a static method(read up on static methods - very important)
 public static double ComputeWindChill(int temperature, int windSpeed)
 {
      double windChill = 35.74 + 0.6215 * temperature - 35.75 * Math.Pow(windSpeed,0.16) +
         0.4275 * temperature *       Math.Pow(windSpeed,0.16);

 return windChill; 
 }

p.s. I'm typing on a phone

Upvotes: 0

Pikoh
Pikoh

Reputation: 7713

You must change your method to this:

public static double ComputeWindChill(int temperature, int windSpeed)
{
    return 35.74 + 0.6215 * temperature - 35.75 * Math.Pow(windSpeed,0.16) +
         0.4275 * temperature * Math.Pow(windSpeed,0.16);
}

This would accept 2 parameters (temperature and windspeed) and would return a double with all the calculations. You should also notice that the method must be declared as static because you are calling it from another static method (main)

The call should be like this:

double windChill=ComputeWindChill(temperature,windSpeed);
Console.Write("Temperature (including windchill): {0}", windChill);

Another alternative is using out parameters. In this case, instead of the method "returning" a value,what it does is setting the value in the variable passed as out parameter:

public void ComputeWindChill(int temperature, int windSpeed, out double windChill)
{
    windChill= 35.74 + 0.6215 * temperature - 35.75 * Math.Pow(windSpeed, 0.16) +
             0.4275 * temperature * Math.Pow(windSpeed, 0.16);
}

Notice the void return paramenter, and the out keyword in the third parameter.

The call to this method is a bit different:

double windChill;
ComputeWindChill(temperature, windSpeed,out windChill);
Console.Write("Temperature (including windchill): {0}", windChill);

Upvotes: 0

Christos
Christos

Reputation: 53958

You should mark ComputeWindChill as static. Otherwise you can't use it. Furthermore, you have to store somewhere the result of the method you call and then use it. As your code is, even it would be compliled withour errors, you call the method and you don't use it's result. Last but not least the method expects 3 parameters and you pass only 1.

Upvotes: 0

Related Questions