Go Passing Arrays To Functions
# Go Passing Arrays to Functions
[Go Arrays](#)
Arrays in Go are value types. Therefore, when an array is passed to a function, a copy of the array is actually passed.
If you want to pass an array parameter to a function, you need to declare the parameter as an array in the function definition. We can declare it in the following two ways:
### Method 1
The parameter specifies the array size:
func myFunction(param int) { ....}
### Method 2
The parameter does not specify the array size:
func myFunction(param []int) { ....}
If you want to modify the original array within the function, you can do so by passing a pointer to the array.
### Example
Let's look at the following example. The function takes an integer array parameter, another parameter specifies the number of array elements, and returns the average:
## Example
func getAverage(arr []int, size int)float32
{
var i int
var avg, sum float32
for i=0;i< size;++i{
sum += arr
}
avg = sum / size
return avg;
}
Next, let's call this function:
## Example
package main
import"fmt"
func main(){
/* Array length is 5 */
var balance =int{1000,2,3,17,50}
var avg float32
/* Array is passed as a parameter to the function */
avg = getAverage( balance,5);
/* Output the returned average */
fmt.Printf("Average: %f ", avg );
}
func getAverage(arr int, size int)float32{
var i,sum int
var avg float32
for i=0;i< size;i++{
sum += arr
}
avg =float32(sum)/float32(size)
return avg;
}
The output of the above example is:
Average: 214.399994
In the above example, the parameter we used did not specify the array size.
Floating-point calculations have some deviation in output. You can also convert to integers to set precision.
## Example
package main
import(
"fmt"
)
func main(){
a :=1.69
b :=1.7
c := a * b // The result should be 2.873
fmt.Println(c)// Outputs 2.8729999999999998
}
Set fixed precision:
## Example
package main
import(
"fmt"
)
func main(){
a :=1690// Represents 1.69
b :=1700// Represents 1.70
c := a * b // The result should be 2873000 representing 2.873
fmt.Println(c)// Internal encoding
fmt.Println(float64(c)/1000000)// Display
}
If you want to modify the original array within the function, you can do so by passing a pointer to the array.
The following example demonstrates how to pass an array to a function. The function accepts an array and a pointer to the array as parameters:
## Example
package main
import"fmt"
// Function accepts an array as a parameter
func modifyArray(arr int){
for i:=0;i<len(arr);i++{
arr= arr*2
}
}
// Function accepts a pointer to an array as a parameter
func modifyArrayWithPointer(arr *int){
for i:=0;i<len(*arr);i++{
(*arr)=(*arr)*2
}
}
func main(){
// Create an integer array with 5 elements
myArray :=int{1,2,3,4,5}
fmt.Println("Original Array:", myArray)
// Pass the array to the function, but it will not modify the values of the original array
modifyArray(myArray)
fmt.Println("Array after modifyArray:", myArray)
// Pass a pointer to the array to the function, which can modify the values of the original array
modifyArrayWithPointer(&myArray)
fmt.Println("Array after modifyArrayWithPointer:", myArray)
}
In the example above, the `modifyArray` function accepts an array and attempts to modify its values, but after being called in the main function, the original array is not modified. Conversely, the `modifyArrayWithPointer` function accepts a pointer to an array and modifies the values of the original array through the pointer.
The output of the above example is:
Original Array: Array after modifyArray: Array after modifyArrayWithPointer:
[Go Arrays](#)
YouTip