Closed lukavdplas closed 10 months ago
I started writing some stuff about begin vs functino vs let in the section about multiple expressions per cell, but maybe it fits better as a separate notebook?
# ╔═╡ 556d876c-34be-4f28-98c9-6b8d2c87f870
md"""
### Code blocks: _**function**_ vs. _**begin**_ vs. _**let**_
So how do you write cells with more than one line of code? Julia offers three good ways to group code together:
- **`function`** for code that you want to use multiple times
- **`begin`** for multiple expressions that should always run together
- **`let`** for a block of intermediate code with temporary variables
In Jupyter, multiple expressions are automatically wrapped in **`begin`**, but the other two options are also useful!
"""
# ╔═╡ f40c5336-b2f0-4a6f-b314-30a12f27b897
md"""
#### begin
> ##### begin
> **`begin`** will group expressions together, and it takes the value of its last subexpression.
>
> We use it when we want multiple expressions to always run together.
In the example below, we do two things
"""
# ╔═╡ 6b23000d-53a0-4a34-9921-eed7ec681e59
begin
# generate a temporary filename
filename = tempname() * ".txt"
# write contents to the file
write(filename, "hello!")
end
# ╔═╡ 9ac78968-f0d1-4ca8-b89c-1f4d76896fe6
read(filename, String)
# ╔═╡ 530f7d22-49a1-4933-9899-49e368f30f8a
md"""
> ##### function
> Writing functions is a way to group multiple expressions (i.e. lines of code) together into a mini-program. Note the following about functions:
> - A function always returns **one object**.[^1] This object can be given explicitly by writing `return x`, or implicitly: Julia functions always return the result of the last expression by default. So `f(x) = x+2` is the same as `f(x) = return x+2`.
> - Variables defined inside a function are _not accessible outside the function_. We say that function bodies have a **local scope**. This helps to keep your program easy to read and write: if you define a local variable, then you don't need to worry about it in the rest of the notebook.
>
> There are two other ways to group expressions together that you might have seen before: `begin` and `let`.
[^1]: Even a function like
`f(x) = return`
returns **one object**: the object `nothing` — try it out!
"""
# ╔═╡ 41f4268d-9604-475a-809f-9e38b289b79c
function create_file_with_text(contents::String)
# generate a temporary filename
filename = tempname() * ".txt"
# write contents to the file
write(filename, contents)
return filename
end
# ╔═╡ 1da3d45a-3d67-4081-a2c4-5ea1244b56d2
test_filename = create_file_with_text("fun!")
# ╔═╡ c611e25a-eda6-4f9d-b409-408f14532014
read(test_filename, String)
# ╔═╡ e7c03d59-de3c-4d84-ae8f-9d288e8a3b9a
md"""
> ##### let
> **`let`** also groups multiple expressions together into one, but variables defined inside of it are **local**: they don't affect code outside of the block. So like `begin`, it is just a block of code, but like `function`, it has a local variable scope.
>
> We use it when we want to define some local (temporary) variables to produce a complicated result, without interfering with other cells. Pluto allows only one definition per _global_ variable of the same name, but you can define _local_ variables with the same names whenever you wish!
"""
# ╔═╡ 1bc838ba-c5c7-454c-b0ed-394761ac7cfa
md"""
Using `let` for intermediate results: *(I don't have to worry that `x` is already used as a global variable)*
"""
# ╔═╡ 477de741-28a9-4b4c-a032-ae4262f65731
seconds_per_day = let
x = 60 * 60
24 * x
end
# ╔═╡ f58cba8b-d11c-4ce4-9eab-43617bb5833e
md"""
Using `let` for showing off small calculations:
"""
# ╔═╡ 504cc626-ef26-4f46-9740-fb50ab008a6c
let
vegetables = ["🥦", "🥔", "🥬"]
length(vegetables)
end
# ╔═╡ 1fa94824-e1ae-4dcf-a3ad-f71ecc223b3c
let
vegetables = ["🌽"]
length(vegetables)
end
You can paste images in github comments to get free image hosting :)
Adds a "Pluto for Jupyter users" introductory notebook!
close #22