R
R
Roflmax2022-02-13 18:12:41
C++ / C#
Roflmax, 2022-02-13 18:12:41

How to return two variables with return?

This is part of the learning task. It uses my returned value to test the length from segment to point in various cases. I can safely return one of them, but I do not know how to do it together.

public static double GetDistanceToSegment(double ax, double ay, double bx, double by, double x, double y)
    {
      double result = 0;
      double sabx = (ax + bx) / 2;
      double saby = (ay + by) / 2;

      double tandotrez1x = x - ax;
      double tandotrez1y = y - ay;
      double tandotrez2x = x - bx;
      double tandotrez2y = y - by;
      double otrezx = bx - ax;
      double otrezy = by - ay;
      double primouotrezx = sabx - x;
      double primouotrezy = saby - y;

      
      if ((otrezx*primouotrezx+otrezy*primouotrezy)==0)
            {
         result = Math.Pow(Math.Pow(primouotrezx, 2) + (Math.Pow(primouotrezy, 2)), 0.5);
            }
      return result;		
  
      double dlina1 = Math.Pow(Math.Pow(otrezx, 2) + Math.Pow(otrezy, 2), 0.5);
      double dlina2 = Math.Pow(Math.Pow(tandotrez1x, 2) + Math.Pow(tandotrez1y, 2), 0.5);
      double dlina3 = Math.Pow(Math.Pow(tandotrez2x, 2) + Math.Pow(tandotrez2y, 2), 0.5);
      double poluperimetr = (dlina1 + dlina2 + dlina3) / 2;
      double square = Math.Pow(poluperimetr*(poluperimetr-dlina1) * (poluperimetr - dlina2) * (poluperimetr - dlina3), 0.5); //S=1/2*h*a h=2S/a
      double h = 2 * square / dlina1;
      return h;
      


    }

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Morrowind, 2022-02-13
@Roflmax

Is very necessary that that use "tuple".
Not the best practice, as they say, but if you need it this way and not otherwise, then why not.

public (int, string) GetUser(){
int a = 14;
string b = "Roman";
return (a,b)
}

R
rPman, 2022-02-13
@rPman

complement responders
, you can add arguments to the function, defining them as reference (ref or out keyword), respectively, one or all of the return values ​​can be placed there

V
Vasily Bannikov, 2022-02-13
@vabka

There are several options:
1. Return an array
2. Declare your structure or class, which will contain the two required values ​​as fields or properties
3. Return a tuple

V
Vladimir Korotenko, 2022-02-13
@firedragon

return returns something that contains 2 values ​​is either a class or a struct or a perverted delimited string (don't do that)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question