Dec 7, 2014 #1 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.
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.
Dec 7, 2014 #2 Easyrider83 Advanced Member level 5 Joined Oct 11, 2011 Messages 1,608 Helped 374 Reputation 748 Reaction score 364 Trophy points 1,363 Location Tallinn, Estonia Activity points 8,575 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; }
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; }