Follow

Follow

How To Use Functions In Go?

Maheshwar Ligade's photo
Maheshwar Ligade
·Apr 5, 2022·

3 min read

Play this article

Table of contents

  • Defining Functions
  • Calling Functions
  • Passing Arguments By Reference
  • Returning Multiple Values

Functions are an essential building block of any programming language, and Go is no exception. In Go, a function is a block of code that performs a specific task and can be called from other parts of your program. In this article, we'll cover the basics of how to define and use functions in Go.

Here is the basic syntax for defining a function in Go:

func functionName(parameters) return_type {
    // function body
    return return_value
}

Let's break this syntax down:

  • func: keyword used to define a function

  • functionName: name of the function

  • parameters: input variables that the function takes

  • return_type: the data type that the function returns

  • function body: the code that the function executes

  • return_value: the value that the function returns

Defining Functions

To define a function in Go, we use the func keyword followed by the name of the function, a set of parentheses, and an optional list of arguments. The arguments are defined by their type, separated by commas. If the function returns a value, we specify the return type after the argument list.

Here's an example of a function that takes two integers as arguments and returns their sum:

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

In this example, the function add takes two integers x and y as arguments and returns their sum. The return type is specified after the argument list as int.

Calling Functions

To call a function in Go, we simply write the name of the function followed by its arguments in parentheses. Here's an example:

sum := add(3, 5)
fmt.Println(sum) // Output: 8

In this example, we call the add function with arguments 3 and 5 and store the result in the variable sum. We then print the value of sum using the fmt.Println function.

Passing Arguments By Reference

In Go, arguments are passed by value by default. This means that a copy of the argument is made and passed to the function. However, we can also pass arguments by reference using pointers. Here's an example:

func increment(x *int) {
    *x++
}

func main() {
    n := 0
    increment(&n)
    fmt.Println(n) // Output: 1
}

In this example, we define a function increment that takes a pointer to an integer as an argument. Inside the function, we use the * operator to dereference the pointer and increment the value of the integer. We then call the increment function with the address of the variable n using the & operator.

Returning Multiple Values

Go allows functions to return multiple values. This can be useful in situations where we need to return more than one value from a function. Here's an example:

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

func main() {
    a, b := "hello", "world"
    a, b = swap(a, b)
    fmt.Println(a, b) // Output: world hello
}

In this example, we define a function swap that takes two strings as arguments and returns them in reverse order. We then call the swap function with arguments "hello" and "world" and store the result in variables a and b. We then print the values of a and b using the fmt.Println function.

Conclusion

In summary, functions are an essential part of the Go programming language, and they allow you to group a block of code that performs a specific task. You can call functions from anywhere within the program and pass arguments by value or reference using pointers. By understanding how to use functions in Go, you can write more efficient and maintainable code.

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this