Jump to content

Can I call a function from within that function?


Recommended Posts

If I have:

Main()

Func Main()
     While 1
     $oIE = _IECreate("about:blank")

          _IENavigate($oIE, "http://www.google.com")
     If @error <>0 then ; If _IENavigate sends an error of anything but 0 then...
          $x = $x +1 ; Moves to the next value in the array
          _IEQuit($oIE) ; Closes the IE window created from above
          Sleep(3000)
          Main()     <---- Here
     EndIf
     Send("1")
     Sleep(50)
     Send("2")
     Sleep(50)
     Send("3")
EndFunc

Can I call Main() from within Main()? As I write this I can see a problem of possibly creating several While 1 loops. How would some of you accomplish this? I want to start at the beginning of the loop from Call("Main")

Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Link to comment
Share on other sites

Sure, a function can call itself recursively, and you'll get a fresh set of local variables for each call. .

It is one of the biggest sins (only less scary than self-modifying code) in structured programing, but certainly works.

It does also push an address onto a system stack each time you call the function (without having first returned from the prior call), so eventually you can run out of memory.

This runs up about 3900 unfinished function calls before going bang! on my pc:

Test()

Func Test($x = 0)
    $x += 1
    ConsoleWrite($x & @CRLF)
    Test($x)
EndFunc

Edit: There are quite a few other options you could use. Maybe just:

While 1
    While 1
        If $status1 then Exitloop ; restart inner loop
        If $status2 then Exitloop(2) ; exit both loops
    WEnd
WEnd
Edited by Spiff59
Link to comment
Share on other sites

I would suggest:

While 1 ; <--- this starts the infinity loop
Main() ; <--- things which will run inside the loop
WEnd ; <--- it ends the loop (remember to add WEnd to every your While

;You are declaring the function Main()
Func Main()
;START
     $oIE = _IECreate("about:blank") ; creates IE window with blank page
          _IENavigate($oIE, "http://www.google.com") ; connects to google
     If @error <>0 then ; If _IENavigate sends an error of anything but 0 then...
          $x = $x +1 ; Moves to the next value in the array
          _IEQuit($oIE) ; Closes the IE window created from above
          Sleep(3000)
          Main()     <--- at THIS POINT the function ENDS and starts again
;from "START" and it goes again to THIS place if "IF" is correct
;and starts again
     EndIf
     Send("1")
     Sleep(50)
     Send("2")
     Sleep(50)
     Send("3")
EndFunc ;end of your function

That's all about it. Calling functions from inside them will probably cause the program error and close (out of memory).

Better to create 2 functions or instead of calling function again, inserting there simple code:

Func Main()
$done = 0
While $done = 0
$oIE = _IECreate("some stuff") ; your code
_IENavigate("blahblahblah","stuff")
If @error <> 0 then
; some stuff here like closing the window
$done = 0 ; which makes the loop to try again
ElseIf @error = 0 then
$done = 1 ; this will end the loop and go next
WEnd
;Rest of the code

I hope you understand !

Or just listen to Spiff59 :oops:

Edited by VixinG

[indent=3][/indent]

Link to comment
Share on other sites

computergroove,

Every time that you call Main() you are going to get another instance of IE navigating to Google. Is this REALLY what you want?

kylomas

Yeah, thats why I suggest using the While:
  • Func Main()
  • $i = 0
  • While $i = 0
  • create IE, navigate
  • If @error <> 0 Then
  • close etc.
  • $i = 0 - to loop it again, because there was an error
  • ElseIf @error = 0 Then
  • $i = 1 - to exit the loop and go forward
  • EndIf
  • rest of the code...
This is good for short functions. Edited by VixinG

[indent=3][/indent]

Link to comment
Share on other sites

@VixinG, Sound advice...

$x = $x +1 ; Moves to the next value in the array

This, however, makes me believe that we do NOT have the entire story...

kylomas

yea, that's because he created 2 or 3 other topics with parts of some scripts and trying to make them work.

[indent=3][/indent]

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...