|
| 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 | +} |
0 commit comments