English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statements

Golang Function & Method

Golang Struct

Golang Slice & Array

Golang String(String)

Golang Pointer

Golang Interface

Golang Concurrency

Golang Exception(Error)

Other items in Golang

Go Language Function

Functions are usually code or statement blocks in a program, allowing users to reuse the same code, thereby ultimately achieving memory saving, time saving, and more importantly, providing better code readability. Therefore, basically, functions are a collection of statements that perform certain specific tasks and return the results to the caller. Functions can also perform certain specific tasks without returning any content.

Function declaration

Function declaration is a method of constructing a function.

Syntax:

func function_name(Parameter_list)(Return_type){
    // function body.....
}

The declaration of the function includes:

  • func:It is a keyword in Go language, used to create functions.

  • function_name:It isfunctionof the name.

  • Parameter_list:Contains the names and types of the function parameters.

  • Return_type:This is optional, it contains the type of the value returned by the function. If the return_type is used in the function, then the return statement must be used in the function.

Function calling

Function calling is completed when the user wants to execute the function. It is necessary to call the function to use its features. As shown in the following example, we have a function named add() with two parameters. Now we use its name, that is, the area with two parameters (12,10)is called in the main function.

package main
import \
//area() is used to find the area of the rectangle
//The two parameters of the function, namely length and width
func area(length, width int) int {
    Ar := length * width
    return Ar
}
func main() {
    //Display the area of the rectangle
    //By method calling
    fmt.Printf("Area of the rectangle : %d", area(12, 10))
}

Output:

Area of the rectangle : 120

Function parameters

In Go language, the parameters passed to the function are called actual parameters (arguments), and the parameters received by the function are called formal parameters (parameters).

Note:By default, Go language uses the call by value method to pass parameters to functions.

Go language supports two methods of passing parameters to functions:

  1. Call by value: In this method of parameter passing, the value of the actual parameters is copied to the formal parameters of the function, and the two types of parameters are stored in different storage locations. Therefore, any changes made inside the function will not reflect in the caller's actual parameters.

    package main 
       
    import \
       
    // Function to swap values
    func swap(a, b int) int { 
      
        var o int
        o = a 
        a = b 
        b = o 
         
       return o  
    } 
       
    func main() { 
     var p int = 10 
     var q int = 20 
      fmt.Printf("p = %d and q = %d", p, q) 
       
     // Call by values 
     swap(p, q) 
       fmt.Printf("\np = %d and q = %d", p, q) 
    }

    Output:

    p = 10 and q = 20
    p = 10 and q = 20
  2. By reference calling:Actual parameters and formal parameters both point to the same location, so any changes made inside the function actually reflect in the caller's actual parameters.

    package main
    import \
    //Function to swap values
    func swap(a, b *int) int {
        var o int
        o = *a
        *a = *b
        *b = o
        return o
    }
    func main() {
        var p int = 10
        var q int = 20
        fmt.Printf("p = %d and q = %d", p, q)
        //Reference Calling
        swap(&p, &q)
        fmt.Printf("\np = %d and q = %d", p, q)
    }

    Output:

    p = 10 and q = 20
    p = 20 and q = 10