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
So far, we did not show how to define functions. However, the above example should show the basic syntax for defining functions. The `return` keyword specifies the function output. In this case, the function returns nothing since we only want to compare numbers. If we need to define a function that returns more than one variable, the following syntax is used.
44
45
45
46
```julia
46
47
return x, y, z
47
48
```
48
49
49
50
Here `x`, `y`, and `z` are some variables. We will discuss the function declaration in more detail in the [next lesson](@ref Functions).
Write the `fact(n)` function that computes the factorial of `n`. Use the following function declaration:
162
165
163
166
```julia
@@ -169,6 +172,7 @@ end
169
172
Make sure that the input argument is a non-negative integer. For negative input arguments and for arguments that can not be represented as an integer, the function should throw an error.
170
173
171
174
**Hint:** use recursion, the `isinteger` function and the `error` function. The or operator is written by `|`.
175
+
172
176
```@raw html
173
177
</div></div>
174
178
<details class = "solution-body">
@@ -278,6 +282,7 @@ julia> f(1) || println(2) # both expressions are evaluated
278
282
<header class="admonition-header">Short-circuit evaluation vs. bitwise boolean operators</header>
279
283
<div class="admonition-body">
280
284
```
285
+
281
286
Boolean operations without short-circuit evaluation can be done with the bitwise boolean operators `&` and `|` introduced in [previous lecture](@ref Numeric-comparison). These are normal functions, which happen to support infix operator syntax, but always evaluate their arguments.
Rewrite the factorial function from the exercises above. Use the short-circuit evaluation to check if the given number is a non-negative integer and the ternary operator for recursion.
In the previous exercise, we rewrote pseudocode to an actual Julia code. This exercise will improve the central part of the code: the inner loop. Write a function which replaces the inner loop in the code from the exercise above. Use the following function definition
Try different values of variable `c` to create different plots. For inspiration, check the Wikipedia page about [Julia set](https://en.wikipedia.org/wiki/Julia_set).
Copy file name to clipboardExpand all lines: docs/src/lecture_02/loops.md
+14Lines changed: 14 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -51,6 +51,7 @@ i = 5
51
51
<header class="admonition-header">An alternative notation for <code>for</code> loops</header>
52
52
<div class="admonition-body">
53
53
```
54
+
54
55
There are two alternative notations for the `for` loop. It is possible to use the `=` or `∈` symbol instead of the `in` keyword.
55
56
56
57
```jldoctest
@@ -65,6 +66,7 @@ i = 5
65
66
```
66
67
67
68
However, it is better to use the `in` keyword to improve code readability. Regardless of which notation is used, it is essential to be consistent and use the same notation in all `for` loops.
69
+
68
70
```@raw html
69
71
</div></div>
70
72
```
@@ -102,9 +104,11 @@ Hi, my name is Bob and I am 23 old.
Rewrite the code from the exercise above. Use a combination of the `while` loop and the keyword `continue` to print all integers between `1` and `100` divisible by both `3` and `7`. In the declaration of the `while` loop use the `true` value instead of a condition. Use the `break` keyword and a proper condition to terminate the loop.
210
+
205
211
```@raw html
206
212
</div></div>
207
213
<details class = "solution-body">
@@ -295,6 +301,7 @@ There are other limitations of the shorter syntax, such as the impossibility to
Use the list comprehension to create a vector of all integers from `1` to `100` divisible by `3` and `7` simultaneously. What is the sum of all these integers?
0 commit comments