What is recursion and when should I use it?

The most simple example would be,
browsing throw directory’s

I find this example:

void Mainwindow::ReadDir(QString path) {
    QDir dir(path);                            //Opens the path
    QFileInfoList files = dir.entryInfoList(); //Gets the file information
    foreach(const QFileInfo &fi, files) {      //Loops through the found files.
        QString Path = fi.absoluteFilePath();  //Gets the absolute file path
        if(fi.isDir()) ReadDir(Path);          //Recursively goes through all the directories.
        else {
            //Do stuff with the found file.
I had a Job interview and had this question  (30 min. on the Phone).
I unable to recall the example for recursion.  
Sadly my brain doesn't work very well under pressure.

No comments:

Post a Comment