Jump to content

does Return exit a function?


gcue
 Share

Recommended Posts

hello.

i am trying to exit a function and return a value to another function.  Test is not a working function.  There are several components to make it working that would take me time to put together.  Hopefully there is enough to illustrate the issue.

Issue: I can always get $recipient to output correctly in consolewrite, but i don't always get it back to Test() where i am calling it from.

I think Return isn't exiting the GetRecipient function as expected.  I have a bunch of IF statements and several instances where GetRecipient gets called multiple times (when i cant get a recipient) so i think that's where the issue lies

any help is greatly appreciated!

Func Test()
    $sender = "bob"
    $recipient = GetRecipient($sender)
    consolewrite($recipient & @crlf)
endfunc

Func GetRecipient($sender)

    $recipient_found = False

    ConsoleWrite(@CRLF & @CRLF & "sender:" & $sender & @CRLF)
    $record = GetRandom($master_array)

    $recipient = $master_array[$record][0]
    ConsoleWrite("recipient:" & $recipient & @CRLF)
    If $sender <> $recipient Then
        ConsoleWrite("sender not same as recipeint" & @CRLF)
        $block = $master_array[$record][3]

        If $block = "" Then
            $recipient_found = True
            ConsoleWrite("0 unassigned recipeint found! " & $recipient & @CRLF) ;;;;RECIPIENT IS OUTPUT CORRECTLY IN CONSOLEWRITE BUT NOT ALWAYS RETURNED
            $master_array[$record][5] = "ASSIGNED"
            Return $recipient
        EndIf

        $block_array = StringSplit($block, ",")

        If @error Then
            ConsoleWrite("only 1 blocked for recipient" & @CRLF)
            If $sender = $block Then
                GetRecipient($sender)
            Else
                If $master_array[$record][5] = "ASSIGNED" Then
                    ConsoleWrite("recipeint already assigned" & @CRLF)
                    GetRecipient($sender)
                Else
                    $recipient_found = True
                    ConsoleWrite("1 unassigned recipeint found! " & $recipient & @CRLF);;;;RECIPIENT IS OUTPUT CORRECTLY IN CONSOLEWRITE BUT NOT ALWAYS RETURNED
                    $master_array[$record][5] = "ASSIGNED"
;~                  Debug($master_array)
                    Return $recipient
                EndIf
            EndIf
        Else
            _ArraySearch($block_array, $sender)

            If Not @error Then
                ConsoleWrite("recipeint found in block array" & @CRLF)
                GetRecipient($sender)
            Else
                ConsoleWrite("recipeint not found in block array" & @CRLF)
                If $master_array[$record][5] = "ASSIGNED" Then
                    ConsoleWrite("recipeint already assigned" & @CRLF)
                    GetRecipient($sender)
                Else
                    $recipient_found = True
                    ConsoleWrite("2 unassigned recipeint found! " & $recipient & @CRLF);;;;RECIPIENT IS OUTPUT CORRECTLY IN CONSOLEWRITE BUT NOT ALWAYS RETURNED
                    $master_array[$record][5] = "ASSIGNED"
;~                  Debug($master_array)
                    Return $recipient
                EndIf
            EndIf
        EndIf
    Else
        ConsoleWrite("sender same as recipeint" & @CRLF)
        GetRecipient($sender)
    EndIf
    
    Consolewrite("ERROR" & @crlf);sometimes the script gets to this point which i have no idea why

EndFunc   ;==>GetRecipient

 

Edited by gcue
Link to comment
Share on other sites

ok here's a working sample (wasnt as hard as i thought it would be :) )-- line 113 outputs $sender and $recipient.  sometimes i do not get $recipient, even though $recipient was in the consolewrite just before the Return

#include <array.au3>
$msg_normal = 0


Global $array[4][3]

Test()

Func GetRandom($array)
    $record = Random(0, UBound($array) - 1, 1)
    Return $record
EndFunc   ;==>GetRandom


Func GetRecipient($sender)

    $recipient_found = False

    ConsoleWrite(@CRLF & @CRLF & "sender:" & $sender & @CRLF)
    $record = GetRandom($array)

    $recipient = $array[$record][0]
    ConsoleWrite("recipient:" & $recipient & @CRLF)
    If $sender <> $recipient Then
        ConsoleWrite("sender not same as recipeint" & @CRLF)
        $block = $array[$record][1]

        If $block = "" Then
            $recipient_found = True
            ConsoleWrite("0 unassigned recipeint found! " & $recipient & @CRLF)
            $array[$record][2] = "ASSIGNED"
            Return $recipient
        EndIf

        $block_array = StringSplit($block, ",")

        If @error Then
            ConsoleWrite("only 1 blocked for recipient" & @CRLF)
            If $sender = $block Then
                GetRecipient($sender)
            Else
                If $array[$record][2] = "ASSIGNED" Then
                    ConsoleWrite("recipeint already assigned" & @CRLF)
                    GetRecipient($sender)
                Else
                    $recipient_found = True
                    ConsoleWrite("1 unassigned recipeint found! " & $recipient & @CRLF)
                    $array[$record][2] = "ASSIGNED"
;~                  Debug($array)
                    Return $recipient
                EndIf
            EndIf
        Else
            _ArraySearch($block_array, $sender)

            If Not @error Then
                ConsoleWrite("recipeint found in block array" & @CRLF)
                GetRecipient($sender)
            Else
                ConsoleWrite("recipeint not found in block array" & @CRLF)
                If $array[$record][2] = "ASSIGNED" Then
                    ConsoleWrite("recipeint already assigned" & @CRLF)
                    GetRecipient($sender)
                Else
                    $recipient_found = True
                    ConsoleWrite("2 unassigned recipeint found! " & $recipient & @CRLF)
                    $array[$record][2] = "ASSIGNED"
;~                  Debug($array)
                    Return $recipient
                EndIf
            EndIf
        EndIf
    Else
        ConsoleWrite("sender same as recipeint" & @CRLF)
        GetRecipient($sender)
    EndIf

EndFunc   ;==>GetRecipient

Func Array()

    ;0 - name
    ;1 - block list
    ;2 - assigned or not

    $array[0][0] = "bob"
    $array[0][1] = "mary,beth"

    $array[1][0] = "mary"
    $array[1][1] = "tom"

    $array[2][0] = "beth"
    $array[2][1] = ""

    $array[3][0] = "tom"
    $array[3][1] = ""

    Return $array

EndFunc   ;==>Array

Func Test()

;~  local $final_array[1][1]

    $array = Array()

    For $x = 0 To UBound($array) - 1
        $sender = $array[$x][0]

        $recipient = GetRecipient($sender)

        Debug($sender, $recipient)
    Next

EndFunc   ;==>Test

Func Debug($variable1 = "", $variable2 = "", $variable3 = "", $variable4 = "", $variable5 = "")

    If IsArray($variable1) Or IsArray($variable2) Then
        If IsArray($variable1) Then _ArrayDisplay($variable1, $variable2)
        If IsArray($variable2) Then _ArrayDisplay($variable2, $variable1)
    Else
        $variable = ""

        If $variable1 <> "" Then $variable &= $variable1 & ": " & Execute($variable1) & @CRLF
        If $variable2 <> "" Then $variable &= $variable2 & ": " & Execute($variable2) & @CRLF
        If $variable3 <> "" Then $variable &= $variable3 & ": " & Execute($variable3) & @CRLF
        If $variable4 <> "" Then $variable &= $variable4 & ": " & Execute($variable4) & @CRLF
        If $variable5 <> "" Then $variable &= $variable5 & ": " & Execute($variable5) & @CRLF

        $variable = StringStripWS($variable, 2)

        ClipPut($variable)

        MsgBox($msg_normal, "Debug", $variable)
    EndIf

EndFunc   ;==>Debug

 

Edited by gcue
Link to comment
Share on other sites

  • Moderators

gcue,

That script is full of horrible recursive calls which are undoubtedly the root of your problem - and which mean I am not even prepared to examine the script in detail. How about explaining what you want to do so that we can provide a better solution.

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

LOL thanks Melba

i am trying to do a secret santa utility. where i have a list of people in array

array[0][0]= name

array[0][1]=email

array[0][2]=block list (used so person A doesnt get their kid or spouse)

array[0][3]=assigned or not 

so i want to  through the list and assign a recipient for every person from the same list (someone cant get themselves or someone from their block list)

hope that makes sense

thank you in advance!

Link to comment
Share on other sites

  • Moderators

gcue,

Sounds like a fun project. I am visiting the in-laws next week and need a distraction - can you wait that long?

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

  • Moderators

gcue,

Quote

would it be easier to fix my horrible recursive one?

Certainly not!!!!!!

Already working on the problem - but do not hold your breath.

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

  • Moderators

gcue,

Here you go:

#include <Array.au3>

Global $aDonor[][] = [["Index", "Name",  "Family", "Email", "Recipient"], _
                        [1,     "Tom",   1,        "",      ""], _
                        [2,     "Dick",  1,        "",      ""], _
                        [3,     "Harry", 1,        "",      ""], _
                        [4,     "Peter", 2,        "",      ""], _
                        [5,     "Paul",  2,        "",      ""], _
                        [6,     "Mary",  2,        "",      ""], _
                        [7,     "Steve", 3,        "",      ""], _
                        [8,     "Dave",  4,        "",      ""], _
                        [9,     "Jane",  5,        "",      ""], _
                        [10,    "Anne",  6,        "",      ""]]

$aRecip = $aDonor
_ArrayShuffle($aRecip, 1)

;_ArrayDisplay($aRecip, "", Default, 8)

For $iDonor = 1 To UBound($aDonor) - 1

    ConsoleWrite("Checking " & $aDonor[$iDonor][1] & @CRLF)

    For $iRecip = 1 To UBound($aRecip) - 1

        ConsoleWrite(@TAB & "Against : " & $aRecip[$iRecip][1] & @CRLF)

        If $aRecip[$iRecip][0] <> $iDonor Then

            If $aRecip[$iRecip][2] <> "" And $aRecip[$iRecip][2] <> $aDonor[$iDonor][2] Then

                If $aRecip[$iRecip][4] = "" Then

                    ConsoleWrite(@TAB & @TAB & "Valid recip" & @CRLF)

                    $aRecip[$iRecip][4] = 1

                    $aDonor[$iDonor][4] = $aRecip[$iRecip][1]

                    ExitLoop

                Else

                    ConsoleWrite(@TAB & @TAB & "Already set" & @CRLF)

                EndIf

            Else

                ConsoleWrite(@TAB & @TAB & "Same family" & @CRLF)

            EndIf

        Else

            ConsoleWrite(@TAB & @TAB & "Same person" & @CRLF)

        EndIf

    Next

Next

_ArrayDisplay($aDonor, "", Default, 8)

Just make sure members of the same family get the same family number so they are blocked from each other's choices of recipient. You can comment out the ConsoleWrite lines when you are happy it works - let me know if it does not and post the array you used.

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

  • Moderators

gcue,

Glad I could help - it was a fun little problem.

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

Not exactly sure how the stars aligned in my testrun, but the last donor didn't have a recipient (which would've been themself by elimination--so I suspect that's why.)  Just wanted to mention the condition; it did not replicate on subsequent trials...but could at some point.  Perhaps you could remove the donor from the recipient list first?

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