user3195513
user3195513

Reputation: 11

Could someone explain the word recursive in relation to computers/programming? I Am having a difficult time understanding its use

In my computing course, the word recursively is used quite a bit - I'm trying to have a deeper understanding of its use and was wondering if someone could enlighten me.

EX: Searches HKLM recursively for the Key SAM

Upvotes: 1

Views: 3264

Answers (3)

JosefZ
JosefZ

Reputation: 30123

@vekerdyb's answer is right: recursively in search terms usually means to search in each (sub)level. Of course, recursion (in computer science) is a bit more universal (more general) concept (see also @Maffe's answer).

Taking your example as a task request "search HKLM recursively for the key SAM" then a solution in Windows cmd could stem from reg.exe utility, see also (truncated) excerpt from reg query /? help:

REG QUERY KeyName [/v [ValueName] | /ve] [/s]
          [/f Data [/k] [/d] [/c] [/e]] [/t Type] [/z] [/se Separator]
          [/reg:32 | /reg:64]

  /s       Queries all subkeys and values recursively (like dir /s).

  /f       Specifies the data or pattern to search for.
           Use double quotes if a string contains spaces. Default is "*".

  /k       Specifies to search in key names only.

  /c       Specifies that the search is case sensitive.
           The default search is case insensitive.

  /e       Specifies to return only exact matches.
           By default all the matches are returned.

For explanation of /s switch recursively (like dir /s) see dir /? | find /I " /s"

/S    Displays files in specified directory and all subdirectories.

Now, you can see the difference: search HKLM for the key SAM (not recursively)

reg query HKLM /K /C /E /F SAM
HKEY_LOCAL_MACHINE\SAM
End of search: 1 match(es) found.

versus search HKLM recursively for the key SAM:

reg query HKLM /K /C /E /F SAM /S
HKEY_LOCAL_MACHINE\SAM
HKEY_LOCAL_MACHINE\SAM\SAM
HKEY_LOCAL_MACHINE\SECURITY\SAM
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Audit\SystemPolicy\ObjectAccess\SAM
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\SAM
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\EventLog\System\SAM
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SAM
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\System\SAM
End of search: 8 match(es) found.

Upvotes: 2

vekerdyb
vekerdyb

Reputation: 1263

@Maffe's answer is correct, but to expand to your example, "recursively" in search terms usually means to search in each level.

(I don't know what HKLM Key SAM are in the context of your example so instead I will use a simple file-system search.)

If your working directory is /home/user1 and you search (non-recursively) for the file test.txt, it means that you are searching for files called /home/user1/test.txt.
If you are searching recursively, it means that you search the current directory /home/user1, and any subdirectories (like /home/user1/documents), and any subdirectories of subdirectories (like /home/user1/documents/tests), etc.

In pseudo code:

define searchRecursively(path, filename):
  results := []
  for every file in path:
    if file.name is filename:
      add file to the list of results
    if file.type is directory:
      subresults := searchRecursively(file, filename) # this is why it is recursive
      add all elements of subresults to results
  return results

Upvotes: 0

Maffe
Maffe

Reputation: 440

"The best way to understand recursion is understand recursion."

Generally speaking, recursion happens when you use something to explain itself.

In computer programming, recursion happens when you call a function/method from itself. For example, this function:

    function printHello() {

        print("Hello!")   //this prints "Hello!" on the console
        printHello()      //this starts from the beginning
    }

will run endlessly printing "Hello!" infinite time.

Recursion can be useful in different situations, for example to computate the factorial of a number.

In maths, the factorial of n is the product (n)(n-1)(n-2)(n-3)...

In programming, we can computate the factorial of a number n with a recursive function, in this way:

//recursive
var factorial = function(n) {
    if(n == 0) {
        return 1
    } else {
        return n * factorial(n - 1);
    }
}

console.log(factorial(num));

Upvotes: 0

Related Questions