Easy Tutorial
❮ Go Interfaces Go Array Of Pointers ❯

Go Language Functions

Functions are the basic building blocks of code, used to perform a task.

Go language has at least one main() function.

You can divide different functionalities through functions, logically each function performs a specific task.

The function declaration tells the compiler about the function's name, return type, and parameters.

The Go language standard library provides various built-in functions that can be used. For example, the len() function can accept parameters of different types and return the length of that type. If we pass a string, it returns the length of the string; if we pass an array, it returns the number of elements in the array.


Function Definition

The format for defining a function in Go is as follows:

func function_name([parameter list]) [return_types] {
    function body
}

Function definition breakdown:

Example

The following example is the code for the max() function, which takes two integer parameters num1 and num2 and returns the maximum of these two:

Example

/* Function returns the maximum of two numbers */
func max(num1, num2 int) int {
   /* Declare local variable */
   var result int

   if (num1 > num2) {
      result = num1
   } else {
      result = num2
   }
   return result 
}

Function Call

When you create a function, you define what the function needs to do, and by calling this function, you execute the specified task.

Calling a function involves passing parameters to it and receiving a return value, for example:

Example

package main

import "fmt"

func main() {
   /* Define local variables */
   var a int = 100
   var b int = 200
   var ret int

   /* Call function and return maximum value */
   ret = max(a, b)

   fmt.Printf( "Maximum value is : %d\n", ret )
}

/* Function returns the maximum of two numbers */
func max(num1, num2 int) int {
   /* Define local variable */
   var result int

   if (num1 > num2) {
      result = num1
   } else {
      result = num2
   }
   return result 
}

The above example calls the max() function within the main() function, and the execution result is:

Maximum value is : 200

Function Returning Multiple Values

Go functions can return multiple values, for example:

Example

package main

import "fmt"

func swap(x, y string) (string, string) {
   return y, x
}

func main() {
   a, b := swap("Google", "tutorialpro")
   fmt.Println(a, b)
}

The above example execution result is:

tutorialpro Google

Function Parameters

If a function uses parameters, these variables are called the function's formal parameters.

Formal parameters behave like local variables defined within the function body.

When calling a function, there are two ways to pass parameters:

Pass Type Description
Value Pass Value pass means that when calling a function, a copy of the actual parameters is passed into the function. Changes to the parameters within the function will not affect the actual parameters.
Reference Pass Reference pass means that when calling a function, the address of the actual parameters is passed into the function. Changes to the parameters within the function will affect the actual parameters.

By default, Go language uses value pass, which means it will not affect the actual parameters during the call.


Function Usage

Function Usage Description
Function as an Argument to Another Function A function defined can be passed as an actual parameter to another function
Closure A closure is an anonymous function, which can be used in dynamic programming
Method A method is a function with a receiver
❮ Go Interfaces Go Array Of Pointers ❯