functions in golang-basics

f

Consider this below program:

package main

import "fmt"

func main() {
	fmt.Println("--------------x--------------")
        fmt.Println("--------------x--------------")
	fmt.Println("hi")
        fmt.Println("--------------x--------------")
        fmt.Println("--------------x--------------")
}

that "------x------" is just for decoration. sure You can print something like this if you want. But the logics is all matters in a code. The code firstly should work.

When you have a code with 150 lines, you don’t want to have your decorations take 50 lines. Do you?

So rather than having your decorations in a single block, you can add it in another block and use it here. Here’s what I mean:

package main

import "fmt"


func decoration() {
	fmt.Println("--------------x--------------")
	fmt.Println("--------------x--------------")
}

func main() {
	decoration()
	fmt.Println("hi")
	decoration()
}

Yes we used something called ‘a function’. Here’s the basic syntax:

func function_name( [parameter list] ) return_type
{
   //body of the function

}

Ok. The previous one was a terrible example. Lets say you want to find factorial of a number. what do you do.

package main

import "fmt"

func main() {
	var a int = 3

	var fact = 1

	for i := 1; i <= a; i++ {
		fact *= i
	}

	fmt.Println(fact)
}

Sure. This works. But what if you have many variables here and there. And You want to find factorial of each. You have to write something like this:

package main

import "fmt"

func main() {
	var a int = 3

	var fact = 1

	for i := 1; i <= a; i++ {
		fact *= i
	}

	fmt.Println(fact)

	var b int = 6

	for i := 1; i <= b; i++ {
		fact *= i
	}

	fmt.Println(fact)

	var c int = 7

	for i := 1; i <= c; i++ {
		fact *= i
	}

	fmt.Println(fact)
}

Sure it gets the work done. But the code looks more complicated from usual right? So lets wrap those common things in a function.

package main

import "fmt"

func factorial(a int) {
	var fact = 1

	for i := 1; i <= a; i++ {
		fact *= i
	}

	fmt.Println(fact)
}

func main() {
	var a int = 3
	factorial(a)

	var b int = 6
	factorial(b)

	var c int = 7
	factorial(c)

}

Now the code looks lot cleaner. Right? Here I passed the variables to the function. This is what we call function with arguments.

return statement

what if you don’t wanna print those values? I want that function to return me the factorial value. AND LET ME DECIDE WHAT SHOULD I DO WITH IT.

check this code:

package main

import "fmt"

func factorial(a int) int{
	var fact = 1

	for i := 1; i <= a; i++ {
		fact *= i
	}

	return fact
}

func main() {
	var a int = 3
	facta := factorial(a)

	var b int = 6
	factb := factorial(b)

	var c int = 7
	factc := factorial(c)

	fmt.Println("the sum of fact is ", facta+factb+factc)

}

In this program rather than printing the value, I returned it and then stored that value using a variable.

And thats it. Thats the basic of functions.

You can divide your code into separate functions. How you divide your code among different functions is up to you, but logically, the division should be such that each function performs a specific task.

About the author

vigneshwar

Add comment

Leave a Reply

By vigneshwar

Most common tags

%d bloggers like this: