Skip to content

Commit 0ed373b

Browse files
authored
Merge pull request #26 from Delta456/patch-1
[WIP] Follow #25 and more progress
2 parents 6fd6884 + 7271403 commit 0ed373b

11 files changed

Lines changed: 334 additions & 91 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Discord server: https://discord.gg/d3Qk65J
1414

1515
- [Arrays](examples/arrays.md)
1616
- [Conditional Statements](examples/conditional_statements/conditional_statements.md)
17+
- [Loop](examples/loops/loops.md)
1718
- [Functions](examples/functions.md)
1819
- [Keywords](examples/keywords.md)
1920
- [Operator](examples/operator.md)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ println(ages)
1313
println(ages[1])
1414
>>> 25
1515

16-
mut github_users := ['vbrazo', 'donnisnoni95', 'Delta456']
16+
mut users := ['vbrazo', 'donnisnoni95', 'Delta456']
1717

18-
println(github_users)
18+
println(users)
1919
>> ['vbrazo', 'donnisnoni95', 'Delta456']
2020

2121
println(github_users[0])
@@ -25,7 +25,7 @@ println(github_users[0])
2525
Note: All elements must have the same type. `['vbrazo', 'donnisnoni95', 'Delta456', 0]` will not compile.
2626

2727
```bash
28-
>>> mut github_users := ['vbrazo', 'donnisnoni95', 'Delta456', 0]
28+
>>> mut users := ['vbrazo', 'donnisnoni95', 'Delta456', 0]
2929
/user/vlang/v_by_example/.vrepl_temp.v:2:43: bad array element type `int` instead of `string`
3030
```
3131

examples/arrays/functions.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Array Functions
2+
3+
## `repeat`
4+
5+
Syntax
6+
7+
```go
8+
Type[element].repeat(number int)
9+
```
10+
11+
Makes an array with the given element number of times.
12+
13+
```go
14+
foo := int[1].repeat(7)
15+
println(foo)
16+
```
17+
Output
18+
19+
```go
20+
[1, 1, 1, 1, 1, 1, 1]
21+
```
22+
23+
## insert
24+
25+
Syntax
26+
27+
```go
28+
array.insert(num int, data T)
29+
```
30+
31+
Inserts the data at a given position and shifts the elements in the array
32+
33+
```go
34+
names := ['Samuel', 'John', 'Peter']
35+
names.insert(2,'Tom')
36+
println(names)
37+
```
38+
39+
Output
40+
41+
```go
42+
['Samuel', 'John', 'Tom', 'Peter']
43+
```
44+
45+
## `delete`
46+
47+
Syntax
48+
49+
```go
50+
array.delete(element T)
51+
```
52+
53+
Deletes the element present in the array
54+
55+
```go
56+
even_numbers = [2, 4, 6, 8, 10]
57+
even_numbers.delete(8)
58+
println(even_number)
59+
```
60+
61+
Output
62+
63+
```go
64+
[2, 4, 6, 10]
65+
```
66+
67+
## `reverse`
68+
69+
Syntax
70+
71+
```go
72+
array.reverse()
73+
```
74+
75+
Reverses the array
76+
77+
```go
78+
float_num := [1.1, 1.3, 1.25, 1.4]
79+
float_num.reverse()
80+
```
81+
82+
Output
83+
84+
```go
85+
[1.4, 1.25, 1.3, 1.1]
86+
```
87+
88+
## `clone`
89+
90+
Syntax
91+
92+
```go
93+
array.clone()
94+
```
95+
96+
Clones and returns a new array
97+
98+
```go
99+
foo := [1, 2, 4, 5, 4, 6]
100+
foo1 := foo.clone()
101+
println(foo1)
102+
```
103+
104+
Output
105+
106+
```go
107+
[1, 2, 4, 5, 4, 6]
108+
```
Lines changed: 38 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Conditional Statements
22

3-
## If statement
3+
## `if` statement
44

5-
An `if` statement is a programming conditional statement that, if proved true, performs a function or displays information. Below is a general example of an if statement in V:
5+
An `if` statement is a programming conditional statement that, if proved true, executes the code given in the block. Below is a general example of an if statement in V:
66

77
```go
88
john_height := 100
@@ -15,6 +15,28 @@ if john_height < maria_height {
1515
}
1616
```
1717

18+
In the above code, `fn println()` will only execute when the condition is true else no statement would be printed.
19+
20+
## `else` statement
21+
22+
An `else` statement is a programming conditional statement in which when `if` evaluates to false then the code in else block executes.
23+
24+
```go
25+
joey_age := 12
26+
kevin_age := 15
27+
28+
if joey_age > kevin_age {
29+
println("Joey is older")
30+
} else {
31+
println("Kevin is older")
32+
}
33+
```
34+
35+
In this example the block in else will execute because the condition in `if` evaluates to false.
36+
37+
38+
## `else if` statement
39+
1840
The `if...else` statement executes two different codes depending upon whether the test expression is `true` or `false`. Sometimes, a choice has to be made from more than 2 possibilities. The `if...else if...else` ladder allows you to check between multiple test expressions and execute different statements.
1941

2042
```go
@@ -38,13 +60,15 @@ ashia_age := 38
3860

3961
if tom_age < ashia_age {
4062
if tom_age < 18 {
41-
println('tom_age < 18 and younger than Ashia.')
42-
} else {
43-
println('tom_age > 18 and older than Ashia.')
44-
}
45-
} else {
46-
println('$tom_age == $ashia_age')
47-
println('Tom and Ashia have the same age.')
63+
println('tom_age < 18 and younger than Ashia.')
64+
} else {
65+
println('tom_age >= 18 and younger than Ashia.')
66+
}
67+
}
68+
else if tom_age > ashia_age {
69+
println('$tom_age > $ashia_age')
70+
} else {
71+
println('$tom_age == $ashia_age')
4872
}
4973
```
5074

@@ -63,7 +87,11 @@ s := if tom_age < ashia_age {
6387
}
6488

6589
print(s)
66-
>> Tom is the youngest
90+
```
91+
92+
Output
93+
```bash
94+
Tom is the youngest
6795
```
6896

6997
## Exercises
@@ -72,73 +100,3 @@ print(s)
72100
2. Write a V program to check whether a given number is even or odd.
73101
3. Write a V program to check whether a given number is positive or negative.
74102
4. Write a V program to find whether a given year is a leap year or not.
75-
76-
## For
77-
78-
`for` loops offer a quick and easy way to do something repeatedly. They're handy, if you want to run the same code over and over again, each time with a different value. You can think of a loop as a computerized version of the game where you tell someone to take X steps in one direction then Y steps in another; for example, the idea "Go five steps to the east" could be expressed this way as a loop:
79-
80-
```go
81-
for i := 0; i < 5; i++ {
82-
println('Walking one step');
83-
}
84-
```
85-
86-
V has the `for` looping construct and the loop can be written in different ways:
87-
88-
1. `in` operator
89-
90-
```go
91-
ages := [18, 25, 32, 43, 50]
92-
93-
for age in ages {
94-
println(age)
95-
}
96-
```
97-
98-
Note, that the value is read-only.
99-
100-
2. `while` loop form
101-
102-
A `while` loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. There are no parentheses surrounding the condition, and the braces are always required.
103-
104-
```go
105-
mut factorial := 1
106-
mut counter := 1
107-
108-
for {
109-
counter++
110-
factorial=factorial*counter
111-
112-
if counter > 5 {
113-
print(factorial)
114-
break
115-
}
116-
}
117-
118-
println(counter)
119-
>> 120
120-
```
121-
122-
3. `for` with the traditional C style
123-
124-
```go
125-
mut factorial := 1
126-
mut counter := 1
127-
128-
for i := 0; i < 5; i++ {
129-
factorial=factorial*counter
130-
if i == 6 {
131-
print(factorial)
132-
continue
133-
}
134-
println(i)
135-
}
136-
```
137-
138-
## Exercises
139-
140-
1. Write a V program to display the first 10 natural numbers.
141-
2. Write a V program to find the sum of first 10 natural numbers.
142-
3. Write a V program to display n terms of natural number and their sum.
143-
4. Write a V program to read 10 numbers from keyboard and find their sum and average.
144-
5. Write a V program to display the cube of the number upto given an integer.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ A function is a block of organized, reusable code that is used to perform a sing
44

55
Ideally you should consider using the [single responsibility principle](https://en.wikipedia.org/wiki/Single_responsibility_principle) (SOLID) which states that every module or function should have responsibility over a single part of the functionality provided by the software to keep your code maintainable.
66

7+
Like C and Go, functions cannot be overloaded.
8+
79
```go
810
fn main() {
911
println(sum(77, 33))
@@ -26,6 +28,77 @@ fn full_name(first_name, last_name string) string {
2628
}
2729
```
2830

31+
## Variadic Functions
32+
33+
Functions can also be variadic i.e. accept infinite number of arguments. They are **not** arrays and cannot be returned.
34+
35+
```go
36+
fn main() {
37+
foo("V", "is", "the", "best", "lang" , "ever")
38+
}
39+
40+
fn foo(test ...string) {
41+
println(test)
42+
}
43+
```
44+
45+
Output
46+
47+
```bash
48+
V
49+
is
50+
the
51+
best
52+
lang
53+
ever
54+
```
55+
56+
## Multi-Return Functions
57+
58+
Similar to Go, functions in V can also return multiple and with different type.
59+
60+
```go
61+
fn main() {
62+
name, age := student("Tom", 15)
63+
println(name, age)
64+
65+
}
66+
67+
fn student(name string, age int) string, int {
68+
return name, age
69+
}
70+
```
71+
72+
Output
73+
74+
```bash
75+
Tom , 15
76+
```
77+
78+
## High Order Functions
79+
80+
Functions can also take in another function which is usually needed to sort, map, fitler etc.
81+
82+
```go
83+
fn square(num int) int {
84+
return num * num
85+
}
86+
87+
fn run(value int, op fn(int) int) int {
88+
return op(value)
89+
}
90+
91+
fn main() {
92+
println(run(10, square))
93+
}
94+
```
95+
96+
Output
97+
98+
```bash
99+
100
100+
```
101+
29102
## Exercises
30103

31104
1. Write a V program to find the square of any number using the function.

examples/hello_world/comment.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Comments
2+
3+
V supports single line comments `//` and multi-line comments `/* */`. They should be used for documenting the code for letting the other users know how the code works. It can also be used for temporarily commenting the code which has to be used later on.
4+
5+
```go
6+
// This is a single line comment
7+
8+
/* This is a
9+
* multi-line comment
10+
* /* This could be nested as well*/
11+
*/
12+
```

examples/hello_world/hello.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# Hello V
2-
31
## Formatted Print
42

53
Printing is handled by various I/O stream functions. One should know where to use them accordingly.

0 commit comments

Comments
 (0)