A recursive function is one that can call itself if necessary. One area where this is generally useful is in traversing filesystem directories. This example deletes a directory on a Win32 system.
void DeleteDirectory( LPCSTR lpdir ){
CHARszFind[MAX_PATH];
HANDLEhFind;
WIN32_FIND_DATAFind;
sprintf( szFind, "%s*", lpdir );
hFind = FindFirstFile( szFind, &Find );
if( hFind == INVALID_HANDLE_VALUE )
return;
do{
if( Find.cFileName[0] == '.' )
continue;
if( Find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ){
sprintf( szFind, "%s%s\\", lpdir, Find.cFileName );
DeleteDirectory( szFind );
}
else{
sprintf( szFind, "%s%s", lpdir, Find.cFileName );
DeleteFile( szFind );
}
}while( FindNextFile( hFind, &Find ) );
FindClose( hFind );
RemoveDirectory( lpdir );
}
It annoys me that this thing doesn't preserve formatting. It makes it hard to read. Copy and paste that into your text editor and it'll look a lot neater.