Introduction to Functions

Functions are the building blocks of a Go program. They allow you to group code into reusable units that can be called with zero or more arguments.

Function Signature

In Go, the variable type comes after the variable name in the function signature. This design makes signatures easier to read from left to right.

func sub(x int, y int) int {
    return x - y
}

Key Components

  • func: The keyword used to declare a function.
  • Parameters: The inputs (like x and y above).
  • Return Type: The type of value the function outputs (like int above).