Functions and RETURN

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
GEOVAN
Posts: 185
Joined: 03 Mar 2022, 11:12

Functions and RETURN

Post by GEOVAN » 15 May 2024, 10:59

Lets say we have a script that uses a function.
Do we need at the end of the function to add a RETURN, or since it is a function, it automatically return without need to add a return?
If we add a return is wrong or is better, please?

Example:

Code: Select all

F3::   ; press F3 to load screenshot to clipboard
GDI_CaptureScreenToClipboard(0, 0, A_ScreenWidth, A_ScreenHeight)
Return

; Here is the FUNCTION:
GDI_CaptureScreenToClipboard( X, Y, W, H) {
   Static SizeOfDIB := A_PtrSize = 8 ? 104 : 84 ; DIBSECTION
   Static SizeOfBM  := A_PtrSize = 8 ?  32 : 24 ; BITMAP
   Static SizeOfBIH := 40                       ; BITMAPINFOHEADER
   Static OffBits   := A_PtrSize = 8 ?  24 : 20 ; BITMAP -> bmBits
   Static OffSize   := A_PtrSize = 8 ?  52 : 44 ; BITMAPINFOHEADER -> biSizeImage
   Local
   hDC := DllCall("GetDC", "Ptr", 0, "UPtr")
   cDC := DllCall("CreateCompatibleDC", "Ptr", hDC, "UPtr")
   hBM := DllCall("CreateCompatibleBitmap", "Ptr", hDC, "Int", W, "Int", H, "UPtr")
   hBM := DllCall("CopyImage", "Ptr", hBM, "UInt", 0, "Int", 0, "Int", 0, "UInt", 0x2008, "UPtr")
   oBM := DllCall("SelectObject", "Ptr", cDC, "Ptr", hBM, "UPtr")
   DllCall("BitBlt", "Ptr", cDC, "Int", 0, "Int", 0, "Int", W, "Int", H
                   , "Ptr", hDC, "Int", X, "Int", Y, "UInt", 0x40CC0020)
   DllCall("SelectObject", "Ptr", cDC, "Ptr", oBM)
   DllCall("DeleteDC", "Ptr", tDC)
   DllCall("ReleaseDC", "Ptr", 0, "Ptr", hDC)
   VarSetCapacity(DIB, SizeOfDIB, 0) ; DIBSECTION
   DllCall("GetObject", "Ptr", hBM, "Int", SizeOfDIB, "Ptr", &DIB)
   Size := NumGet(DIB, OffSize, "UInt")
   Bits := NumGet(DIB, OffBits, "UPtr")
   hDIB := DllCall("GlobalAlloc", "UInt", 2, "UInt", SizeOfBIH + Size, "UPtr")
   pDIB := DllCall("GlobalLock", "Ptr", hDIB, "UPtr")
   DllCall("RtlMoveMemory", "Ptr", pDIB, "Ptr", &DIB + SizeOfBM, "UInt", SizeOfBIH)
   DllCall("RtlMoveMemory", "Ptr", pDIB + SizeOfBIH, "Ptr", Bits, "UInt", Size)
   DllCall("GlobalUnlock", "Ptr", hDIB)
   DllCall("DeleteObject", "Ptr", hBM)
   DllCall("OpenClipboard", "Ptr", 0)
   DllCall("EmptyClipboard")
   DllCall("SetClipboardData", "UInt", 8, "Ptr", hDIB)
   DllCall("CloseClipboard")
}

So, the correct is what?

(a) To add a RETURN at the end of the function like following:?

Code: Select all

   DllCall("CloseClipboard")
RETURN
}

(b) To add a RETURN at the end of the function like following:?

Code: Select all

   DllCall("CloseClipboard")
}
RETURN

(c) To NOT add a RETURN at the end of the function like following:?

Code: Select all

DllCall("CloseClipboard")
}

User avatar
CoffeeChaton
Posts: 35
Joined: 11 May 2024, 10:50

Re: Functions and RETURN

Post by CoffeeChaton » 15 May 2024, 11:04

You can use (A) or (C)

RussF
Posts: 1298
Joined: 05 Aug 2021, 06:36

Re: Functions and RETURN

Post by RussF » 15 May 2024, 12:09

A Return statement is not required for a function unless you are returning a value to the caller, for example:

Code: Select all

#Requires AutoHotkey v1.1.33+

MsgBox % Sum(5,9)
Exitapp

Sum(a,b) {
    c := a + b
    Return % c
}
or

Code: Select all

#Requires AutoHotkey v1.1.33+

MsgBox % Sum(5,9)
Exitapp

Sum(a,b) {
    Return (a + b)
}
Russ

GEOVAN
Posts: 185
Joined: 03 Mar 2022, 11:12

Re: Functions and RETURN

Post by GEOVAN » 15 May 2024, 13:26

RussF wrote:
15 May 2024, 12:09
A Return statement is not required for a function unless you are returning a value to the caller, for example:

Code: Select all

#Requires AutoHotkey v1.1.33+

MsgBox % Sum(5,9)
Exitapp

Sum(a,b) {
    c := a + b
    Return % c
}
or

Code: Select all

#Requires AutoHotkey v1.1.33+

MsgBox % Sum(5,9)
Exitapp

Sum(a,b) {
    Return (a + b)
}
Russ
Thank you, so in a case we will NOT returning a value to the caller, then we can use (a) or (c) as CoffeeChaton says ?
Or is better to not use Return as (c) ?

RussF
Posts: 1298
Joined: 05 Aug 2021, 06:36

Re: Functions and RETURN

Post by RussF » 15 May 2024, 14:03

One way is not any "better" than the other. It is not necessary to have a Return in AHK. That said, MANY other languages DO require a Return at the end of a function and I usually use one out of years of habit. It also helps me to know I am definitely at the end of the function.

Just as a side note - you can technically use a Return from anywhere within the function - many times in fact - from within IF-THEN clauses, Loops, etc. For example,

Code: Select all

MyFunc(input) {
	If (input < 10)
		Return % "Less than"
	Else
		Return % "Greater then"
}
is perfectly legal syntactically and many would say preferred. However, I was personally taught that a good function has one entry point and one (and only one) exit. It makes it easier to debug large functions that may have a stray Return buried somewhere in the code. Using my preferred method, the function above would be:

Code: Select all

MyFunc(input) {
	If (input < 10)
		Result := "Less than"
	Else
		Result := "Greater then"
Return % Result
}
Of course, you could really shorten it like this:

Code: Select all

MyFunc(input) {
Return (input < 10 ? "Less than" : "Greater then")
}
I'm sure there will be many differences of opinion on this, so use whatever is easiest for you to remember and more importantly, to debug, as long as it is syntactically correct.

Russ

GEOVAN
Posts: 185
Joined: 03 Mar 2022, 11:12

Re: Functions and RETURN

Post by GEOVAN » 16 May 2024, 14:06

Thank you so much!
You help me understand Functions and RETURN.

Just please let me know a minor but significant clarification:
Speaking for cases of functions where NO returning a value to the caller, as for general example, below:

Code: Select all

; Here is the FUNCTION:
Function_Example( X, Y, W, H) {
   ....some code.....
}
This function "opens" with " { " .......... and closes with....... " } "

Now, if we add RETURN before the last " } " of the function, is there a possibility to leave any traces of the function running in the background causing memory leak or similar problems, please?
(because the function never will come to its normal end by reaching its last " } " ) - ?

RussF
Posts: 1298
Joined: 05 Aug 2021, 06:36

Re: Functions and RETURN

Post by RussF » 16 May 2024, 14:41

RussF wrote: ...is there a possibility to leave any traces of the function running in the background causing memory leak or similar problems, please?
No, the closing brace } IS the end of the function. As I said, in AHK, the Return statement is not necessary unless you are returning a value. Whether of not you have a Return makes absolutely no difference to the way the code operates as long as you have the closing brace.

Russ

GEOVAN
Posts: 185
Joined: 03 Mar 2022, 11:12

Re: Functions and RETURN

Post by GEOVAN » 17 May 2024, 12:32

Thank you very very much!!!

Post Reply

Return to “Ask for Help (v1)”