Swift program to pass an array from the function

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to p ass an array to the function.

In Swift, a function is a set of statements that is used to solve some specific problem. It is defined using func keyword. We can call the function with the help of its name.

Function Declaration− It is used to tell compiler about function’s name, parameters and return types.

Function Definition− It represent the body of the function.

Syntax

  • Following is the syntax of a function −

func functionName(para1 : Type, para2 : Type)-> returnType{ 
   // Body 
}

An array is an ordered collection which is used to store same type of data. It can be mutable or immutable.

  • Following is the syntax of an array −

var myArray = [Type]() 
Or 
var myArray = [value1, value2, value3]

Swift allows you to pass an array in the function. To do so we just si mple declare a function with array type parameters. And at the time of the function calling we can pass the array to the function.

  • Following is the syntax −

func functionName(para : [Type])-> returnType{ 
   // Body 
}

Example 1

The following program shows how to pass an array to the function.

import Foundation import Glibc // Function which accept array as an argument func myFunction(arr: [String]) -> Int{ // Finding the total number // of elements in the array let countArr = arr.count // Returning the count return countArr } var a = ["Amu", "Mohit", "Sumit"] // Passing array to the function var myout = myFunction(arr: a) print("Array: ", a) print("The total number of elements present in the array:", myout)

Output

Array: ["Amu", "Mohit", "Sumit"]
The total number of elements present in the array: 3

Here, in the above code, we create a function named myFunction(). This function takes array as an argument and return the total number of element present in the given array. In the myFu nction(), we passed the array at the time of function calling: myFunction(arr: a) or myFunction(arr: ["Amu", "Mohit", "Sumit"]) and display the count that is 3.

Example 2

The following program shows how to pass an array to the function.

import Foundation import Glibc // Function which accept array as an argument func myFunction(arr: [Int]) { var mArr = arr print("Original array:", mArr) // Finding the total number // of elements in the array let countArr = mArr.count print("Count of the array:", countArr) // Reversing the array mArr.reverse() print("Reversed Array:", mArr) } // Passing array to the function myFunction(arr: [98, 32, 847, 12, 1, 45, 2])

Output

Original array: [98, 32, 847, 12, 1, 45, 2] 
Count of the array: 7 
Reversed Array: [2, 45, 1, 12, 847, 32, 98]

Here, in the above code, we create a function named myFunction(). This function takes array as an argument and return the total number of element present in the given array and reversed array In the myFunction(), we passed the array at the time of function calling: myFunction(arr: [ 98,32, 847, 12, 1, 45, 2]) and display the count that is 7 and reversed array that is [2, 45, 1, 12, 847, 32, 98].

raja
Updated on 10-Oct-2022 13:12:28

Advertisements