Jump to content

Passing Function as Parameters


PNB
 Share

Recommended Posts

Hi,

My question is regarding how to pass function as parameter

Suppose I have 3 Functions like this

Func A()

EndFunc

Func B($sabc,$pqr,$xyz)

EndFunc

Func C($abc,$xyz,$test,$text)

EndFunc

I have to call these function from only function, all function should be call only from one call function.Just pass function name to that function no matter function has multiple paramaters.Without any select,switch case,if, or calling one by one function like this

Call("A")

Call("B","a","b","c")

Call("B","a","b","c","d")

 

 

 

 

 

Link to comment
Share on other sites

  • Moderators

PNB,

Welcome to the AutoIt forum.

But please pay attention to where you post - the "Examples" section where you started this thread is clearly marked: "Do not post general support questions here".  I have moved it for you, but would ask you to be more careful in future.

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

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

Func A()
  ConsoleWrite( "A()" & @CRLF )
EndFunc

Func B($sabc,$pqr,$xyz)
  ConsoleWrite( "B(" & $sabc & "," & $pqr & "," & $xyz & ")" & @CRLF )
EndFunc

Func C($abc,$xyz,$test,$text)
  ConsoleWrite( "C(" & $abc & "," & $xyz & "," & $test & "," & $text & ")" & @CRLF )
EndFunc

Func CallFunc( $f, $p1 = Default, $p2 = Default, $p3 = Default, $p4 = Default )
  Return FuncName($f) = "A" ? $f() : FuncName($f) = "B" ? $f($p1,$p2,$p3) : $f($p1,$p2,$p3,$p4)
EndFunc

Example()

Func Example()
  CallFunc( A )
  CallFunc( B, 1, 2, 3 )
  CallFunc( C, 1, 2, 3, 4 )
EndFunc

 

Link to comment
Share on other sites

3 hours ago, PNB said:

I have to call these function from only function, all function should be call only from one call function.Just pass function name to that function no matter function has multiple paramaters.

It's the part in bold which keeps you from doing so. Well, unless all parameters are optional (have default values).
I see that a "workaround" was just posted but it merely illustrates my point about optional parameters.

BTW you rarely need to use Call("A"), just do A() to call it. Also functions are now first-class citizens: a variable can be assigned a function and be passed around.

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

As explained in the help of the Call() function you can pass a variable number of arguments to a function using the Call() statement and an 1D array as second argument. That is, you have to stack all your arguments in an 1D array, where the first element  (element[0]) must contains the string "CallArgArray" and the following elements contains your arguments (also arrays are allowed as parameters), the receiving function must accept as many arguments as are stacked in the array.
So is up to you to prepare the proper array related to the number of arguments accepted by the function you have to call.

Call("A")

Dim $aArguments[4] = ["CallArgArray", "First parameter", "Second", 3] ; pass 3 arguments
Call("B", $aArguments)

Dim $aArguments[5] = ["CallArgArray", 1, "Second", 3, 4] ; pass 4 arguments
CallMyFunction("C", $aArguments) ; use your custom function to make the Call

; this function call another function and pass as many arguments as contained in the array (if any)
Func CallMyFunction($FunctionName, $aArguments = "")
    Call($FunctionName, $aArguments)
EndFunc   ;==>CallMyFunction


Func A()
    ConsoleWrite("Function A() has no arguments" & @CRLF)
EndFunc   ;==>A

Func B($sabc, $pqr, $xyz)
    ConsoleWrite("Received: " & $sabc & @TAB & $pqr & @TAB & $xyz & @CRLF)
EndFunc   ;==>B

Func C($abc, $xyz, $test, $text = "")
    ConsoleWrite("Received: " & @NumParams & " arguments." & @CRLF)
EndFunc   ;==>C

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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