PDA

View Full Version : call statement



TomLebowski
15-08-2010, 16:58
for next thinbasic issue ;)
I don't understand the meaning of "call" function. why you can't call up a simple function with "call" statement ?
bye, tom

Michael Hartlef
15-08-2010, 17:35
Can you please explain your issue? Call works fine for me.

ErosOlmi
15-08-2010, 19:41
CALL is mainly used when you want to call a function whose name is not know and can change at runtime.
You can compute the name of the function as a string expression and ask thinBasic to search for that function name and call it.

What is not working for you or what you was expecting from CALL?

Lionheart008
17-08-2010, 18:01
hi tom, perhaps this little "call" example can help ?


' Empty GUI script created on 08-17-2010 17:43:44 by (ThinAIR)

Uses "console"

Dim str As String

PrintL "Usual 'print' message"
str = PrintAndReturnLine("this is a teststring with " + _
Chr$(13) + Chr$(10) + "Breaking the lines")
Call PrintAndReturnLine "Line: " & Str$(str)
PrintAndReturnLine "this is working fine too :)"

Function PrintAndReturnLine (ByVal Texts As String) As Integer
PrintL Texts
Function = 1
End Function

MsgBox 0, "little 'call' test ok for you?"

WaitKey



bye, frank

TomLebowski
17-08-2010, 18:21
thanks for replies, michael, eros and frank for example :)

I was last week on that trip to understand ...


' Empty GUI script created on 08-17-2010 11:10:44 by (ThinAIR)

Uses "console"

Dim myvary As Long
Dim vary1, vary2, vary3 As Long

If myvary = 1 Then
MyCalc_A(vary1, vary2, vary3)
Else
MyCalc_B(vary1, vary2, vary3)
Else
MyCalc_C(vary1, vary2, vary3)
End If

Function MyCalc_A( ByVal x As Long, ByVal y As Long, ByVal z As Long ) As Long
x = 200
y = 150
Function = x + y
End Function

Function MyCalc_B( ByVal a As Long, ByVal b As Long, ByVal c As String ) As String
a = 100
b = 50
c = "hello summertime"

Function = c
End Function

Function MyCalc_C( ByVal a As Long, ByVal b As Long, ByVal c As Long ) As Long
a = 100
b = 50
Function = a - b
End Function

Call "MyCalc_" & IIf$(myvary = 1, "A", "B") (vary1, vary2, vary3)

PrintL myCalc_A(1,1,1)
PrintL myCalc_B(1,1,"y")
PrintL myCalc_c(1,1,1)

MsgBox 0, "test 1 ok? " + myCalc_A(1,1,1)
MsgBox 0, "test 2 'string' ok? " + myCalc_B(1,1,"y")
MsgBox 0, "test 3 ok? " + myCalc_C(1,1,1)

WaitKey

now it`s more clear to understand call statement. Have found another examples at the board and on help manual. thought it was easy to call up function only with one parameter behind command call. my mistake. sorry.

for eros:


You can compute the name of the function as a string expression and ask thinBasic to search for that function name and call it.

do you have any other example for me ?

thanks for help! , bye, tom

Michael Hartlef
17-08-2010, 18:43
Hi Tom,

http://community.thinbasic.com/index.php?topic=1598.0

here I show how to calculate the function name and call it.

Cheers
Michael

Petr Schreiber
17-08-2010, 18:46
Tom,

there are 2 examples on CALL in the help file, just write CALL to thinAir, move cursor to it and hit F1.

Here is one more example to demonstrate the use:


Dim sFunctionNameFromUser As String
Dim sFunctions As String

' -- Get names of functions in script
sFunctions = Function_Names(%FUNCTION_USER, $CRLF)

' -- Offer the user to call them
sFunctionNameFromUser = InputBox$("Please enter name of function you want to execute:"+$CRLF(2)+sFunctions, "Request", "FUNCTION_A")

If Function_Exists(sFunctionNameFromUser) = %FALSE Then
MsgBox 0, "There is no function "+sFunctionNameFromUser
Else
' -- Function name is in string, and CALL statement will execute it
' -- So it does not call function named "sFunctionNameFromUser", but the one you entered, such as Function_A, ...
Call sFunctionNameFromUser()
End If

Function Function_A()
MsgBox 0, "I am in function A, which does ... not much"
End Function

Function Function_B()
MsgBox 0, "I am in function B, as you wanted"
End Function

Function Function_C()
MsgBox 0, "I am in function C, last here :)"
End Function


And here another one:


' Use File module for handling files
uses "FILE"

function TBMain()
' Assign file name to variable, notice assignment can be done at time of declaring variable
local MyFile as string = "c:\File.txt"
local ReturnValue as long

' Functions are named Load_<fileExtension>, so we can determine
' the function name we need at run time and save some lines of code
call "Load_"+FILE_PATHSPLIT(MyFile, %Path_Ext) ( MyFile ) To ReturnValue

end function

function Load_TXT( fName as string ) as long

' Code to load TXT file here
MSGBOX 0, "Loading TXT"

end function

function Load_BMP( fName as string ) as long

' Code to load BMP file here
MSGBOX 0, "Loading BMP"

end function



Petr

Michael Clease
17-08-2010, 21:39
Here is an example using a table of routines

http://community.thinbasic.com/index.php?topic=1405.0