You are on page 1of 8

CS 315

Homework 3
Julia Language Subprograms

Yiğit Harun
21803241
Nested Functions:
Julia has nested functions. Which are made with indentations and function keywords.
Code:
function​ ​func1​()

print​(​"First"​) ​

 ​
function​ func2​()​

print​(​" Second "​)

 ​
function​ func3​()​

println​(​
" Third "​)

end

func3​()

end

func2​()

end

 ​
func1()​

Output:

First Second Third

Local Scopes in Julia


Julia language uses lexical scoping. There are hard scopes and soft scopes. Hard scopes
which are let function scopes and macro and function construct types. Soft scops which are
for, while and try scopes. If and begin do not declare a scope.
Code:
x = ​2
function​ ​localfunc1​()

local​ x = ​1

println​(x, ​" local"​)

 ​
global​ x = 3​

end
(x, ​" global"​)
println​

 ​
localfunc1()​

(x, ​" after func "​)


println​

Output​:

2 global

2 local

3 afterfunc

As used in the function local keyword is useless because it will be a local scope if there are
no keywords at the beginning of it. However, x inside the localfunction1 with the global
keyword is global because the global keyword is used.

Code:
x = 10
let​ x = x ​# local

x = 5 ​# changes the local x

println​(​"inside let: "​


, x)

end

(​"global: "​, x)
println​

Output:

inside let: 5
gloabl: 10

The above example is for the scoping of the let function.

Soft scoping looks for a global variable named the same and changes the global variable. If
it does not exist, it declares the variable as local.

Pass by Sharing
Julia uses a method known as pass-by-sharing. It means that values are not copied when
they pass through functions. When primitive types are passed in it uses pass-by-value. For
arrays and objects are passed, it uses pass by reference.
x = ​1

function​ ​fvar​(x) ​# when passing a variable pass by value

x = 10

return​ x

end

f1var(x)

(x, ​" x "​)


println​

1​,​2​,3
arr = [​ ​ ​,4
​]

function​ ​farr​(arr) ​# when passing objects​ and arrays

#​ pass by reference

arr[​1​] = ​4

arr[2​]​ = 3

arr[2​]​ = 2

arr[2​]​ = 1​​

end

farr​(arr)

println(arr)

Output

x[4,3,2,1]

Default Parameters
Julia allows developers to use default values as function arguments. This makes it easier
for functions with multiple arguments
Code:
function​ ​default​
(x=​ 2​
1, y=​ )

println​(​"x=​$​x​ - y=$​y"​) ​

end
() ​# calling with the default parameters
default​
default​
(5​)
(​3​,​4​)
default​

Outputs:

x=1 - y=2

x=5 - y=2

x=3 - y=4

Keyword Arguments
Default parameters do have a problem. We cannot give fewer parameters to the function.
Also, we cannot change one value without changing the other in a two-parameter function
example. If we want to use different parameters we must provide the keywords for it

Code:

function​ ​keyword​args(a=​1​, b=​2​; c=​3​, d=​4​)

println​(​"a=​$​a,​ b=$​b,​ c=$​c,​ d=$​d"​)​

end

a=​20

keywordargs​()

args(a, b, c=​4​)
keyword​

keywordargs(a,​ b, d=23​) ​
 
Output:

a=1, b=2, c=3, d=4


a=20, b=2, c=4, d=4
a=20, b=2, c=11, d=23

Closures
Julia has nested functions. The closure function which is the function inside the function has
access to the variables of the parent function.
function foo()

x = ​11

println​(​" in parent x=​


$x​"​)
 ​
function​ closure​()​
​ = x + 1
x

println​(​"in child x=​$x​"​)

end

end

foo​() #still has access to x

Output:

in parent 11

in child 12 # has access

Readability / Writability
In terms of parameter passing, Julia uses pass-by-sharing. This offers a writability
improvement because the developer doesn't need to use syntax. Another improvement for
each readability and writability is keyword arguments. Keyword arguments increase the
writability and readability of the code since the developer will pass the parameter that he
needs by merely passing its keyword. This eases the readability too. Because if a function
takes too many parameters, it will be easier to read since the developer does not need to
provide every parameter. On the negative Julia’s readability decreases because it treats
differently when a primitive type is given to the function in contrast to when an array or an
object is given to the function.

Learning Strategy

To learn Julia I mostly looked at the official documentation. They were well written however
they lacked some key functionality examples that Julia offers. There was no closure and
pass by sharing examples. For those examples, I looked at Stackoverflow and some tutorials
that explained pass by sharing. Most of the tutorials about pass by sharing were about
javascript so that was not helpful. I also asked my older cousin for some help since he
graduated two years ago however it was his first time hearing about Julia. For the compiler, I
used the official compiler but it gave errors pretty commonly so I had to use the tutorialspoint
one.

References
● https://discourse.julialang.org/t/retrieve-default-values-of-keyword-arguments/19320

● https://riptutorial.com/julia-lang/example/23207/introduction-to-closures
● https://stackoverflow.com/questions/13899171/does-call-by-sharing-and-call-by-refer
ence-differ-only-while-multithreading
● https://julia-lang.programmingpedia.net/en/tutorial/5724/closures

● https://docs.julialang.org/en/v1/devdocs/functions/#Julia-Functions

● https://docs.julialang.org/en/v1/manual/variables-and-scoping/#:~:text=The%20scope
%20of%20a%20variable,referring%20to%20the%20same%20thing
● https://docs.julialang.org/en/v1/manual/functions/#man-functio​n
Compilers
● https://www.tutorialspoint.com/execute_julia_online.php

● https://julialang.org/learning/tryjulia/

You might also like