Return two numbers in function

hey guys;
Im new one there and i wanna know how can i return 2 numbers ( UTMEasting And UTMNorthing ) from function
my code calculate UTM cord
#include <SPI.h>
#include <SD.h>
#include <math.h>

File myFile;

int counter =0 , e = 0 , i=0 , j=0 , k=0 , q , g , f ;
char A[100] ;
bool LatON = true;
String Lat[20] , Lon[20] , Latnew , Lonnew ;
float LatN[20], LonN[20] , Latt , Lonn , LastLat , LastLon ;
const double deg2rad = PI / 180;
const double rad2deg = 180.0 / PI;
const double equrad = 6377563;
const double squecc = 0.00667054;
double o;

void setup()
{
Serial.begin(115200);
while (!Serial) {
}

if (!SD.begin(4)) 

{
while (1);
}
myFile = SD.open("test.txt");

if (myFile)
{
while (myFile.available())
{
char check_char = myFile.read();
if(check_char == ',' or check_char == '\n' or check_char == ' ')
{
e = counter - 1;
if (LatON)
{
for (i=0 ; i <= e ; i++)
{
Lat[j] += A[i];
LatON=false;
}
j++;
}
else
{
counter = counter -1;
for (i=0 ; i <= e ; i++)
{
Lon[k] += A[i];
LatON=true;
}
k++;
}
counter = 0;
for (i=e ; i >= 0 ; i--)
{
A[i] = NULL;
}
}
else
{
A[counter]= check_char;
counter++;
}
}
}
for (q=0 ; q < j ; q++)
{
LatN[q] = atof(Lat[q].c_str());
LonN[q] = atof(Lon[q].c_str());
}

  myFile = SD.open("gps.txt");

if (myFile)
{
while (myFile.available())
{
char gps_sig = myFile.read();

  if (gps_sig == ',')
  {
    counter++;
  }
  else if (counter == 2)
  {
      Latnew += gps_sig;

  }
  else if (counter == 4)
  {
      Lonnew += gps_sig;
  }
  else if (gps_sig == '$')
  {
    if(counter != 0)
    {
       Latt = atof(Latnew.c_str());
       Lonn = atof(Lonnew.c_str());

       g = ((int)Latt/100)*100;
       f = ((int)Lonn/100)*100;

       LastLat = ((Latt - g)/60)+((int)Latt/100);
       LastLon = ((Lonn - f)/60)+((int)Lonn/100);
    }

    Latnew = "";
    Lonnew = "";
    counter = 0;

  }

}

}

}

void loop() {

while(1)
{
Serial.println(LLtoUTM(LastLat, LastLon));
}

}

double LLtoUTM( const double InLat, const double InLong)
{
double a = 6378137;
double eccSquared = 0.00669438;
double k0 = 0.9996;
double LongOrigin;
double eccPrimeSquared;
double N, T, C, A, M;
double UTMEasting;
double UTMNorthing;
double LongTemp = (InLong+180)-int((InLong+180)/360)360-180;
double LatRad = InLat
deg2rad;
double LongRad = LongTemp*deg2rad;
double LongOriginRad;
int ZoneNumber;

ZoneNumber = int((LongTemp + 180)/6) + 1;
if( Latt >= 56.0 && Latt < 64.0 && LongTemp >= 3.0 && LongTemp < 12.0 )
ZoneNumber = 32;

// Special zones for Svalbard
if( Latt >= 72.0 && Latt < 84.0 )
{
if( LongTemp >= 0.0 && LongTemp < 9.0 ) ZoneNumber = 31;
else if( LongTemp >= 9.0 && LongTemp < 21.0 ) ZoneNumber = 33;
else if( LongTemp >= 21.0 && LongTemp < 33.0 ) ZoneNumber = 35;
else if( LongTemp >= 33.0 && LongTemp < 42.0 ) ZoneNumber = 37;
}

LongOrigin = (ZoneNumber - 1)6 - 180 + 3;
LongOriginRad = LongOrigin * deg2rad;
eccPrimeSquared = (eccSquared)/(1-eccSquared);
N = a/sqrt(1-eccSquared
sin(LatRad)sin(LatRad));
T = tan(LatRad)tan(LatRad);
C = eccPrimeSquared
cos(LatRad)cos(LatRad);
A = cos(LatRad)
(LongRad-LongOriginRad);
M = a
((1 - eccSquared/4 - 3eccSquaredeccSquared/64 - 5eccSquaredeccSquaredeccSquared/256)LatRad
- (3
eccSquared/8 + 3
eccSquaredeccSquared/32 + 45eccSquaredeccSquaredeccSquared/1024)sin(2LatRad)
+ (15eccSquaredeccSquared/256 + 45eccSquaredeccSquaredeccSquared/1024)sin(4LatRad)
- (35
eccSquaredeccSquaredeccSquared/3072)sin(6LatRad));

UTMEasting = (double)(k0N(A+(1-T+C)AAA/6
+ (5-18
T+TT+72C-58eccPrimeSquared)AAAAA/120)
+ 500000.0);

UTMNorthing = (double)(k0*(M+Ntan(LatRad)(AA/2+(5-T+9C+4CC)AAAA/24
+ (61-58T+TT+600C-330eccPrimeSquared)AAAAAA/720)));

if(Latt < 0)
UTMNorthing += 10000000.0; 

// //Preparation of MGRS 100km chars
// String UTMTwo = MGRSZoneDesignator(UTMEasting, UTMNorthing);
//
// //Preparation to output
// String toUTM = String(ZoneNumber);
//// toUTM += " ";
//// toUTM += UTMTwo;
// toUTM += UTMEasting;
// toUTM += UTMNorthing;
//// return toUTM;
//Serial.println(toUTM);
}

String MGRSZoneDesignator(double UTMEasting, double UTMNorthing)
{
String e100kLetters[] = {"S","T","U","V","W","X","Y","Z"};
String n100kLetters[] = {"A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V"};
const int col = floor(UTMEasting /100000); //Cutting first number. Upside is more info
const int row = int(floor(UTMNorthing /100000))%20; //Cutting first two numbers. Upside is more info
String ZoneDesignator = e100kLetters[col-1];
ZoneDesignator += n100kLetters[row];
// return ZoneDesignator;
}

char UTMLetterDesignator(double Lat)
{
//Written by Chuck Gantz- chuck.gantz@globalstar.com
char LetterDesignator;
if((84 >= Lat) && (Lat >= 72)) LetterDesignator = 'X';
else if((72 > Lat) && (Lat >= 64)) LetterDesignator = 'W';
else if((64 > Lat) && (Lat >= 56)) LetterDesignator = 'V';
else if((56 > Lat) && (Lat >= 48)) LetterDesignator = 'U';
else if((48 > Lat) && (Lat >= 40)) LetterDesignator = 'T';
else if((40 > Lat) && (Lat >= 32)) LetterDesignator = 'S';
else if((32 > Lat) && (Lat >= 24)) LetterDesignator = 'R';
else if((24 > Lat) && (Lat >= 16)) LetterDesignator = 'Q';
else if((16 > Lat) && (Lat >= 8)) LetterDesignator = 'P';
else if(( 8 > Lat) && (Lat >= 0)) LetterDesignator = 'N';
else if(( 0 > Lat) && (Lat >= -8)) LetterDesignator = 'M';
else if((-8> Lat) && (Lat >= -16)) LetterDesignator = 'L';
else if((-16 > Lat) && (Lat >= -24)) LetterDesignator = 'K';
else if((-24 > Lat) && (Lat >= -32)) LetterDesignator = 'J';
else if((-32 > Lat) && (Lat >= -40)) LetterDesignator = 'H';
else if((-40 > Lat) && (Lat >= -48)) LetterDesignator = 'G';
else if((-48 > Lat) && (Lat >= -56)) LetterDesignator = 'F';
else if((-56 > Lat) && (Lat >= -64)) LetterDesignator = 'E';
else if((-64 > Lat) && (Lat >= -72)) LetterDesignator = 'D';
else if((-72 > Lat) && (Lat >= -80)) LetterDesignator = 'C';
else LetterDesignator = 'Z'; //This is here as an error flag to show that the Latitude is outside the UTM limits
// return LetterDesignator;
}

TY

Please edit your post and put your sketch between lines with three backslash-single-quotes or use the </> button.

```
Your sketch
```

There are a few ways, but the most common way is to use pointers or "by reference". The "by reference" is the nicest one.

void loop()
{
  double number1;
  double number2;
  myFunction( 10.0, 20.0, &number1, &number2);   // pointers

  myFunctionTwo( 123.4, 567.8, number1, number2);   // "by reference"
}

int myFunction( double x, double y, double * p, double * q)
{
  *p = x + y;
  *q = x - y;
  return 0;
}

int myFunctionTwo( double x, double y, double & p, double & q)
{
  p = x * y;
  q = x / y;
  return 0;
}
3 Likes

thanks but i wanna dont use pointers

And I don't wanna give cheesy examples that return a package of data :clown_face:

Then use References.

1 Like

Sounds like UTMEasting and UTMNorthing belong in a struct together.

1 Like

It’s heresy, but global variables can be used in this case.

Usually ppl want to minimise the use of global variables. With small and smaller programs, it becomes less important.

Make two global variables and stuff them whenever you need to, however you want, and then use them as needed.

Just be careful.

a7

2 Likes

mybe a pointer

That's one of the best and simplest examples I've seen of passing by reference or using a pointer, thank you.

I've always found this confusing in C/C++, don't know why because the basic idea is simple.

very often the return value is only used to return status and one of more values are returned thru pointers (or using &symbol)

#include <stdio.h>

int
func (
    int val, int *cube, int *sqr)
{
    if (0 > val)
        return -1;

    *sqr  = val * val;
    *cube = val * *sqr;
    return 0;
}


int
main ()
{
    int  cube;
    int  sqr;
    int   val  = 2;

    if (! func(val, &cube, &sqr))  {
        printf (" %s: val %d, cube %d, sqr %d\n", __func__, val, cube, sqr);
    }

    return 0;
}

It most certainly is not heresy.
With the exception of variables inside of libraries I only use global variables (with any MCU) as it forces me to allocate and account for all memory before the program runs. I've never had the stack and heap overlap.
No returns, very few parameters, simple code.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.