Skip to content

Commit

Permalink
Explain more pointer stuff in tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Feb 20, 2024
1 parent 5bbbe2d commit e111167
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,7 @@ def main() -> int:
return 0
```

Instead of pointers, you could also use an `int[2]` array to return the two values.
However, **this doesn't mean that you don't need to understand pointers**,
as they have many other uses in Jou.
Instead of pointers, you could also use an `int[2]` array to return the two values:

```python
import "stdlib/io.jou"
Expand All @@ -331,6 +329,24 @@ def main() -> int:
return 0
```

However, **this doesn't mean that you don't need to understand pointers**,
as they have many other uses in Jou.
Pointers are used for strings, arrays that are not fixed-size,
instances of most classes, and so on.

Basically, you need pointers whenever you want to use a large object in multiple places
without making several copies of it.
Instead, you just make one object and point to it from many places.
This is probably what you expect if you have mostly used high-level languages,
like Python or JavaScript.
In fact, in Python, **all** objects are passed around as pointers.

You don't need a pointer when the value is small enough to be passed around as is.
For example, if you want to make a function takes two `int`s and prints them,
just make a function that takes two `int`s.
On the other hand, if your function takes an array of 1000000 `int`s and prints them,
you should use a pointer.


## Undefined Behavior (UB)

Expand Down

0 comments on commit e111167

Please sign in to comment.