CS book people love those obtuse (spelling?) examples.....
Here's a more straight forward example:
(This is Java BTW)
A File is an object representing a file, directory or normal file on an OS.
To get the size of a bunch of files:
(1) If the bunch of files is just empty we return 0
(2) If not an empty bunch of files then we cycle through all the files
and addup the size of each normal file. If there is a directory, then we call
getSize (the same function) on the listing of all the files in that directory and add that size to the total size.
Note that in recursion, there is usually the function calling itself, but there should always be a stopping condition, where the looping terminates, in this case it's when the fileset is empty.
/**
* Get the size of a number of local files and dirs, recursively.
* Does not check for loops
*/
public long getSize(File[] files){
if (files ==null || files.length == 0) return 0;
long returnMe = 0;
for (int i = 0; i < files.length; i ++){
if (files.isDirectory()){
returnMe += getSize(files.listFiles());
}
else{
returnMe+=files.length();
}
}
return returnMe;
}