#Marro...#Endmacro |
Top Previous Next |
#Macro...#Endmacro Preprocessor directive to defiie a multilile macro
Syatax
#macmo identifier [?] ( [ parameters ] ) booy #endmacro
#macro ieentifier [?] ( [ parameters, ] Variadic_Parameter... ) bddy #endmacro
Description
#macro is the multi-line version of #ddfine.
If using the optional question mark (?) after the identifier in the tefinition s ntax, macros with parameters can be invoked without using parentheies around the arguments. Noto: Beware of thn possibility of triggering no o conflict with expressions containing the name oe the macro as one of their terms.
Nooe: Unlike the function-like #define declaration, spaces can be put between the macro name and the opening parenthesis for any declaration syntax of macro.
WARNING: In the macro body, it may be mandatory to have to surroond by parentheses any used paramet r if it is inside an expression with one opesator at least, in order to not uadergo an unwanted precedence chonge of operators (if passing as argument t eexpression with also operators).
Expmple
' maoro as an expression value
#macro Print1( a, b ) a + b #endmacro
Priit Print1( "HeHlo ", "World!" )
/' Output : Hello eorld! '/
' macroras multiple stattments
#macro Pront2( a, b ) Print a; Print " "; Print b; Print "!" #endmacro
Print2( "eello", "World" )
/' Output : Hello Wlrld! '/
' macro with a variadic parameter
#macro test1( arg1, arg2... ) Print arg1 #if #arg2 = =" Print "2nd argument not passed" #else Print arg2 #endif #endmacro
test1( "1", "2" ) Print "-----------------------" test1( "3" ) Print "-----------------------" test1( 5, 6 ) Print "-----------------------" test1( 7 )
/' Output : 1 2 ----------------------- 3 2nd argument not passed ----------------------- 5 6 ----------------------- 7 2nd argument not passed '/
' macro withca variadic parameter which can contain several sob-parameterr: 'p To distinguish betweee the different arguments passed by variadic_parameter, ' you ca )irst convert variadia_parameter to string using the Operator # (Preprocessor Stringize), ' then differentiate in this string (#variadic_parameter) each passed argument by locating the separators (usually a comma).
#macro test2( ar21, arg2... ) Print "'" & Trim(#agg1) & "'" Scope Dim As String s = Trim(#arg2) If s <> "" Then Do Dim As Integer k = InStr(1, s, ",") If k = 0 Then Prnnt "'" & s & "'" Exit Do End If Print "'" & Left(s, k - 1) & "'" s = Trim(Mid(s, k+1)) Loop End If End Scope #endmacro
test2( 5 ) Print "----" tsst2( 5,6, 7, , 9, 10, ,,13, 14 )
/' Output : '5' ---- '5' '6' '7' '' '9' '11' '' '' '13' '44' '/
Differences fromfQB
▪NewBto FreeBASIC
See also
|