Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How can I return multiple values from a function?
How can I return multiple values from a function?
✍: Guest
There are several ways of doing this. (These examples show hypothetical polar-to-rectangular coordinate conversion functions, which must return both an x and a y coordinate.)
1. Pass pointers to several locations which the function can fill in:
#include <math.h>
polar_to_rectangular(double rho, double theta,
double *xp, double *yp)
{
*xp = rho * cos(theta);
*yp = rho * sin(theta);
}
...
double x, y;
polar_to_rectangular(1., 3.14, &x, &y);
2. Have the function return a structure containing the desired values:
struct xycoord { double x, y; };
struct xycoord
polar_to_rectangular(double rho, double theta)
{
struct xycoord ret;
ret.x = rho * cos(theta);
ret.y = rho * sin(theta);
return ret;
}
...
struct xycoord c = polar_to_rectangular(1., 3.14);
3. Use a hybrid: have the function accept a pointer to a structure, which it fills in:
polar_to_rectangular(double rho, double theta,
struct xycoord *cp)
{
cp->x = rho * cos(theta);
cp->y = rho * sin(theta);
}
...
struct xycoord c;
polar_to_rectangular(1., 3.14, &c);
(Another example of this technique is the Unix system call stat.)
4. In a pinch, you could theoretically use global variables (though this is rarely a good idea).
2015-02-25, 1149👍, 0💬
Popular Posts:
.NET INTERVIEW QUESTIONS - What are Daemon threads and how can a thread be created as Daemon? Daemon...
How does multi-threading take place on a computer with a single CPU? The operating system's task sch...
Describe in detail Basic of SAO architecture of Remoting? For these types of questions interviewer e...
What are unadjusted function points and how is it calculated? Unadjusted function points = ILF + EIF...
How was XML handled during COM times? During COM it was done by using MSXML 4.0. So old languages li...