Functions in Go programming

Functions in Go programming

Introduction

In this tutorial, you’ll understand functions in Go programming. Basically, a function is a group of instructions with a name. And the name is actually optional, but it’s a bunch of instructions that are group together. as we’ll see step by step.

The func main(). We’re defining a function named, main. And we have, all programs in Golang have to have the main function, because, That’s where execution starts.

So here is our first main() function,

 func main()
 {
   fmt.Printf("Welcome to GoLang")
 }

Now, the main() function is a special function and that you never call this function. So when you run your program from the command line say, when you run the program, then the main function gets called and invoked itself immediately.

When we calling a function, it means to execute that function. And it happens automatically with main() function. As soon as you run it, it goes straight to main function, calls that function. But for any other function you define, you have to call explicitly to that code.

The function declaration is where you define the function. In the Golang functions, declaration starts with that keyword func. And then you have the name of the function after that func keyword. You have open paren, close paren. You might have arguments in there.

func main()
 {
   fmt.Printf("Welcome to GoLang")
   PlaySong()
 }
 func PlaySong()
 {
   fmt.Printf("Play Song !!")
 }

Why use a function

Reusability

The reusability means, actually, you no need to rewrite the same code over and over again. So if already you have a function, and you define a function, you only need to define that function or declare that function one time. after that, you can call it and run it as many times as you want.

Basically, It shrinks the size of your source code and then you can reuse it. if you can actually reuse it across projects too. So people can use your function and also they can import the library and use your function and your code also. So it’s good for reuse.

Abstraction

We have another important feature to use functions is an abstraction. the abstraction is hiding details of the implementation. actually, the problem with big designs is that there are so many details that a human can’t keep all these details in his or her mind.

If you working with complicated code, but it’s got to be simple or else your mind can’t hold it. So abstraction is the easy way to do that. the abstraction is, you group things and you hide the details and functions are exactly for this.

Function Parameters and Return values

Basically, the functions need some sort of input data to operate on. because they are sequences of instructions or sets of instructions, and also they need some data to work on. So it is very common for functions to have a set of parameters.

Parameters

The parameters a set of variables that are listed right after the function name in the declaration in the parenthesis. so those variables become local variables that are used as inputs to the function. So the function actually operates on these variables.

Argument

Now an argument is a data that’s applied for those parameters during the call. you can see the following example,

func multiplication(a int, b int)
{
  fmt.Print(a*b)
}
func main()
{
  multiplication(2,5)
}

In the parenthesis is a list of the parameters, so it means there are two parameters, a and b, and they are both integers. So they expect somebody when the function gets called, then they expect the caller to pass two integers as arguments.

Call by Value and Reference

The call by value describes how arguments pass to the parameters during a function call. then you call a function and pass it a set of arguments that are bound to the parameters inside the function when you execute the function.

But different languages can pass arguments in different ways. Call by value is how it’s done in GoLang. Actually, call by value means is that the arguments that are passed as parameters are copied to the parameters. So the data that the function is using, it’s using the data that’s assigned to the parameters. the data that it uses is a copy of the original, it’s not the original.

func sample(x int)
{
 x = x+1
}
func main()
{
 y := 2
 sample(y)
}

The above example, the function called can’t interfere with the original variables in the calling function. So after the modifying parameters have no effect on the outside function, on the calling function.

Passing Arrays and Slices

If you want to pass an array as an argument to a function. then, you want to do some processing on the array. So the all arguments are all copied because this is called by value, so the whole array has to be copied to the parameters. And if the array is big, then it’s a problem, it will take a lot of time to copy the arguments, also use an excessive amount of space.

we have an example,

func sample(x [3]int) int{
 return x[0]
}

func main()
{
 y := [3]int{1, 2, 3}
 fmt.Print(sample(y))
}

Passing Slices Instead

In GoLang, if you use slices instead. In fact, in general, in Golang get used to using slices instead of arrays. the slice is like a window on an array. and also you can almost always use a slice instead of an array. then passing slice copies the pointer. after that we pass an array, it copies the whole array. But a slice is not actually an array. It’s a structure that contains three things.

A pointer to the array or a pointer to the start to the slice in the array, and the length and capacity. the structure with those three pieces of data, one of which is a pointer.

If you pass a slice when you actually get the pointer and also the length and capacity. So that pointer can use to change the slice without you having to dereference and reference as we did with the array example.

 func sample(slival int)
 {
   slival[0] = slival[0] + 1
 }
 func main()
 {
  x := []int {1, 2, 3}
  sample(x)
  fmt.Print(x)
 }

Leave a Reply

Your email address will not be published. Required fields are marked *