Jump to content

Can you have a Function inside a Function?


Recommended Posts

yes, but you should write it like this: and you can also return parameters if you define them. and don't put Exit anywhere except the mainfunction or in a IF statement or check.

Call("MainFunction")

Func MainFunction()

Call("SubFunction")

MsgBox (0,"end:","")

EndFunc

Func SubFunction()

MsgBox(1024,"Test","This is a test!")

EndFunc

Link to comment
Share on other sites

  • Developers

Only shouldn't be using the Call() statement in these cases :)

MainFunction()
;
Func MainFunction()
    SubFunction()
    MsgBox(0, "end:", "")
EndFunc  ;==>MainFunction
;
Func SubFunction()
    MsgBox(1024, "Test", "This is a test!")
EndFunc  ;==>SubFunction

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

yes, but you should write it like this: and you can also return parameters if you define them. and don't put Exit anywhere except the mainfunction or in a IF statement or check.

Call("MainFunction")

Func MainFunction()

Call("SubFunction")

MsgBox (0,"end:","")

EndFunc

Func SubFunction()

MsgBox(1024,"Test","This is a test!")

EndFunc

No. Your example doesn't show a nested function. It does prove what I stated. And, of course, Jos is correct (call is not necessary). Also, I think it's good practice to "run" another function conditionally from within a function (unless it's for organization purposes) but try not to run a function, which runs another function... which runs another function.

Link to comment
Share on other sites

  • 6 months later...

is there any way to have the script return to the first function where it left off after running the second function? muttley

At last, after two thousand years of research, the illudium Q-36 explosive space modulator.Well, back to the old drawing board.

Link to comment
Share on other sites

is there any way to have the script return to the first function where it left off after running the second function? muttley

Have you found a way to stop it doing that then?
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • Moderators

Have you found a way to stop it doing that then?

No kidding, I had to read that 3 or 4 times lol.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I am not looking to simply go back to the top of the first function, I am looking to basically do a GoSub comand but there is not GoSub comand in version 3. muttley

At last, after two thousand years of research, the illudium Q-36 explosive space modulator.Well, back to the old drawing board.

Link to comment
Share on other sites

  • Moderators

I am not looking to simply go back to the top of the first function, I am looking to basically do a GoSub comand but there is not GoSub comand in version 3. muttley

Seems the consensus on the net is that no language (worth it's salt) is using GoSub/GoTo anymore "on purpose".

For your "GoSub/GoTo" call, it's better to create independent functions and call them as needed.

Proper code structuring makes for less errors and better readability for even the coder themselves.

http://www.autoitscript.com/forum/index.ph...st&p=505435

http://www.autoitscript.com/forum/index.php?showtopic=72973

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I am not looking to simply go back to the top of the first function, I am looking to basically do a GoSub comand but there is not GoSub comand in version 3. muttley

From your last post, it appears your still have not got it.

Experiment, it's a great teacher. Jos's example with more MsgBox()'s (Message Boxes).

MainFunction()
MsgBox(0, "", "Just after where  MainFunction() was called at the start")

Func MainFunction()
    MsgBox(0, "", "Top of Main Function")
    SubFunction()
    MsgBox(0, "", "This is in Main Function() just after the SubFunction() statement")
EndFunc  ;==>MainFunction

Func SubFunction()
    MsgBox(1024, "", "This is from/inside the SubFunction()")
    Return
EndFunc  ;==>SubFunction

As you should be able to see is the function that is called from inside the main function does not go back to the top of the first function which it appears you think it does. It doesn't.

It acts more like a GoSub command, which you want.

The commands in a GoSub block make up the function. And this function (GoSub block) is stored outside of any other functions. It takes it place separately among the other functions you create.

If the SubFunction() command returned to the top of the MainFunction() from where it was called, then the MsgBox(0, "", "Top of Main Function") command would appear a second time after the "This is from/inside the SubFunction()" message.

I hope this clarifies what's happening.

Edit: Added Another MsgBox() now @ 2nd line

Edited by Malkey
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...