The Lazy Programmer

January 5, 2009

Returning multiple values from a function in C++

Filed under: C#,Programming — ferruccio @ 10:22 pm
Tags: , ,

Ideally all functions should return just one value. There are many times, however, when returning more than one value makes a function so much more convenient. The classic example of this convenience is file input. When we read data from a file we want to know two things: Did we reach the end of the file? and if not, what is the next piece of data from the file.

Sometimes, we can encode multiple return values into one. For many of us, the first C idiom we learned from K&R is processing input a character at a time:

int ch;
while ((ch = getchar()) != EOF) {
    // do character processing...

This works because the EOF macro was set to something outside the range of valid characters (usually -1). While this approach can work fairly well for simple cases, it quickly breaks down as the types we wish to return get more complex.


November 18, 2008

Functional C++

Filed under: C#,Programming — ferruccio @ 9:56 pm
Tags: , ,

It’s been awhile since my last post. It was near the end of a product shipping cycle and, well, you know how that goes. Then I got a bit addicted to stackoverflow and spent way too much time each night reading and answering  questions. Eventually, I started a couple of side-projects which may eventually yield something interesting to write about.

Anyway, a little over a month ago, I answered a question on Stack Overflow titled “What is the one programming skill you always wanted to master but haven’t had time?” I didn’t have to think much to come up with an answer to that: Functional Programming.

I understand some of the fundamental concepts behind functional programming and occasionally I dabble a bit with LISP or I read a bit more of SICP but the practical applications of FP have been elusive to me.


Blog at