Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

return two variables from a function

Status
Not open for further replies.

kappa_am

Full Member level 6
Joined
Jul 16, 2012
Messages
331
Helped
19
Reputation
38
Reaction score
19
Trophy points
1,298
Location
Vancouver
Activity points
3,859
hi all;
I am working with Mikroc, and I like to return two values from a function. I would be grateful if you guide me how to do this work.

Thank you.
 

First way - use a pointer as a parameters:
Code:
void main (void)
{
  char x,y;
  GetXY (&x, &y);
}

void GetXY (char * x, char * y)
{
 * x = 1;
 * y = 2;
}
Second way - return the pointer as the result:
Code:
typedef struct
{
 char x;
 char y;
} XY_StructTypeDef;



void main (void)
{
 XY_StructTypeDef * XY;
 XY = GetXY();
 ...
}

XY_StructTypeDef * GetXY (void)
{
 static XY_StructnTypeDef XY;
 XY.x = 1;
 XY.y = 2;
 return & XY;
}

Good rule is to use a function to return the error code:
Code:
typedef enum {  Success = 0, Error } Error_TypeDef;
Error_TypeDef XdivY (char x, char y, char * res);

void main (void)
{
  Error_TypeDef ErrorCode;
  char x, y, x_div_y;
  ErrorCode = XdivY (x, y, &x_div_y);
}

Error_TypeDef XdivY (char x, char y, char * res)
{
  if (y == 0) return Error;
  * res = x / y;
  return Success;
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top