Jump to content

within the parentheses of a function | func example()


Go to solution Solved by Luke94,

Recommended Posts

 i utilize functions often but have yet to add anything within the parentheses.

func example() ; <==

endfunc

can someone politely walk me through "what happens within the parentheses?"
the help directory is somewhat confusing to me at times..

 

random script for example:

#Include <File.au3>
#Include <Array.au3>

$Debug = 0

Func EmptyFolder($FolderToDelete) ; <=== *here*
    $AllFiles =_FileListToArray($FolderToDelete, "*", 0)
    If $Debug Then MsgBox(0, "", $FolderToDelete)
    If IsArray($AllFiles) Then
        If $Debug Then
            _ArrayDisplay($AllFiles, $FolderToDelete)
        EndIf
        For $i = 1 To $AllFiles[0]
            FileDelete($FolderToDelete & "\" & $AllFiles[$i])
            DirRemove($FolderToDelete & "\" & $AllFiles[$i], 1)
        Next
    EndIf
EndFunc

EmptyFolder(@ScriptDir & "\Example")

is the variable being declared / read?
 

Link to post
Share on other sites

Have you read this?

Quote

The ByRef keyword indicates that the parameter should be treated as a reference to the original. By default the parameter is copied into a new variable but ByRef links the new variable to the original. Note that not only a named variable can be passed for a ByRef parameter - unnamed temporary variables, such as function return values, may be passed as ByRef parameters as well. However, a literal cannot be passed to a ByRef parameter. ByRef should be used when passing large amounts of data (such as the contents of a file) where copying all the data would impose a significant performance penalty. Another advantage is that passing a parameter ByRef when the function is intended to change the content of the parameter removes any requirement to Return the changed value as the original is directly affected.

 

Link to post
Share on other sites
  • Solution

No, the information within the parentheses (parameter) is passed by the user which is then copied into a new variable for use within the function - any changes made to the variable will be Local. See https://www.autoitscript.com/wiki/Best_coding_practices#Scopes_of_Variables

Declaring ByRef links the parameter to the original variable meaning any changes made to the variable within the function will be made to the original variable.

This feels like a bad example but have a look anyway:

Global $g_sText = 'iamkoshr' ; This is the variable we are passing to the functions

Function1($g_sText)
ConsoleWrite('Global Variable: ' & $g_sText & @CRLF) ; Here the output WILL be changed even though the parameter was changed within Function1
Function2($g_sText)
ConsoleWrite('Global Variable: ' & $g_sText & @CRLF) ; Here the output will NOT be changed because the ByRef keyword was used

Func Function1($sText)
    ConsoleWrite('Parameter: ' & $sText & @CRLF) ; Output the parameter passed by the user
    $sText = 'CHANGED!' ; Change the parameter
EndFunc

Func Function2(ByRef $sText)
    ConsoleWrite('Parameter: ' & $sText & @CRLF) ; Output the parameter passed by the user
    $sText = 'BYREF CHANGED!' ; Change the parameter
EndFunc

 

Edited by Luke94
Link to post
Share on other sites
#Include <File.au3>
#Include <Array.au3>

$Debug = 0

Func EmptyFolder($FolderToDelete) ; <=== *here* -- This parameter is then stored as a new variable and used with the function
    $AllFiles =_FileListToArray($FolderToDelete, "*", 0) ; Used here
    If $Debug Then MsgBox(0, "", $FolderToDelete) ; Used here
    If IsArray($AllFiles) Then
        If $Debug Then
            _ArrayDisplay($AllFiles, $FolderToDelete) ; Used here
        EndIf
        For $i = 1 To $AllFiles[0]
            FileDelete($FolderToDelete & "\" & $AllFiles[$i]) ; Used here
            DirRemove($FolderToDelete & "\" & $AllFiles[$i], 1) ; Used here
        Next
    EndIf
EndFunc

EmptyFolder(@ScriptDir & "\Example") ; Here you are calling the EmptyFolder function with the parameter @ScriptDir & "\Example"

; It's the same as doing:

; Global $sParam = @ScriptDir & "\Example"
; EmptyFolder($sParam)

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...