Q:

Swift program to return an array from a user-defined function

belongs to collection: Swift User-defined Functions Programs

0

Here, we will create a user-defined function that will accept an array as a parameter. Then we will return the modified array from the function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to return an array from a user-defined function is given below. The given program is compiled and executed successfully.

// Swift program to return an array 
// from a user-defined function

import Swift

func RetArr(arr:Array<Int>) -> Array<Int>{
var arr1 = arr

arr1.append(6)
arr1.append(7)
arr1.append(8)

return arr1
}

let arr:[Int] = [1,2,3,4,5]
var retArr = RetArr (arr:arr)

print(retArr)

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

...Program finished with exit code 0
Press ENTER to exit console.

Explanation:

In the above program, we imported a package Swift to use the print() function using the below statement,

import Swift;

Here, we created a user-defined function RetArr(). The RetArr() function accepts an integer array as a parameter and returns the modified array to the calling position and then we printed the result on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Swift program to create a function with default ar... >>
<< Swift program to pass an array into a user-defined...