
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass an Array to a Function in Golang
In programming, to make the code more modular, usable, and readable we break the code into different functions. For example, we have to sort some set of numbers for which we will create a separate function. For this, we have a data structure called arrays in programming that is nothing but a collection of multiple values of the same type. Once the array of numbers is sorted then at the end of that function we have to return the sorted elements so in this article we will see how to return the array.
Syntax
The syntax to declare a function with an array as a parameter is.
func functionName(arrayVariableName [sizeOfArray]datatype) returnType { // logic }
Algorithm
Step 1: var array [2]string ? Declare the array of string type with size 2.
Step 2: array[0] = "John" ? Initialize both the indexes of the array.
Step 3: printInAlphabeticalorder(array) ? Call the function to which we are passing the array as an argument.
if strings.Compare(array[0], array[1]) == 1 {} ? Start the if condition to compare both strings and print them in the if else condition.
Step 4: Print the result in alphabetical order in the called function.
Example
In this example, we are going to create an array of size two, datatype string and then print the strings in that array in alphabetical order by passing the array to a function.
package main import ( // fmt package provides the function to print anything "fmt" // strings package has functions related to string "strings" ) // array is the argument in this function func printInAlphabeticalorder(array [2]string) { fmt.Println("Printing strings in alphabetical order.") // comparing both the string and printing the // one first that comes first in alphabetical order if strings.Compare(array[0], array[1]) == 1 { fmt.Printf("%s %s\n", array[0], array[1]) } else { fmt.Printf("%s %s\n", array[1], array[0]) } } func main() { // declaring the array of type string with size 5 var array [2]string // initializing all the indexes of an array array[0] = "John" array[1] = "Andrew" fmt.Println("Golang program to pass array in an argument.") // passing argument in the function printInAlphabeticalorder(array) }
Output
Golang program to pass array in an argument. Printing strings in alphabetical order. John Andrew
Algorithm
Step 1: var array [5]int ? Declare the array of integers type with size 5.
Step 2: array[0] = 5 ?.? Initialize all the indexes of the array.
Step 3: printInSumOfArray(array) ? Call the function which is passing the array as an argument.
var sum int / sum = 0 ? The function first initializes and declares the number.
for i := 0; i < len(array); i++ { } ? Running a loop to find the sum of all numbers in the array.
Step 4: Print the result in alphabetical order in the called function.
Example
In this example, we are going to create an array of size 5, data type int, and then find the sum of all the numbers in the array by passing the array to a function.
package main import ( // fmt package provides the function to print anything "fmt" ) // array is the argument in this function func printInSumOfArray(array [5]int) { fmt.Println("Printing the sum of all the numbers in the array.") // declaring the variable sum of int type int to store the sum // of all the numbers in the array var sum int // initializing the variable sum with 0 sum = 0 // running a for loop over the array for i := 0; i < len(array); i++ { // adding current index number of array in sum sum = sum + array[i] } fmt.Printf("The sum of all the numbers in the array is %d.\n", sum) } func main() { // declaring the array of type string with size 5 var array [5]int // initializing all the indexes of an array array[0] = 5 array[1] = 59 array[2] = 43 array[3] = 12 array[4] = 98 fmt.Println("Golang program to pass array in argument.") // passing argument in the function printInSumOfArray(array) }
Output
Golang program to pass array in an argument. Printing the sum of all the numbers in the array. The sum of all the numbers in the array is 217.
Conclusion
These are the two examples of passing an array to the function where in the first one we have passed an array of type strings and print the values in alphabetical order. In the second one, we passed an array of type integers and printed the sum of all the numbers in the array. To learn more about Go you can explore these tutorials.