Functions in Golang4 min read

Functions in Golang

  • Functions in Golang are the blocks of code or statements in a program that allows the user to reuse the same code, which saves memory and also improves the readability of the code.
  • A function is generally used to perform a specific task in a program. Input and output are present in all programming functions. The function includes the instructions for producing the output from the input.
  • For example, a function might take as input any 2 numbers. The function might create output by adding the 2 input numbers. Therefore, the output of the function would be the addition of its input.

A function can be called from any other function such as the main function (starting point of a program). A function might or might not return any value.

Syntax:

func function_name(parameter_list){
	//statements
}

Where,

  • func – it is a keyword in golang which is used to define a function
  • function_name – it is a name that we provide to our function
  • parameter_list – here we provide the parameters for our function along with their data types. We can include n number of parameters here. All the parameter names should be unique.

Example:

package main
import "fmt"

func greet(name string){
  fmt.Println("Hello", name)
}

func main(){
  greet("John")
}

Output:

C:\Users\Hiberstack\GoPrograms>go run FunctionDemo1.go
Hello John
program to demonstrate functions in golang

Functions in Golang with return type

As mentioned earlier, a function might even return a value. In such case, we need to provide a return type to a function. Following is the syntax to provide a return type to a function,

Syntax:

func function_name(parameter_list) return_type{
  //statements
}

Following is the example of a program that includes a function that returns a value.

Example:

package main
import "fmt"

var areaOfRect int

func area(length int, width int) int{
  areaOfRect = length * width
  return areaOfRect
}

func main(){
  fmt.Println(area(5, 4))
}

Output:

C:\Users\Hiberstack\GoPrograms>go run FunctionDemo2.go
20

Function Calling

A function in golang is called when we want to execute it. A function can be called through another function. We can call a function from the main function but it is not mandatory. We can even call a function from another user-defined function. A function has to be called by passing the values to it which the function accepts in its parameter_list. Below is an example of calling a function,

Example:

package main
import "fmt"

func function1(s1 string) {
  fmt.Println("called", s1)
  function2("function 2") //calling function2
}

func function2(s2 string) {
  fmt.Println("called", s2)
}

func main(){
  function1("function 1") //calling function1
}

Output:

C:\Users\Hiberstack\GoPrograms>go run FunctionDemo3.go
called function 1
called function 2

Function Arguments/Parameters

If a function has to use arguments, we have to declare variables in it so that the value can be saved in them. The arguments provided to a function are known as actual parameters in Go, while the parameters received by a function are known as formal parameters. The format parameters behave like local variables inside a function which are created at the time of calling a function and are deleted when the function ends.

In Golang, there are 2 ways to pass arguments to the function.

  1. Call by Value
  2. Call by Reference

Note: By default, Golang uses Call by Value to pass the arguments to the function.

1. Call by Value

In this method, the values of the variables are passed as the arguments to the functions. In short, the values of the actual variables will be passed to the function and variables declared in the function argument will store their values in a different memory location. Here, any change in the value of the variables that are declared in the function will not affect the values of the actual variables. Both the variables are different and hold a unique location in the memory.

2. Call by Reference

In this method, the memory address of the actual variable is passed to the function and is held by the variable that is declared in the function argument. Any change in the value of the function variable will result in a change in the value of the actual variable. In short, both the variables point to the same memory location and hence contain the same values.


Share:

Leave a Reply