Jump to content

Can a funktion call itself


Konan--M
 Share

Recommended Posts

Is it possible that a function calls itself with other parameters and if the new called funktion ends, that the old one's going on ????

I don't care about the limiter of 384 reg levels, just want to know if its possible?

<{POST_SNAPBACK}>

Possible.

Personally I think it's stupid.

I made many scripts and I've always found a way to avoid this.

Here's what silly people would do:

RunThis("C:\WINNT\notepad.exe", "C:\WINNT\WindowsUpdate.log")

Func RunThis($par1, $par2)
   Local $Filename
   Local $Args
   
   $Filename = $par1
   $Args = $par2

   If NOT FileExists($Filename) Then
      $Filename = StringReplace($Filename, "C:\", "D:\")
      RunThis($Filename, $Args)
   Else
      Run($Filename & " " & $Args)
   EndIf
EndFunc

Here's what I would do:

RunThis("C:\WINNT\notepad.exe", "C:\WINNT\WindowsUpdate.log")
If @error Then
   RunThis("D:\WINNT\notepad.exe", "D:\WINNT\WindowsUpdate.log")
EndIf

Func RunThis($par1, $par2)
   Local $Filename
   Local $Args
   
   $Filename = $par1
   $Args = $par2

   If NOT FileExists($Filename) Then
      SetError(1)
      Return
   Else
      Run($Filename & " " & $Args)
   EndIf
EndFunc
Link to comment
Share on other sites

  • Administrators

Possible.

Personally I think it's stupid.

I made many scripts and I've always found a way to avoid this.

I used to think it was silly when I heard about it too, but most of AutoIt's internals are based on the fact that functions call themselves. Everytime you run a function AutoIt actually reruns it's main procedure.

Try and write a program that gives you a list of every file on a disk without recursion - pretty much impossible :ph34r:

Edited by Jon
Link to comment
Share on other sites

I used to think it was silly when I heard about it too, but most of AutoIt's internals are based on the fact that functions call themselves.  Everytime you run a function AutoIt actually reruns it's main procedure.

Try and write a program that gives you a list of every file on a disk without recursion - pretty much impossible :ph34r:

<{POST_SNAPBACK}>

I wasn't sure, because in my first scripts i tried recursion´, but it didn't work.

And now i really need it and i wanted to be sure :(

Link to comment
Share on other sites

I used to think it was silly when I heard about it too, but most of AutoIt's internals are based on the fact that functions call themselves.  Everytime you run a function AutoIt actually reruns it's main procedure.

Try and write a program that gives you a list of every file on a disk without recursion - pretty much impossible :ph34r:

<{POST_SNAPBACK}>

What language? AutoIt? Very hard. C++, probably not all that hard if you used a linked-list that not only had siblings but children as well.

class NodeDir
{
public:
    std::string name;
    NodeDir *pNextSibling;
    NodeDir *pPrevSibling;
    NodeDir *pParent;
    std::vector<NodeDir*> pChild;
};

Then use a stack to track which node should be the root, pushing a new "root" on each time you go into a sub-directory. Once you hit bottom, pop the last item, look to see if there is another child in the new "root" (Sibling to the "root" just popped). Keep this up until everything is popped off the stack signifying you've fully traversed the directory structure of the drive. This would trigger an exit from the end of the while loop.

Edit: Changed pChild to be a vector of pointers since it'd be necessary to have access to all children from the parent.

Edited by Valik
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...