In this tutorial you will learn about the Swift Functions and its application with practical example.
Swift Functions
In Swift, function gives you way to wrap up the set of statements that means to perform any specific task and give it a name, so that it can be invoked later from any where in the program.
- Swift Functions
- Advantages of function
- Type Of Function
- Swift Built In Function
- Swift User defined functions
- Functions without parameters without return value
- Function with Multiple Parameters and return value
- Function with Multiple Parameters and no return value
- Swift Function with Multiple Return Values
- Swift Function with Default Parameter Values
- Swift Variadic Function
- Swift Assign Function to Variable
- Swift Nested Functions
Functions makes it easy to divide the complete program into sub-units that perform a specific task for that program, this way it enhance the modular approach and increase the code re-usability of the program.
We pass information in function call as its parameter and the function can either returns some value to the point it where it called from or returns nothing.
Advantages of function
- It enhance the modularity of the program.
- It enhance the re usability.
- It makes development easy as development can be shared in team.
- It reduces the coupling.
- It reduces duplication.
Type Of Function
- Built in function
- User defined functions
Swift Built In Function
Built in functions are the functions available in swift Programming Language to perform some common and standard tasks that includes the functions for file access, mathematical computations, graphics, memory management etc.
Swift User defined functions
User defined function are custom function defined by user it self to perform as custom task that is not available as built in, in this way user can define and write subprograms as functions to perform a task relevant to their programs.
Defining a function In Swift
In swift, a function can be declared using “func” keyword with list of required parameters and return type if any. Below is the general syntax of a swift function.
Syntax:-
1 2 3 4 |
func fun_name(parameter_list) -> return_type { statements return statement } |
func :- It is swift a keyword which is used to define a function.
fun_name :- It is replaced with the name of the function .
parameter_list :- It represents the list of the parameters need to be passed when function call made.
return_type :- It represents return type of the function.
Syntax:-
Swift function without return value.
1 2 3 |
func fun_name(parameter_list){ statements } |
func :- It is swift a keyword which is used to define a function.
fun_name :- It is replaced with the name of the function .
parameter_list :- It represents the list of the parameters need to be passed when function call made.
Functions without parameters without return value
Example:-
1 2 3 4 |
func sayHelloWorld() { print("Hello, World!") } sayHelloWorld() // Hello, World! |
Output:-
1 |
Hello, World! |
Function with Multiple Parameters and return value
Example:-
1 2 3 4 |
func sum( n1:Int, n2:Int) -> Int { return n1 + n2 } print(sum(n1: 3, n2: 2)) // 5 |
Output:-
1 |
5 |
Function with Multiple Parameters and no return value
Example:-
1 2 3 4 5 6 7 |
func sum( n1:Int, n2:Int){ var sum:Int sum = n1+n2 print("Sum of give no. is : \(sum)") } print("W3Adda - Swift function to print sum of two numbers") sum(n1: 3, n2: 2) |
Output:-
Swift Function with Multiple Return Values
In Swift, it is possible to return multiple values from a function in a single return statement by using tuple type as a function return type.
Syntax:-
1 2 3 |
func fun_name(param_list) -> (type1, type2...typeN) { return ( arg1, arg2...argN) } |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 |
func mathOps( n1:Int, n2:Int) -> (Int,Int) { var sum:Int var mul:Int sum = n1+n2 mul = n1*n2 return (sum, mul) } print("W3Adda - Swift function with multiple return value") var res = mathOps(n1: 3, n2: 2) print("Sum of given no. is :\(res.0)") print("Multiplication of given no. is :\(res.1)") |
Output:-
Swift Function with Default Parameter Values
In Swift, we can assign default parameter values in a function definition, so when we call that function with no argument passed for a parameter then its default value assigned. When a function is called with argument, then these arguments is used as parameter values in function, but when a function is called or invoked without passing argument for any specific parameters than default parameter values been used as parameters value as per function definition.
Syntax:-
1 2 3 |
func <fun_name>(<param1_name: param1_type,param2_name: param2_type = default_value>) -> returnType { // function body } |
Example :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* n1 : default argument is 5 n2 : default argument is 10 */ func addnum(n1:Int = 5, n2:Int = 10){ let result:Int = n1+n2 print("Sum of \(n1) and \(n2) is : \(result)") } print("W3Adda - Swift function with default arguments") // Call with all arguments addnum(n1:15,n2:25) // Call with some arguments addnum(n1:15) // Call with no arguments addnum() |
Output:-
Swift External parameter names
In Swift, we can assign external name or alias for function parameters, so we can use them making a function call.By default local name is used as external parameter name for parameters, which you can change by defining external parameter names just before the local name.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 |
/* Here, "n1" is external name and "a" is local name for first parameter "n2" is external name and "b" is local name for second parameter */ func addnum(n1 a:Int, n2 b:Int = 10){ let result:Int = a+b print("Sum of \(a) and \(b) is : \(result)") } print("W3Adda - Swift function with external parameter names") // Call with external parameter names addnum(n1:15,n2:25) |
Output:-
If you want to ignore the external name then you can do it using _ in before parameter name as following –
Example:-
1 2 3 4 5 6 7 |
func addnum(_ a:Int, _ b:Int = 10){ let result:Int = a+b print("Sum of \(a) and \(b) is : \(result)") } print("W3Adda - Swift function ignore external parameter names") // Call without external parameter names addnum(15,25) |
Output:-
Swift Variadic Function
A variadic function is a function that can accept variable number of arguments. In Swift, when are you not sure about the input parameter for a function then you have option to create a A variadic function. A variadic function allows us to You can pass zero or more parameters for a function. A variadic function can be declare using an ellipsis/three dots after type of parameter.
Example:-
1 2 3 4 5 6 7 8 9 |
func addnum(_ nums: Int...) -> Int { var sum: Int = 0 for num in nums { sum += num } return sum } print("W3Adda - Swift variadic function") print(addnum(10,11,12,13,14,15)) |
Output:-
Swift Assign Function to Variable
In Swift, a function can be assigned to a variable.In order to assign a function to a variable, variable must be parameter type and return type same as the function.
Example:-
1 2 3 4 5 6 7 |
func addnum( n1:Int, n2:Int) -> Int { return n1 + n2 } var sum: (Int, Int) -> Int = addnum var result = sum(10,25) print("W3Adda - Assign function to variable") print("Sum of give no. is : \(result)") |
Output:-
Swift Nested Functions
In Swift, we are allowed to define a function inside another function. A function inside another function is called nested function. Nested functions can only be accessed by their enclosing functions.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
func addnum(str:String) -> (Int,Int)->Int { func add(a:Int,b:Int)->Int { return a+b; } if str == "+" { return add; } return add } let sum = addnum(str:"+") print("W3Adda - Swift Nested Function") print(sum(10,25)) |
Output:-