Jump to content
Sign in to follow this  
careca

Exit Func before finish

Recommended Posts

careca

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

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

GPO Tool - Export/Import Group policy settings.

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.

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

Share this post


Link to post
Share on other sites
FireFox

Return ?


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
careca

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

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

GPO Tool - Export/Import Group policy settings.

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.

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

Share this post


Link to post
Share on other sites
BrewManNH

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

Share this post


Link to post
Share on other sites
junkew

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

Share this post


Link to post
Share on other sites
BrewManNH

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

Share this post


Link to post
Share on other sites
junkew

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

Share this post


Link to post
Share on other sites
careca
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

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

GPO Tool - Export/Import Group policy settings.

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.

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

Share this post


Link to post
Share on other sites
BrewManNH

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

Share this post


Link to post
Share on other sites
careca

Thank you for your input. :)


Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

GPO Tool - Export/Import Group policy settings.

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.

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

Share this post


Link to post
Share on other sites
FireFox

Another thing to add to the ToDo list of the Best Coding Practices wiki page...


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
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
Sign in to follow this  

×