Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - FUNCTION


A 'Function' in Go is a chunk of Code that is used to conduct a particular task. And that chunk is only executed when it is called.


Say if you want to add two numbers, you can have an add 'Function' that would be dedicated to add the numbers.


Similarly, if you want to divide two numbers, you can have a divide 'Function' that would divide the numbers.


So, instead of placing all the codes in a single place. You can distribute the work among different 'Functions'. So, that your code looks more structured and clean.


How a Function in Go is created?


Below are the rules to create a Function :

  1. A Function should begin with a 'func' keyword - (i.e. func).
  2. Following the 'func' keyword, a Function should have a name - (i.e. func myfunction).
  3. And the name of the function should have a starting and ending bracket '()' - (i.e. func myfunction()).
  4. There can be(Or cannot be) something called as Arguments inside the bracket '()' - (i.e. def myfunction(myarg int)).
  5. After the bracket '()' there could be a return type, which means, we might have to specify the data type of the value we want to return(It can be more than one). And if no value is returned, then we there is no need of specifying any data types.
  6. As said, a Function is a chunk of Code used to conduct a task. And the block of code should be inside starting brace '{' and an ending brace '}'.

    func myfunction() {
        fmt.Println("This is my first function")
    }

And thats how we define a 'Function'.


But it was mentioned in the 'Function' description that a 'Function' only executes only when it is called.


So, how a 'Function' is called?


Let us see in the below example.


Note : Let us omit the argument and return type for now.

Example :



package main
import "fmt"
	
func main() {
	
    myfunction()			 	
}
	
func myfunction() {
    fmt.Println("This is my first function")
}


Output :



 This is my first function

So, in the above code, there are two parts,

  1. The Function definition.

    func myfunction() {
        fmt.Println("This is my first function")
    }
  2. And the 'Function', call, inside the 'main()' Function.

    myfunction()

So, in the above code we have defined a 'Function', 'myfunction()'. You can give any name to the 'Function'(In this case we have given the name 'myfunction()'). Just don't forget to write the 'func' keyword before it.


func myfunction() {
	fmt.Println("This is my first function")
}

java_Collections

And the work of the 'Function', 'myfunction()' is, just to print 'This is my first function'.


Now, just remember one thing, the above 'Function', 'myfunction()' will never execute until it is called.


And thus we have called the 'Function', 'myfunction()' from main().


myfunction()

And the 'Function', 'myfunction()' is called, printing,


This is my first function

Although, the 'Function', 'myfunction()' is defined in the first line in the code.


def myfunction():
    print("This is my first function")
	

But it will never be executed. Go will ignore the the above 'Function' definition.


And come to the next line, where it finds the 'Function', 'myfunction()' is called.


func main() {

	myfunction()			 	
}
	

And only then the Function executes.


Now, let us look at another example of adding two numbers and returning the result using 'Functions'.


Example :



package main
import "fmt"
	
func main() {
	
	first_num := 5
	second_num := 4   	
	value := add(first_num, second_num)
	fmt.Println("The added result is :",value)			 	
}
	
func add(first_number int, second_number int) int {
	result := first_number + second_number
	return result	
}


Output :



 The added result is : 9

Let us explain the above example with a practical scenario.


Say you are given a task of adding two numbers. And you would follow the below steps to achieve it :

  1. You would ask the firstNumber and secondNumber from the person who has given you this task.

    And the above Go Function expects the same thing.

    func add(first_number int, second_number int) int {


    Here, 'first_number' and 'second_number' are the two numbers which a Go 'Function' expects just like you(first_number' and 'second_number' are called as arguments that we will be learning in a separate tutorial).

    And 'add' is the name of the 'Function'. And just like the above example we have used the 'func' keyword to define a 'Function'.

    In addition to that, there is something called as the return type(i.e. 'int'). Which we will be explaining soon.

    java_Collections
  2. The next thing you would do is, add the numbers and give the added value to the caller.

    Same thing is done by the Above 'Function'.

    It adds 'first_number' and 'second_number' and stores the added value in a variable called 'result'.

    result = first_number + second_number


    It then returns the 'result' to the caller(We will see next, who is a caller).

    return result;


    And since the returned value 'result' is of type 'int'. We have specified 'int' in the returned type.

So far, we have seen how a 'Function' works. Now, let us see who is the caller?


When the program execution begins,


java_Collections

And we initialise the first variable 'first_num' with the value '5'.


first_num := 5

java_Collections

Then in the next line, we have initialised the second variable 'second_num' with the value '4'.


second_num := 4

java_Collections

Then we call the 'add(first_num, second_num)' Function.


value := add(first_num, second_num)

And Go searches for a Function with two arguments.


When it finds the Function, it assigns the value of 'first_num' to 'first_number' and 'second_num' to 'second_number'. And expects a value in return.


java_Collections

java_Collections

java_Collections

And gets into the block of the 'def add(first_number, second_number):' Function.


java_Collections

This is where the numbers are added,


result := first_number + second_number

And the result is stored in a variable 'result'.


java_Collections

And in the next line we 'return' the added value(i.e. 'result').


return result

And the returned value goes back to the line, where the Function was called.


value := add(first_num, second_num)

And the variable 'value' gets the added result(i.e. '9') from the variable 'result'.


java_Collections

And in the next line the added value is printed as output.


fmt.Println("The added result is :",value)