Skip to content

Commit 4c1bf24

Browse files
shrn01ronreiter
authored andcommitted
Added functions and fixed a typo in Loops.md
Added functions in golang. Fixed the typo in for-range loop in Loops.md. Changed "for index, value in range" to "for index, value := range"
1 parent d0d30c0 commit 4c1bf24

File tree

3 files changed

+114
-2
lines changed

3 files changed

+114
-2
lines changed

Diff for: tutorials/learn-golang.org/en/Functions.md

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
Tutorial
2+
--------
3+
Functions provide a way to divide your code into logical blocks and reuse them minimising the time and effort and improve the managability of the code.
4+
5+
## Functions in Go
6+
7+
Functions in golang are defined using the `func` keyword.
8+
9+
The syntax to define a function in go
10+
11+
func <name of the function> (<inputs to the function>) (<datatypes of return values from the function>) {
12+
// your code here
13+
}
14+
15+
Let's write a add function using this syntax
16+
17+
func add (a int, b int) int {
18+
return a + b
19+
}
20+
21+
## How to call a function in your code
22+
23+
Calling a function is as easy as writing the function name and giving it the arguments. Now for an example let's call our add function in the main method.
24+
25+
package main
26+
27+
import "fmt"
28+
29+
func main() {
30+
sum := add(3, 5)
31+
32+
fmt.Println(sum) // prints 8
33+
}
34+
35+
Go also let's you ignore specific fields in the return values using the `_` variable.
36+
37+
package main
38+
39+
import "fmt"
40+
41+
func sumAndDiff (a int, b int) (int, int) {
42+
return a + b, a - b
43+
}
44+
45+
func main() {
46+
sum, _ := sumAndDiff(5, 3) // the 2nd variable is ignored and not used
47+
48+
fmt.Println(sum) // prints 8
49+
}
50+
51+
52+
We can also add named return values so that we dont need to deliberately return something in the function. But beware, use this only in small functions.
53+
54+
package main
55+
56+
import "fmt"
57+
58+
func add (a int, b int) (sum int) { // here we are defining the variable name of what we are returning
59+
sum = a + b // so no need for a return statement, go takes care of it
60+
}
61+
62+
func main() {
63+
sum := add(3, 5)
64+
65+
fmt.Println(sum) // prints 8
66+
}
67+
68+
69+
Exercise
70+
--------
71+
In this exercise, your will be given a integer slice as a input. Write a function with name `sumAll` that loops through all the numbers in the slice and returns the sum of the numbers.
72+
73+
Tutorial Code
74+
-------------
75+
package main
76+
77+
import "fmt"
78+
79+
// your code here
80+
81+
func main() {
82+
list := []int{1, 2, 3, 4, 5, 6}
83+
84+
fmt.Println(sumAll(list))
85+
}
86+
87+
Expected Output
88+
---------------
89+
21
90+
91+
Solution
92+
--------
93+
package main
94+
95+
import "fmt"
96+
97+
func sumAll(list []int) int {
98+
sum := 0
99+
100+
for _, value := range list {
101+
sum = sum + value
102+
}
103+
104+
return sum
105+
}
106+
107+
func main() {
108+
list := []int{1, 2, 3, 4, 5, 6}
109+
110+
fmt.Println(sumAll(list))
111+
}

Diff for: tutorials/learn-golang.org/en/Loops.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ The for-range loop provides us access to the index and value of the elements in
5757
func main() {
5858
myList := []int{1,2,3}
5959

60-
for index, value in range myList {
60+
for index, value := range myList {
6161
fmt.Printf("%d is index, %d is value", index, value)
6262
}
6363
}
6464

6565
We can ignore one or both the fields in a for-range loop by giving an `_` instead of giving a variable name
6666

67-
for _, value in range myList {
67+
for _, value := range myList {
6868
fmt.Println(value)
6969
}
7070

Diff for: tutorials/learn-golang.org/en/Welcome.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ learn-golang.org is still under construction - If you wish to contribute tutoria
1616
- [[Slices]]
1717
- [[If-Else]]
1818
- [[Loops]]
19+
- [[Functions]]
1920

2021
### Contributing Tutorials
2122

0 commit comments

Comments
 (0)