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'.
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 }
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'.
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.
Now, what if, you have defined a 'Function' with two 'Argument' but made the Function call with one argument.
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 }
So, in the above code we have the 'add' Function with three 'Arguments'.
But when the 'add' Function is called, it is called with just two 'Arguments'.
And there is a mismatch of 'Arguments'. And we end up with the below error.
Let us see in the next example, how to fix it.
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.
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 }
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'.
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'.
And called the 'add' Function, passing above two values(i.e. 'first_num' and 'second_num') as 'Arguments'.
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'.
And internally an Array is created for 'numbers' that has both the values of 'first_num' and 'second_num'.
And the variable 'numbers' is an Array holding the values '5' and '4'. And we know an Array can be accessed using indexes.
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.
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'.
package main import "fmt" func main() { num := 5 my_function(num) fmt.Println("The value is :",num) } func my_function(num1 int) { num1 = 9 }
So, in the above code, we have defined a variable 'num' and assigned the value '5' to it.
Then in the next line, we have called the Function 'my_function(num)' after passing the variable 'num' to it.
And what happens is, the value of variable 'num' (i.e. '5') is passed to the variable 'num1'.
Now, 'num1' has the value '5' in it.
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'.
And the function execution ends and we come back to the main method. Where the value of variable 'num' is printed.
And if we see the output,
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.
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.
Let us modify the above example using 'Pass by Reference'.
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 }
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'.
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.
Now, what happens is, the variable 'num1' points to the location '1087'. Because of the pointer declaration (i.e. 'num1 *int')
Now, in the function,
func my_function(num1 *int) { *num1 = 9 }
The first line,
Is where we have assigned the value '9' to the address, '1087'.
And the value inside the address , '1087' gets changed.
Now, in the 'main()' function, when we try printing the value of 'num'.
It prints '9'. Because the address of 'num' is '1087', and the value of the address '1087' gets changed.