Wednesday, January 23, 2008

A spoiled programmer

I've been writing quite a bit of Python code recently and I've become a bit spoiled. It's easy in Python to define new classes on the fly, create new functions, pass them here and there, and return arbitrary collections from a method. C will always have a special place in my heart (I think everyone's first language does), but I often think of ways I could make it a bit easier to do certain things like have functions that return functions or have a function return multiple values.

To explain by way of example, it would be fun to do something like this:
/* A function that returns multiple values */
int, char, int myFunction(int a, int b, int c, char d) {...}

...
/* Invoke the function and store the results */
int x, y;
char c;
{x, y, c} = myFunction(5, 6, 7, 'Z');

The above is a fairly Pythonic way of doing things, and it seems like it should be possible in C. The first way I thought of is using structs. I like to think of a struct as the precursor to a class. It allows the arbitrary grouping of variables into a single collection where they can be referred to by name. (In a couple of earlier posts, I showed how you could use structs to simulate classes in C.)

If I define a struct for each one of my multi-variable-returning functions, I can create functions which effectively return multiple values instead of just one. Yes, technically I am returning one value, the struct, but you know what I meant :-) Namely, if you look at the program's stack, there is probably no perceptible difference between returning a struct and returning multiple variables.
/* Create a 2 member struct to hold the return value */
struct myFuncReturn {
int first;
char second;
};

/* A function that returns an int and a char */
struct myFuncReturn myFunc(int a, int b, int c, char d) {
struct myFuncReturn to_return;
to_return.first = (a+b)*c;
to_return.second = d;
return to_return;
}

int main() {
struct myFuncReturn pattern;
pattern = myFunc(2, 3, 4, 'Z');
printf("Pattern: %i, %c\n", pattern.first, pattern.second);
}
This works ok, but I would like to avoid having to create a new struct for each one of my functions. It might be easier if I didn't have to worry about types at all, so the natural choice is to have the function return a type-less void pointer (void*). The calling code would then be responsible for interpreting the function's return struct correctly. If I want to return a new anonymous struct from a function, it might look something like this:
void* myFunc(int a, int b, int c, char d);
If I use the above, I'll need to allocate memory for the struct and return it's address. This is a bit of a bother as well, because now I need to worry about cleaning up that memory later. Instead of having the function allocate a new structure to return, why not pass in a structure and have the function modify it? The code I would need to write would be more aesthetically pleasing (in my opinion) for both the function definition and the calling code which invokes it, and it might even be more efficient.

If I pass in a pointer to the result struct as the first parameter to the function, my program could look like this.
/* Function definition, the out parameter is the return value */
void myFunc(void* out, int a, int b, int c, char d) {
((struct{int first; char second;}*)out)->first = (a+b)*c;
((struct{int first; char second;}*)out)->second = d;
}

int main() {
struct{int first; char second;} pattern;
myFunc(&pattern, 2, 3, 4, 'Z');
printf("Pattern: %i, %c\n", pattern.first, pattern.second);
}
Look ma, no type declarations! Now you might say that writing out the entire struct definition each time is a bit unpleasant, but you could always define a struct and use it instead. I wanted to show that you don't really need to declare a type for each function, which could create a bit of a mess if you start using multi-return functions everywhere. With the above pattern, you could also start to play some interesting games by having functions that actually return different structs in different situations (provided the out pointer's reserved space is large enough for the data you want to send back). If I've lost you by now, I do apologize.

For added effect, note that the anonymous structs don't need to match, you just need to make sure that the shape of the structure is the same so that you don't overwrite data. I could have written myFunc like this:
void myFunc(void* out, int a, int b, int c, char d) {
((struct{int first;}*)out)->first = (a+b)*c;
((struct{int x; char second;}*)out)->second = d;
}
Or if you want to go even further, like this:
void myFunc(void* out, int a, int b, int c, char d) {
*((int*)out) = (a+b)*c;
((struct{int x; char second;}*)out)->second = d;
}
Ah, the joys of programming. It's little games like this that make programming lots of fun. It's like working on a big wide open puzzle that you get to build yourself. No wonder I'm spoiled.

No comments: