Repository navigation
Macro Endmacro
Home › Preprocessor › #Macro...#Endmacro
Preprocessor directive to define a multiline macro
#macro identifier [?] ( [ parameters ] )
body
#endmacro
#macro identifier [?] ( [ parameters, ] Variadic_Parameter... )
body
#endmacro#macro is the multi-line version of #define.
If using the optional question mark (?) after the identifier in the definition syntax, macros with parameters can be invoked without using parentheses around the arguments.
Note: Beware of the possibility of triggering so a conflict with expressions containing the name of the macro as one of their terms.
Note: 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 surround by parentheses any used parameter if it is inside an expression with one operator at least, in order to not undergo an unwanted precedence change of operators (if passing as argument an expression with also operators).
- New to FreeBASIC
' macro as an expression value
#macro Print1( a, b )
a + b
#endmacro
Print Print1( "Hello ", "World!" )
/' Output :
Hello World!
'/' macro as multiple statements
#macro Print2( a, b )
Print a;
Print " ";
Print b;
Print "!"
#endmacro
Print2( "Hello", "World" )
/' Output :
Hello World!
'/' 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 with a variadic parameter which can contain several sub-parameters:
' To distinguish between the different arguments passed by variadic_parameter,
' you can first convert variadic_parameter to a 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( arg1, arg2... )
Print "'" & Trim(#arg1) & "'"
Scope
Dim As String s = Trim(#arg2)
If s <> "" Then
Do
Dim As Integer k = InStr(1, s, ",")
If k = 0 Then
Print "'" & 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 "----"
test2( 5,6, 7, , 9, 10, ,,13, 14 )
/' Output :
'5'
----
'5'
'6'
'7'
''
'9'
'10'
''
''
'13'
'14'
'/Category: Preprocessor
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing