Skip to content

Commit 3794c85

Browse files
committed
add funcs and loops
1 parent 9480726 commit 3794c85

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

tutorial/tutorial_1.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,68 @@ https://github.com/lcompilers/lpython/issues.
125125
```
126126

127127
Similarly, please note that the default type of float is `f64`.
128+
129+
### Functions
130+
131+
Functions in LPython are also strictly typed, meaning, each function
132+
argument should have a type. The function also needs to have a return
133+
type (if it returns a value).
134+
135+
Consider the following example of how function looks
136+
```
137+
% cat func1.py
138+
from lpython import i32
139+
140+
def function_void_example():
141+
print("This function has no args and return type.")
142+
143+
def function_with_args_and_ret(a: i32) -> i32:
144+
# Function having i32 as return type
145+
return a + 1
146+
147+
def function_with_args(a: i32, b: str):
148+
print("Hello", b)
149+
c: i32 = function_with_args_and_ret(a)
150+
print("You just got", c)
151+
152+
function_void_example()
153+
function_with_args(2, "lpython")
154+
```
155+
156+
Running this we get
157+
158+
```
159+
% lpython func1.py
160+
This function has no args and return type.
161+
Hello lpython
162+
You just got 3
163+
```
164+
165+
### Loops
166+
167+
LPython provide loop in the same way as in python. The only extra
168+
condition is that the loop variable must be a typed and declared
169+
beforehand. Take a look at the `for` loop example to understand it better
170+
171+
```
172+
% cat loop1.py
173+
from lpython import i32, i64
174+
175+
def loop_for():
176+
i: i32
177+
res: i64 = i64(0)
178+
for i in range(0, 10000):
179+
res += i64(i)
180+
print("for loop result:", res)
181+
182+
def loop_while():
183+
i: i32 = 1
184+
res: i64 = i64(0)
185+
while i < 10000:
186+
res += i64(i)
187+
i += 1
188+
print("while loop result:", res)
189+
190+
loop_for()
191+
loop_while()
192+
```

0 commit comments

Comments
 (0)