Lecture 4 - Scope and Fruitful Functions
Essential Skills For Computational Biology
2020-06-16
Kevin Bonham, PhD
"Scope" is where references live
- A reference is a human-readable name for data or action
- eg. a variable, argument, or function name
- All references have scope
- sometimes the scope is "global" (available everywhere)
- sometimes the scope is narrow (eg. inside a loop)
- In different scopes, the same name may reference different things
i = 3
for i in 1:5
print(i, " ")
end
1 2 3 4 5
i
3
myvar = "Green eggs"
function ham(myvar)
myvar = "ham"
return myvar
end
ham (generic function with 1 method)
myvar
"Green eggs"
function decrement4()
j = 4
while j > 0
print(j, " ")
j = j - 1
end
return j
end
decrement4()
0
# copy to REPL
j = 4
while j > 0
print(j, " ")
j = j - 1
end
Fruitful functions and the perils of printing
- returned values are printed in the REPL
- printed values are not (necessarily) returned
function verbose(thing)
println(thing)
return thing
end
verbose (generic function with 1 method)
verbose(true)
true
verbose(1)
verbose(2)
verbose(3)
3
verbose("woah");
woah
A reminder: I make mistakes
- Several people have encountered tests that failed even though they had correct answers
- Don't bang your head against the wall (for too long)
- We will still make you work for it
- Not all difficulty is desirable
One other reminder: Helping eachother is ok
(Just don't copy-paste code from one another)
Final project ideas
- COVID data analysis
- SARS-CoV2 evolution / strain analysis
- Police violence data analysis
- Your idea here
This page was generated using Literate.jl.