Book Image

Getting Started with Julia

By : Ivo Balbaert
Book Image

Getting Started with Julia

By: Ivo Balbaert

Overview of this book

Table of Contents (19 chapters)
Getting Started with Julia
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
The Rationale for Julia
Index

Defining macros


In the previous chapters, we already used macros, such as @printf in Chapter 2, Variables, Types, and Operations, and @time in Chapter 3, Functions. Macros are like functions, but instead of values, they take expressions (which can also be symbols or literals) as input arguments. When a macro is evaluated, the input expression is expanded, that is, the macro returns a modified expression. This expansion occurs at parse time when the syntax tree is being built, not when the code is actually executed.

The following highlights the difference between macros and functions when they are called or invoked:

  • Function: It takes the input values and returns the computed values at runtime

  • Macro: It takes the input expressions and returns the modified expressions at parse time

In other words, a macro is a custom program transformation. Macros are defined with the keyword as follows:

macro mname 
# code returning expression 
end

It is invoked as @mname exp1 exp2 or @mname(exp1, exp2) (the...