Jump to content

Exit Func before finish


 Share

Go to solution Solved by FireFox,

Recommended Posts

Hello, i would like to know if it's possible to exit the func earlier, let's say i have a function like this:

Func Test()
ConsoleWrite('test1' & @CRLF)
If $T = 1 Then
;ExitFunc ---- exit here, skip the rest.
EndIf
 ConsoleWrite('test2' & @CRLF)
If $T = 2 Then
;BlahBlah
EndIf
EndFunc   ;==>Mail2
Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

yeah, the thing is, never have used it before, and don't really get how to use it, helpfile is not being very usefull in explaining what it does.

Forget about it, made a plactical example, that worked, thank you, learning everyday.

today()
Func today() ;Return the current date in mm/dd/yyyy form
    If @YEAR = '2011' Then
        ConsoleWrite('2011'&@CRLF)
        Return (@MON & "/" & @MDAY & "/" & @YEAR)
    EndIf
    If @YEAR = '2013' Then
        ConsoleWrite('2013'&@CRLF)
        Return (@MON & "/" & @MDAY & "/" & @YEAR)
    EndIf
    If @YEAR = '2012' Then
        ConsoleWrite('2012'&@CRLF)
        Return (@MON & "/" & @MDAY & "/" & @YEAR)
    EndIf
EndFunc   ;==>today
Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

That's not a very well written example, as it could be done much easier with one point of return.

today()
Func today() ;Return the current date in mm/dd/yyyy form
    If @YEAR = '2011' Then
        ConsoleWrite('2011' & @CRLF)
    ElseIf @YEAR = '2013' Then
        ConsoleWrite('2013' & @CRLF)
    ElseIf @YEAR = '2012' Then
        ConsoleWrite('2012' & @CRLF)
    EndIf
    Return (@MON & "/" & @MDAY & "/" & @YEAR)
EndFunc   ;==>today

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Whats the goal of the example. The if statements seems silly at all. See shorthand version below.

If the point was just to demonstrate return at different places it depends of the internal interpreter of AutoIT whats more efficient.

return will probably immediately return whereas elseif first will jump to the return just before the end of the function and as such a little less efficient.

Personal opinion is have as less returns in a function as possible but its just a coding style where you decide to return

today()
Func today() ;Return the current date in mm/dd/yyyy form
consolewrite(@YEAR & @CRLF)
    Return (@MON & "/" & @MDAY & "/" & @YEAR)
EndFunc   ;==>today
Link to comment
Share on other sites

Functions should have one point of entry, and one point of exit, or a different return upon an error condition. Jumping out of a function in the middle of it can lead to some interesting confusions as to WTF the function is supposed to be doing.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Just a different example for a simple card game function.

On Google there are many personal opinions on subject "coding practices multiple return"

third example How if probably would code it

consolewrite(card2Text1(1) & @CRLF)
consolewrite(card2Text1(11) & @CRLF)
consolewrite(card2Text2(2) & @CRLF)
consolewrite(card2Text2(12) & @CRLF)
consolewrite(card2Text3(0) & @CRLF)
consolewrite(card2Text3(14) & @CRLF)
func card2text1($ival)
    Select
        case $iVal = 1
            return "Ace"
        case $iVal = 2
            return "Two"
        case $iVal = 3
            return "Three"
        case $iVal = 4
            return "Four"
        case $iVal = 5
            return "Five"
        case $iVal = 6
            return "Six"
        case $iVal = 7
            return "Seven"
        case $iVal = 8
            return "Eight"
        case $iVal = 9
            return "Nine"
        case $iVal = 10
            return "Ten"
        case $iVal = 11
            return "Jack"
        case $iVal = 12
            return "Queen"
        case $iVal = 13
            return "King"
        case Else
            return "It must be the Joker"
    EndSelect
EndFunc

func card2text2($ival)
    Select
        case $iVal = 1
            $retVal= "Ace"
        case $iVal = 2
            $retVal= "Two"
        case $iVal = 3
            $retVal= "Three"
        case $iVal = 4
            $retVal= "Four"
        case $iVal = 5
            $retVal= "Five"
        case $iVal = 6
            $retVal= "Six"
        case $iVal = 7
            $retVal= "Seven"
        case $iVal = 8
            $retVal= "Eight"
        case $iVal = 9
            $retVal= "Nine"
        case $iVal = 10
            $retVal= "Ten"
        case $iVal = 11
            $retVal= "Jack"
        case $iVal = 12
            $retVal= "Queen"
        case $iVal = 13
            $retVal= "King"
        case Else
            $retVal= "It must be the Joker"
    EndSelect

    return $retval
EndFunc

func card2text3 ($iVal)
    $aCardNames=stringsplit("Ace,Two,Three,Four,Five,Six,Seven,Eight,Ten,Jack,Queen,King,It must be the joker",",")

    if ($ival<=0) or ($ival>=ubound($aCardnames)) Then
        return $aCardnames[ubound($aCardnames)-1]
    Else
        return $aCardnames[$iVal]
    EndIf
EndFunc
Exit 
Edited by junkew
Link to comment
Share on other sites

If the point was just to demonstrate return at different places it depends of the internal interpreter of AutoIT whats more efficient.

 

Yes it was, totally made up according to something with the same structure i had on another code.

The objective was: search, if found, exit that function, meaning no need to keep on searching.

I guess it depends on the situation, in this situation, and for the code i got, i works very well.

I just skip all the if's and whatever code is next to it, i find that more simple.

Thanks all.

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

An "If/ElseIf/Else/Then", "Select/Case", "Switch/Case" statement exits the comparisons as soon as it finds the first match. Anyone using more than one return statement in such a case is making things several degrees more complicated than it needs to be.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thank you for your input. :)

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

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