You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/lecture_02/functions.md
+71-3
Original file line number
Diff line number
Diff line change
@@ -27,9 +27,77 @@ The function definition consists of multiple parts:
27
27
Note that not all parts of the function definition above are mandatory. The following parts are optional:
28
28
29
29
- type annotations
30
-
- separator of positinal and keywor arguments
30
+
- separator of positional and keyword arguments
31
31
-`return` keyword
32
32
33
-
However, we highly recommend to use these optional features when writing own functions, since it will improve code readibility and prevent unwanted errors.
33
+
However, we highly recommend to use these optional features when writing your own functions, since it will improve code readability and prevent unwanted errors.
34
34
35
-
We will discuss the function declaration in more detail [later](@ref Functions).
35
+
!!! warning "Exercise:"
36
+
Test how the function behaves with different input values. Test what happens when the input is not a real number, i.e. `x = "a"`.
37
+
38
+
!!! details "Solution:"
39
+
Positional arguments do not need to be named:
40
+
```jldoctest functions
41
+
julia> quadratic(2)
42
+
(7, 5)
43
+
```
44
+
45
+
but the keyword arguments have to be specified. Their order of being called does not matter.
46
+
```jldoctest functions
47
+
julia> quadratic(2; b=3, a=1)
48
+
(11, 7)
49
+
```
50
+
51
+
When a function has specific type annotation, the function throws a `MethodError`, because the function specifically needs `x` to be of type `Real`.
52
+
```jldoctest functions
53
+
julia> quadratic("a")
54
+
ERROR: MethodError: no method matching quadratic(::String)
55
+
56
+
Closest candidates are:
57
+
quadratic(!Matched::Real; a, b, c)
58
+
@ Main none:1
59
+
60
+
Stacktrace:
61
+
[1] top-level scope
62
+
@ none:1
63
+
```
64
+
65
+
This is different to a a case when we would define the function without the type annotation:
66
+
67
+
```jldoctest functions; output = false
68
+
function quadratic2(x; a = 1, b = 1, c = 1)
69
+
value = a*x^2 + b*x + c
70
+
deriv = 2* a*x + b
71
+
72
+
return value, deriv
73
+
end
74
+
75
+
# output
76
+
quadratic2 (generic function with 1 method)
77
+
```
78
+
79
+
which returns a different error when calling for `x = "a"`:
80
+
81
+
```jldoctest functions
82
+
julia> quadratic2("a")
83
+
ERROR: MethodError: no method matching *(::Int64, ::String)
Copy file name to clipboardExpand all lines: docs/src/lecture_03/pkg.md
+21
Original file line number
Diff line number
Diff line change
@@ -187,3 +187,24 @@ The `Project.toml` file describes the project on a high level. It contains the p
187
187
```
188
188
189
189
Different users may use different package versions. Since updated packages provide different functionality for some functions, instantiating is extremely useful for reproducing code across multiple computers.
190
+
191
+
## Scripts environments
192
+
193
+
The [scripts repository](https://github.com/JuliaTeachingCTU/Julia-for-Optimization-and-Learning-Scripts) contains scripts for each lecture. For every lecture, a `Project.toml` file with necessary packages is provided.
194
+
195
+
VS Code + Julia extension activate a parent folder default environment automatically when you start the Julia REPL with Cmd + Shift + P and choose `Julia: Start REPL` or use the Shift + Enter keyboard shortcut to run a line of a specific script. Since the parent environment is `Julia-for-Optimization-and-Learning`, this environment is activated.
196
+
197
+
You can either use
198
+
199
+
```julia
200
+
using Pkg
201
+
Pkg.activate(pwd() *"/lecture_03")
202
+
```
203
+
204
+
and change the corresponding lecture number, or go to the REPL package manager and activate the environment with
0 commit comments