# Printing names and values of a number of variables

**URL:** <https://discourse.julialang.org/t/printing-names-and-values-of-a-number-of-variables/47451>\
**Category:** New to Julia\
**Tags:** macros\
**Created:** [September 28, 2020, 11:51pm UTC](https://discourse.julialang.org/t/printing-names-and-values-of-a-number-of-variables/47451 "2020-09-28T23:51:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cnf](https://avatars.discourse-cdn.com/v4/letter/c/b38774/32.png) [@cnf](https://discourse.julialang.org/u/cnf)\
**Post date:** [September 28, 2020, 11:51pm UTC](https://discourse.julialang.org/t/printing-names-and-values-of-a-number-of-variables/47451/1 "2020-09-28T23:51:33Z")

</div>

I am looking for a julia function that can print both the  
names and the values of a sequence of variables, as per the following example:  
#Define a number of variables (could be any number)  
s1 = 123  
s2 = " s g ny ny ny uuy nhneejn"  
s3 = 123.4567890  
#Print their names and values using a custom  
#julia function my\_display(), taking a  
#variable number of arguments, as follows.  
my\_display(s1,s2,s3)  
s1 = 123  
s2 = " s g ny ny ny uuy nhneejn"  
s3 = 123.4567890

I have found a primitive way of doing this as follows.  
my\_display([:s1,s1,:s2,s2,:s3,s3])

Any other solutions that are more elegant and  
general would be appreciated.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [September 28, 2020, 11:57pm UTC](https://discourse.julialang.org/t/printing-names-and-values-of-a-number-of-variables/47451/2 "2020-09-28T23:57:25Z")

</div>

No function can do this, but it’s a trivial example of a macro:

```julia
import Printf: @printf

macro names_values(vars...)
    lines = Any[]
    for var in vars
        push!(lines, :(@printf("%s = %s\n", $(QuoteNode(var)), $(esc(var)))))
    end
    quote
        begin
            $(lines...)
        end
    end
end

let x = 11, y = 21, z = 31
    @names_values(x, y, z)
end

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 29, 2020, 12:43am UTC](https://discourse.julialang.org/t/printing-names-and-values-of-a-number-of-variables/47451/3 "2020-09-29T00:43:55Z")

</div>

Seeing how a relatively simple macro can be written is instructive, but note that the built-in `@show` does exactly the same

```julia
julia> let x = 11, y = 21, z = 31
           @show(x, y, z)
       end;
x = 11
y = 21
z = 31

```
