Skip to content

Files

Latest commit

 Cannot retrieve latest commit at this time.

History

History
42 lines (28 loc) · 690 Bytes

solution.md

File metadata and controls

42 lines (28 loc) · 690 Bytes

Functions 101

Objectives

  1. Define a function that gets two integers, adds them and returns the result

    1. Make sure to also write code that executes the function
  2. What is the problem with the following code? How to fix it?

package main
        
import "fmt"

func add(x, y int) {
    return x + y
}

func main() {
    fmt.Println("Result:", add(2, 3))
}

Solution

package main

import "fmt"

func add(x, y int) int {
    return x + y
}

func main() {
    fmt.Println(add(2017, 2022))                       
}
  1. It returns an integer but at the same time the function doesn't specify any return value so Go expects the function to return nothing.