Understanding how to declare a function and store it inside another variable for later use

histogram(x[1000:end], norm=true, label=["Empirical distribution"])
xGrid = -4:0.1:4
pdfnormalDensity(z) = pdf(Normal(0,1),z)
pdf_normal=pdfnormalDensity.(xGrid)
plot!(xGrid, pdf_normal, xlims=(-4,4), label=["Normal pdf"])

Can someone explain me the code here? I understand that we are creating a normal distribution of mean 0 and standard deviation 1, and we are drawing a histogram and a normal graph of the axis from -4 to 4. However, my question is pdfnormalDensity(z) = pdf(Normal(0,1),z) , can someone explain z and what is z doing here, and then what happens in pdfnormalDensity(z)? Thanks

You can improve the legibility of your code by using a triple backtick fence:

```
like this
```

If you’re ever uncertain about what a function does, or what arguments you can give it, try pressing ? in a REPL, which will get you to the help-mode. Then write the name of the function, and you will see the docstrings.

In this case, you are defining a function pdfnormalDensity(z) which gives the value at z of the probability density function for a normal distribution.

2 Likes

thank you so much for the information. I saw the docstrings however, was confused what was going on, now it makes sense thanks. :slight_smile: