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




GO - FUNCTION ARGUMENTS


Say, you want to pass some information to a 'Function'.


For example, if you want a 'Function' to add two numbers. You need to pass those two numbers to the 'Function'.


Say, in the below example, you want the 'add' Function to add two numbers for you. And you have passed those numbers in between the brackets '()' of the 'add' Function(i.e. 'first_number' and 'second_number').


And those elements inside the brackets '()' of the 'add' Function is called as 'Parameters' and the actual values passed to them are called as 'Arguments'.


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

Say, in the below example, you want the 'add' Function to add two numbers forSo, the variables inside the 'add' Function is called as 'Parameter'.


java_Collections

And the actual values (i.e. '5' and '4') to be sent to the 'Function' are the 'Arguments'.


Throughout the tutorial we will be calling the 'Parameters' as 'Arguments' to avoid unnecessary confusion.


Now, what if, you have defined a 'Function' with two 'Argument' but made the Function call with one argument.


What if there is an Argument mismatch?


Now, what if, you have defined a 'Function' with two 'Argument' but made the Function call with one argument.


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, third_number int) int {
    result := first_number + second_number + third_number
    return result	
}


Output :



 not enough arguments in call to add
   have (int, int)
   want (int, int, int)

So, in the above code we have the 'add' Function with three 'Arguments'.


func add(first_number int, second_number int, third_number int) int {

java_Collections

But when the 'add' Function is called, it is called with just two 'Arguments'.


java_Collections

And there is a mismatch of 'Arguments'. And we end up with the below error.


not enough arguments in call to add
   have (int, int)
   want (int, int, int)

Let us see in the next example, how to fix it.


Fixing Argument mismatch with variable number of arguments


To solve the problem of Argument mismatch, we can just add an asterisk '*' in front of the 'Argument'.


Let us see in the below example.


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(numbers ... int) int {
    result := numbers[0] + numbers[1]
    return result	
}


Output :



 The added result is : 9

So, to avoid the 'Argument' mismatch problem. We have replaced the arguments 'first_number' and 'second_number' with variable number arguments, i.e. 'numbers ... int'.


'func add(numbers ... int) int'

And no matter how many 'Arguments' you pass, 'numbers ... int' will make an intelligent guess and create an integer 'Array' out of it.


Now, if you see the complete execution, we have initialised the first and second number in the variables, 'first_num' and 'second_num'.


first_num := 5
second_num := 4

java_Collections

java_Collections

And called the 'add' Function, passing above two values(i.e. 'first_num' and 'second_num') as 'Arguments'.


value = add(first_num, second_num)

And the execution of 'add' Function begins.


func add(numbers ... int) int {
    result := numbers[0] + numbers[1]
    return result	
}

So, the arguments 'first_num' and 'second_num' gets assigned to 'numbers ... int'.


java_Collections

And internally an Array is created for 'numbers' that has both the values of 'first_num' and 'second_num'.


java_Collections

And the variable 'numbers' is an Array holding the values '5' and '4'. And we know an Array can be accessed using indexes.


result := numbers[0] + numbers[1]

So, 'number[0]' would access the first element of the Array (i.e. '5') and 'number[1]' would access the second element of the Array (i.e. '4').


And the added value is returned to the caller.


return result

Call/Pass by Value


So far, we have seen that we have passed a value to the Function for addition or for any other operation.


Now, let us see the next example to understand 'pass by value'.


Example :



package main
import "fmt"
    
func main() {
    
    num := 5
            
    my_function(num)
    fmt.Println("The value is :",num)			 	
}
    
func my_function(num1 int) {
    
    num1 = 9
}


Output :



 The value is: 5

So, in the above code, we have defined a variable 'num' and assigned the value '5' to it.


num := 5

java_Collections

Then in the next line, we have called the Function 'my_function(num)' after passing the variable 'num' to it.


my_function(num)

And what happens is, the value of variable 'num' (i.e. '5') is passed to the variable 'num1'.


java_Collections

Now, 'num1' has the value '5' in it.


java_Collections

And the Function execution begins.


func my_function(num1 int) {

    num1 = 9
}

And all we have done in the Function is, changed the value of 'num1' to '9'.


java_Collections

And the function execution ends and we come back to the main method. Where the value of variable 'num' is printed.


fmt.Println("The value is :",num)

And if we see the output,


The value is : 5

The value of 'num' has not changed. It is still '5'.


Well! Let's not be confused.


The value hadn't changed because we have just passed the value.


java_Collections

And it's obvious that the value of 'num' won't change if the value of 'num1' changes. Because they aren't connected.


And what if we want to rectify it?


This is where 'Pass by reference' comes to rescue.


Call/Pass by Reference


Let us modify the above example using 'Pass by Reference'.


Example :



package main
import "fmt"
    
func main() {
    
    num := 5
            
    my_function(&num)
    fmt.Println("The new value is :",num)			 	
}
    
func my_function(num1 *int) {
    
    *num1 = 9
}


Output :



 The new value is: 9

And in this case, what we have done is, passed the address of the 'num' variable to the function.


Let us elaborate a little.


For every variable there is a memory location. The same way the house you live in has an address,the same way, the memory location of a variable also has an address.


Say for example, the variable 'num' has the address, '1087'(Just for our understanding).'num' is just a name. The value '5' resides in the address '1087'.


num := 5

java_Collections

In the next line we have passed the address(i.e. '1087') instead of the value '5'. Just remember, '&num' refers to the address of the variable num.


my_function(&num)

java_Collections

Now, what happens is, the variable 'num1' points to the location '1087'. Because of the pointer declaration (i.e. 'num1 *int')


java_Collections

Now, in the function,


func my_function(num1 *int) {
	*num1 = 9
}

The first line,


*num1 = 9

Is where we have assigned the value '9' to the address, '1087'.


java_Collections

And the value inside the address , '1087' gets changed.


Now, in the 'main()' function, when we try printing the value of 'num'.


fmt.Println("The new value is :",num)

It prints '9'. Because the address of 'num' is '1087', and the value of the address '1087' gets changed.


java_Collections