# Julia Notes Day1 -- functions and operators

## Functions

```julia
∑(n) = sum(1:n)

∑(3) # Output: 6
∑(100) # Output: 5050
```

> [Unicode](https://www.fileformat.info/info/unicode/category/index.htm) variable and function names `∑` are allowed.
> 
> Julia functions can be defined using compact "assignment form" syntax.
> 
> The value returned by a function is the value of the last expression evaluated, the `return` keyword can be omited.

```julia
∏(n) = (p=1; for i in 1:n; p*=i; end; p) # the ; chain syntax

∏(4) # Output: 24
∏(10) # Output: 3628800
```

> Sometimes it is convenient to have a single expression which evaluates several subexpressions in order, returning the value of the last subexpression as its value.
> 
> There are two Julia constructs that accomplish this: `begin` blocks and `;` chains.

```julia
function foo(a,b)
    a+b, a*b #return multiple values by returning a tuple
end

foo(2,3) # Output: (5, 6)
```

> Julia has a built-in data structure called a *tuple* that is closely related to function arguments and return values.

## Operators

In Julia, operators and symbols often have specific meanings or uses that are integral to the language's syntax and functionality. Below is a list of some of these symbols, along with brief explanations and code examples to illustrate their use:

### Arithmetic Operators

* `+` (Addition)
    
* `-` (Subtraction)
    
* `*` (Multiplication)
    
* `/` (Division)
    
* `÷` (integer divide)
    
* `^` (Power)
    
* `%` (Remainder)
    

### Comparison Operators

* `==` (Equal to)
    
* `!=` or `≠` (Not equal to)
    
* `<` (Less than)
    
* `>` (Greater than)
    
* `<=` or `≤` (Less than or equal to)
    
* `>=` or `≥` (Greater than or equal to)
    

### Logical Operators

* `!` (Not)
    
* `&&` (And)
    
* `||` (Or)
    

### Bitwise Operators

* `&` (And)
    
* `|` (Or)
    
* `~` (Not)
    
* `<<` (logical/arithmetic shift left)
    
* `>>` (arithmetic shift right)
    
* `>>>` (logical shift right)
    
* `⊻` bitwise xor (exclusive or)
    
* `⊼` bitwise nand (not and)
    
* `⊽` bitwise nor (not or)
    

### [Spe](https://en.wikipedia.org/wiki/Logical_shift)[cia](https://en.wikipedia.org/wiki/Arithmetic_shift)l Symbols

* `:` (Colon, used for ranges)
    
* `;` (Semicolon, used to separate expressions)
    
* `=>` (Pair, used in dictionaries)
    
* `[]` (Brackets, used for indexing or array literals)
    
* `{}` (Curly braces, used for comprehension or generator expressions)
    
* `()` (Parentheses, used for grouping or function calls)
    
* `|>` (Pipe, used for function chaining)
    
* `.` (Dot, used for broadcasting)
    

```julia
# Range
1:5  # 1:5

# Pair and Dictionary
dict = Dict("a" => 1, "b" => 2)

# Pipe
sqrt(25) |> println  # 5.0

# Broadcasting
[1, 2, 3] .+ 1  # [2, 3, 4]
1:3 .|> (x -> x^2)  # [1, 4, 9]
```

### Division and Related Operations

* `\` (Backslash, used for solving linear equations, `A \ b` solves `Ax = b`)
    
* `//` (Rational, creates a rational number)
    

```julia
# Solving linear equations
A = [1 2; 3 4]
b = [5, 11]
A \ b  # [1.0, 2.0]

# Rational number
3 // 4  # 3//4
```
