Jump to content

Return level from function, like ExitLoop level from loop?


Go to solution Solved by trancexx,

Recommended Posts

hello world,

i'm looking for some method like ExitLoop level, but for Return from functions (not confusing with function return value).

i.e. ExitLoop can get you out of a loop1, and out of loop2 that loop1 is in, if you specify level 2.

i want to call func1, and func1 needs to call func2, and i want func2 to return to func1 and make func1 also return to the calling script.

currently i'm doing it with a condition check, but i'm looking for something more elegant, and with less code lines.

Func func1()
    If True Then    ; the condition that makes func1 call func2
        func2()
        Return
    EndIf
EndFunc
Func func2()
    Return ; how to make return level 2 that will also make func1 return?
EndFunc

i want func1 to look like this:

Func func1()
    If True Then func2()    ; func1 will return upon func2 return, so the next lines in func1 are not executed
    ; do something
EndFunc

ok, so it does not make a lot of sense in this short example, but over a longer project, the coding difference is apparent.

any ideas are welcome, thanks all.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

You will have to design your own. Why not use SetError instead and using Return 2 won't return from the 2nd function.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

i figured this much, but that will still impose a Return statement in func1 right after the call to func2, and this is what i want to avoid.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Sorry, trancexx is on the mark. It's early, that's my excuse.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 7 years later...
  • Moderators

zkenjiz,

Welcome to the AutoIt forums.

The answer from trancexx a couple of posts above is the answer.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Developers
4 hours ago, zkenjiz said:

After 8 years, there are no answer for this, I need it too, sad.

First post and unable to read...  sad. ;) 

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

6 hours ago, Melba23 said:

zkenjiz,

Chào mừng đến với diễn đàn AutoIt.

Câu trả lời từ trancexx một vài bài viết trên là câu trả lời.

M23

Thanks, but that answer not match the question. As above question, I don't think there a many many line with "If Not _Func() Then Return" to come back main loop at anytime when event catched is a good choose. 

Link to comment
Share on other sites

  • Developers

Post what you have that isn't working and explain the issue.

Also please stop adding Translated Quoted text in your replies!

Jos 

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

Sorry for the translated quoted.

This is an example code I using a custom sleep function to Sleep() and check event all the time. The problem I find for this situation: Return Multilevel like ExitLoop(2).

If I need Exit when $event=true (anytime when Func _Sub is on running) It's simple by "If $event Then Exit", but if I need return to mainloop by "If Not _iSleep() Then Return" code is terrible , is there any better way?

$callFunc = True
$event = False

_Main()
Func _Main()
    While 1
        If $callFunc Then _Sub()
    WEnd
EndFunc   ;==>_Main

Func _Sub()
    ConsoleWrite('do work 1' & @CRLF)
    If Not _iSleep(1000) Then Return
    ConsoleWrite('do work 2' & @CRLF)
    If Not _iSleep(2000) Then Return
    ConsoleWrite('do work 6' & @CRLF)
    If Not _iSleep(500) Then Return
    ConsoleWrite('do work 4' & @CRLF)
    If Not _iSleep(3000) Then Return
    ConsoleWrite('do work 2' & @CRLF)
    If Not _iSleep(5000) Then Return
    ConsoleWrite('do work 1' & @CRLF)
    If Not _iSleep(1000) Then Return
    ConsoleWrite('do work 7' & @CRLF)
    If Not _iSleep(500) Then Return
    ConsoleWrite('do work a' & @CRLF)
    If Not _iSleep(10000) Then Return
    ConsoleWrite('do work c' & @CRLF)
    If Not _iSleep(4000) Then Return
    ConsoleWrite('do work g' & @CRLF)
    If Not _iSleep(1000) Then Return
    ;............
EndFunc   ;==>_Sub

Func _iSleep($sleepTime)
    Local $timer = TimerInit()
    While TimerDiff($timer) < $sleepTime
        Sleep(10)
        If $event Then Return False
    WEnd
    Return True
EndFunc   ;==>_iSleep

 

Link to comment
Share on other sites

Hi,

You can use something like this:

#include <Misc.au3>

$callFunc = True
$event = False

Global $statemachine ,$quit
_Main()
Func _Main()
    While 1
        If $callFunc Then
            _Sub()
            ConsoleWrite ("out" & @crlf)
        EndIf
        If $quit=1 then Return
    WEnd
EndFunc   ;==>_Main

Func _Sub()
    $quit = 0
    $statemachine=0

    While $quit = 0
        Switch $statemachine
            Case 0
                ConsoleWrite('do work 1' & @CRLF)
                _iSleep(1000)
            Case 1

                ConsoleWrite('do work 2' & @CRLF)
                _iSleep(2000)
            Case 2

                ConsoleWrite('do work 6' & @CRLF)
                _iSleep(500)
            Case 3

                ConsoleWrite('do work 4' & @CRLF)
                _iSleep(3000)
            Case 4
                ConsoleWrite('do work 2a' & @CRLF)
                _iSleep(5000)
            Case 5
                ConsoleWrite('do work 1a' & @CRLF)
                _iSleep(1000)
            Case 6
                ConsoleWrite('do work 7' & @CRLF)
                _iSleep(500)
            Case 7
                ConsoleWrite('do work a' & @CRLF)
                _iSleep(10000)
            Case 8
                ConsoleWrite('do work c' & @CRLF)
                _iSleep(4000)
            Case 9
                ConsoleWrite('do work g' & @CRLF)
                _iSleep(1000)
            Case 10
                $statemachine=0
                ;............
        EndSwitch
    WEnd
    $callFunc=0
EndFunc   ;==>_Sub

Func _iSleep($sleepTime)
    Local $timer = TimerInit()
    While TimerDiff($timer) < $sleepTime
        Sleep(10)
        If _isPressed("1b") Then
            $quit = 1
            $statemachine = 1000
            Return False
        EndIf
    WEnd
    $statemachine=$statemachine+1
    Return True
EndFunc   ;==>_iSleep

Of course you can skip the Switch/Endswitch and use

If Not _iSleep(1000) Then Return

as well.

Edited by Dan_555

Some of my script sourcecode

Link to comment
Share on other sites

Another way :

$callFunc = True
$event = False

_Main()
Func _Main()
    While $callFunc
        _Sub()
    WEnd
EndFunc   ;==>_Main

Func _Sub()
    Local $aWork = [[_Work1,1000],[_Work2,2000],[_Work6,500]] ;.....
    For $i = 0 to UBound($aWork) - 1
      $aWork[$i][0]()
      If Not _iSleep($aWork[$i][1]) Then Return
    Next
EndFunc   ;==>_Sub

Func _iSleep($sleepTime)
    Local $timer = TimerInit()
    While TimerDiff($timer) < $sleepTime
        Sleep(10)
        If $event Then Return False
    WEnd
    Return True
EndFunc   ;==>_iSleep

Func _Work1()
  ConsoleWrite ("job1" & @CRLF)
  Return
EndFunc

Func _Work2()
  ConsoleWrite ("job2" & @CRLF)
  Return
EndFunc

Func _Work6()
  ConsoleWrite ("job6" & @CRLF)
  Return
EndFunc

 

Link to comment
Share on other sites

1. Can you give an example on what you need from any other programming language?

2. I think what @Nine has given is an elegant programming style to make for each job a worker function which is dispatched with assigning a function to a variable

Personally I would feel returning multiple nested levels back will make your code hard to maintain and understand.

Even exitloop nn should be used with care

Link to comment
Share on other sites

#include <Misc.au3>

$callFunc = True
$event = False

Global $quit
_Main()
Func _Main()
    While 1
        If $callFunc Then
            _Sub()
            ConsoleWrite ("out" & @crlf)
        EndIf
        If $quit=1 then Return
    WEnd
EndFunc   ;==>_Main

Func _Sub()
    $quit = 0

    While $quit = 0

    ConsoleWrite('do work 1' & @CRLF)
    If Not _iSleep(1000) Then Return
    ConsoleWrite('do work 2' & @CRLF)
    If Not _iSleep(2000) Then Return
    ConsoleWrite('do work 6' & @CRLF)
    If Not _iSleep(500) Then Return
    ConsoleWrite('do work 4' & @CRLF)
    If Not _iSleep(3000) Then Return
    ConsoleWrite('do work 2' & @CRLF)
    If Not _iSleep(5000) Then Return
    ConsoleWrite('do work 1' & @CRLF)
    If Not _iSleep(1000) Then Return
    ConsoleWrite('do work 7' & @CRLF)
    If Not _iSleep(500) Then Return
    ConsoleWrite('do work a' & @CRLF)
    If Not _iSleep(10000) Then Return
    ConsoleWrite('do work c' & @CRLF)
    If Not _iSleep(4000) Then Return
    ConsoleWrite('do work g' & @CRLF)
    If Not _iSleep(1000) Then Return

    WEnd
    $callFunc=0
EndFunc   ;==>_Sub

Func _iSleep($sleepTime)
    Local $timer = TimerInit()
    While TimerDiff($timer) < $sleepTime
        Sleep(10)
        If _isPressed("1b") Then            ;Check for the escape key.
            $quit = 1           
            Return False
        EndIf
    WEnd
    
    Return True
EndFunc   ;==>_iSleep

As said previously, it works even with your sample code. 

Start it, then press esc, and it jumps out of the two sub's.

Edited by Dan_555
removed the $statemachine refference

Some of my script sourcecode

Link to comment
Share on other sites

8 hours ago, Dan_555 said:
 

As said previously, it works even with your sample code. 

Start it, then press esc, and it jumps out of the two sub's.

Right, that code work but in fact the code is not so few lines, anytime I call _iSleep() I must type "If Not _iSleep() Then Return" - every where every time, so it not ok because _iSleep() using like Sleep and I call it many times in script, if somethings exitst like Return(2) from inside _iSleep(), I can only type _iSleep().

Of course, I think this need comes from the lack of multiprocess, so its not need in other language like C# example.

Edited by zkenjiz
Link to comment
Share on other sites

Basic rule of programming:

  • If you feel you are writing the same to frequently probably time to refactor your coding style
    • No multiprocess is not an excuse as you can see in @nine 's answer there is an elegant way to have it handled in a maintainable and understandable way

 

Link to comment
Share on other sites

20 minutes ago, junkew said:

Basic rule of programming:

  • If you feel you are writing the same to frequently probably time to refactor your coding style
    • No multiprocess is not an excuse as you can see in @nine 's answer there is an elegant way to have it handled in a maintainable and understandable way

 

I working with AutoIt over 10 years, similar with C# or Java, so I don't thing my coding style is problem here, not all situation can change to using switch or loop sequence, especially in script programming. It's difficult to understand the need here if you are not really in that situation.

Ok, so I thing there's no right answer for finding multi level return like exitloop.

Thanks all!

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