Function Syntax & Example In Golang 2020

In This Article You will Understand Function In golang with how to return value from function and multiple parameters from function and Variadic function in go language.

Check This Previous blog post written by Sagar Jaybhay for better understanding.

Function In Golang:

package main

import "fmt"

func main() {
    fmt.Println("i am in main function")
}

Every program execution in golang starts with func main() . It is the entry point in golang.

Functions will be with a parameter, without parameters you can pass multiple parameters to function.

Main use of function is that reusability is achieved suppose you want to display customer name for every customer who buys something from shop so you need to write a code to print customer name and theirs is another section where account department want names who buy something then you no need to write display customer name code again instead you need to write display customer name in a function and call this function when you need that.

And suppose in future you need to modify the code you need to change in single location.

Function in golang
Function in golang

Some terminology for naming for function

package main

import "fmt"

func displaycustomer(name string) {
    fmt.Println(name)
}

func main() {
    fmt.Println("i am in main function")

    displaycustomer("sagar")
    displaycustomer("jaybhay")
}

In the above code when we write the function displaycustomer where we pass the name so this called parameter.

    displaycustomer("sagar")

when you call this you called as argument which has value sagar in our case.

How to return the value from a function?

package main

import "fmt"

func displaycustomer(name string) {
    fmt.Println(name)
}

func main() {
    fmt.Println("i am in main function")

    displaycustomer("sagar")
    displaycustomer("jaybhay")

    b := addition(10, 20)
    fmt.Println("addition is => ", b)

}

func addition(a, b int) int {
    return a + b
}

In above code see addition code of a function

func addition(a, b int) int {
    return a + b
}

We pass a and b as a parameter where both are integer type and if both are integer type you need to declared only once as both have the same type.

How to return multiple values from one function?

func multiplevaluesreturn(a, b int) (int, int) {
    return a * 10, b * 20
}

By using this you can return multiple values from the function.

Return multiple value from function in golang
Return multiple value from function in golang

Variadic function in golang:

The function that called with a variable number of parameters or arguments is known as a variadic function.  In simple words, you can pass a parameter from 0 to N number of parameters or arguments.

In golang fmt.Println() is a variadic function but the only things are that it required one fixed argument at the start after that it will accept any number of parameters.

Important points to remember

  • The declaration of a function, the last parameter is preceded by an ellipsis i.e (…) which indicates that function can be called with any number of arguments.
  • General Syntax is

function function_name(para1, para2...type)type{
// code...
}

  • Inside the function …type behaves like an array in simple word and in golang term is called slice.
  • When you didn’t pass anything to vardiac function then the slice becomes nil.
  • Remember one thing you can not use a variadic parameter as a return value.

package main

import "fmt"

func main() {
    fmt.Print("This is variadic function demo")
    n := totalsum(10, 20, 30, 40, 50)
    fmt.Println("total sum is ", n)

}

func totalsum(array ...int) int {

    fmt.Println(array)
    fmt.Println("------------------")

    fmt.Printf("%T \t", array)

    total:= 0

    for _, v:= range array {
        total += v
    }
    return total
}

Variadic function in golang
Variadic function in golang

When we use a Variadic function:

  1. When you don’t know how many parameters will pass then you can use variadic function/
  2. When you use this type of function it will increase the readability of our program
  3. It is also used when you want to pass a slice in function.

GitHub Project Link :- https://github.com/Sagar-Jaybhay/GoLanguageLab

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...