2
2
.
.
5
5
.
.
1
1
F
F
u
u
n
n
c
c
t
t
i
i
o
o
n
n
s
s
I
I
n
n
f
f
o
o
[
[
R
R
]
]
Function is a group of statements that can be called with Input Parameters and that can return a Value.
Each Function has Signature defines
Data Types of Input Parameters (name:String, age:Int)
Data Type of Return Value (String)
Arrow "->" inside Signature symbolizes that these Input Parameters are converted into this Output Parameter.
Function is called by providing Input Parameters in the exact order in which they were declared.
Usually by specifying their External or Internal Names as discussed in Function Input Parameters.
Since Functions can be stored inside Variables or given as Input Parameters into other Functions, Signature is used to say
what kind of Functions can be stored into a Variable or given as Input Parameter into other Functions.
For instance var name : Int says that this Variable can store Int Data Type.
Similarly var name : (name:String, age:Int) -> (String) says that this Variable can store Function that accepts String & Int as
Input Parameters and returns String.
Content
Function Syntax (Similar to Closures)
Function Input Parameters (Parameters are given as local copies)
Function Input-Output Parameters (Parameters are given as a references to original Variables)
Function Return Value (Return single Variable or Tuple or use Input-Output Parameters)
S
S
y
y
n
n
t
t
a
a
x
x
Function is declared by
using Keyword func func
followed by Function's Name greet
followed by Function's Signature (name:String, age:Int) -> (String) (accepts two Input Parameters & returns String)
followed by Function's Body { return("\(name) is \(age) years old.") }
Syntax
//DECLARE & IMPLEMENT FUNCTION.
func greet (name:String, age:Int) -> (String) { return("\(name) is \(age) years old.") }
//CALL FUNCTION.
var result = greet(name: "John", age:50)
//DISPLAY RESULT.
print(result)
F
F
u
u
n
n
c
c
t
t
i
i
o
o
n
n
I
I
n
n
p
p
u
u
t
t
P
P
a
a
r
r
a
a
m
m
e
e
t
t
e
e
r
r
s
s
Function Input Parameters have Internal and External Names
Internal Parameter Names are used inside a Function Body to reference Input Parameters ageInt:Int.
They are part of Function Implementation and therefore not necessarily what should be exposed to outside Users.
You are required to specify them during Function declaration (otherwise Body couldn't reference them).
External Parameter Names are used when calling a Function nameExt nameInt:String.
They are supposed to be more user friendly compared to their equivalent Internal Parameter Names. Their
purpose is to make it more clear to Programmer what each Parameter does.
Specifying External Parameter Names during Function declaration is optional.
If External Parameter Name is not defined, Internal Parameter Name will be used by default as External Parameter
Name for that Parameter. In other words in such scenario Internal Parameter Names are also used as External
Parameter Names. This is why specifying them is optional since we can always use Internal Parameter Names.
Even though Input Parameters have Names, when you call the Function you still need to specify Parameters in the
exact order in which they were declared. In other words in Swift, External Parameter Names are not used to tell
compiler which Value goes into which Parameter during Function Call. Swift already knows that based on the order
in which Values are given during Function Call. Instead, during a Function Call External Parameter Names are just
used to clarify to Programmer what is the purpose of each Parameter (External Parameter Names are completely
useless to the Compiler). Which is why you can use underscore '_' to omit specifying External Parameter Names
during Function Call and Compiler will still know which Value goes into which Input Parameter simply based on the
order in which Values are given.
When declaring a Function, in the place of External Parameter Name you can type underscore '_' _ heightInt: Int.
This means that during Function Call External Parameter Name must be omitted (only Parameter Value should be
provided). This is how in Swift you can have Function Calls where only Input Parameter Values are provided without
specifying their names as is the case is many other Languages.
Function Parameters can also have default values weightInt: Int = 80.
Such parameters can be omitted during Function Call allowing us to have Functions with Variable number of Parameters.
E
E
x
x
a
a
m
m
p
p
l
l
e
e
In this example Internal Parameter Names are: nameIn, ageInt, heightInt & weightInt (Used inside the Function Body)
For first Parameter we have defined External Parameter Name nameExt which is used during Function Call.
For second Parameter no External Name is defined, so its Internal Name is used during Function Call.
For third Parameter underscore '_' is used, so no External Name should be used during Function Call.
For forth Parameter default Value is given so this Parameter doesn't need to be used during Function Call.
Function Input Parameters
//DECLARE FUNCTION.
func greet (nameInt:String, ageExt ageInt:Int, _ heightInt: Int, weightInt: Int = 80) -> () {
print("\(nameInt) is \(ageInt) years old, \(heightInt) cm tall and has \(weightInt) kg.")
}
//CALL FUNCTION.
var result = greet(nameInt: "John", ageExt:50, 170)
//DISPLAY RESULT.
print(result)
Output
John is 50 years old, 170 cm tall and has 80 kg.
F
F
u
u
n
n
c
c
t
t
i
i
o
o
n
n
I
I
n
n
p
p
u
u
t
t
-
-
O
O
u
u
t
t
p
p
u
u
t
t
P
P
a
a
r
r
a
a
m
m
e
e
t
t
e
e
r
r
s
s
In Swift Function Parameters can also be used to return values from the Function and they can be used
just as placeholders for return value
or they can also provide some input value into Function and then be rewritten with some return value
Input-Output Parameters are used
by using Keyword inout when declaring Input Parameter
by giving a reference to a Variable &name when calling a Function so that Function can refer to the original Variable
instead of working with a local copy (as is the case with normal Input Parameters)
E
E
x
x
a
a
m
m
p
p
l
l
e
e
In this example Input Parameter name is declared as inout Parameter which means that it accepts reference to a Variable.
When Function is called this Input Parameter is given Reference to a person Variable by using & before the person.
From that moment onward Variables person and name become one and the same Variable.
They become bound to each other (they have become synchronized).
You can think of name Variable as an Alias for a person Variable.
They both point to the same memory location so any changes done to name Variable are reflected to person Variable.
Input-Output Parameters
//DECLARE FUNCTION.
func greet (name: inout String) -> () {
name = "John"
}
//CALL FUNCTION.
var person = "Unknown"
greet(name: &person)
//DISPLAY CHANGED NAME.
print(person)
Output
John
F
F
u
u
n
n
c
c
t
t
i
i
o
o
n
n
R
R
e
e
t
t
u
u
r
r
n
n
V
V
a
a
l
l
u
u
e
e
Functions only have a single Return Value. Return Value Data Type is part of their Signature.
To return multiple Values from a Function
Function can return Tuple which is Collection of Values
Function can return Instance of an Struct or Class
Function can use Input-Output Parameters to effectively return multiple Values.
R
R
e
e
t
t
u
u
r
r
n
n
S
S
t
t
r
r
i
i
n
n
g
g
In this example Function returns a String. Function has no Input Parameters since the first bracket is empty.
Return String
//DECLARE FUNCTION.
func test () -> (String) {
return("Hello")
}
//CALL FUNCTION.
var result = test()
//DISPLAY RETURN VALUE.
print(result)
R
R
e
e
t
t
u
u
r
r
n
n
T
T
u
u
p
p
l
l
e
e
In this example Function returns a Tuple which is collection of Variable.
When Function is called, Tuple Values can be immediately stored into separate Variables.
Or you can store returned Tuple into another Tuple Variable.
Tuple is a simpler alternative compared to struct since struct needs to be declared before you can use it.
Think of Tuple as a simplified temporary struct for a more convenient way of returning multiple values.
Return Tuple
//DECLARE FUNCTION.
func test() -> (name: String, age: Int) {
return("John", 50)
}
//CALL FUNCTION.
var (nameRet, ageRet) = test() //Store Tuple values into Variables.
var result = test() //Store Tuple into another Tuple.
//DISPLAY RETURN VALUE.
print(result.0) //Access Tuple values through their indexes.
R
R
e
e
t
t
u
u
r
r
n
n
s
s
t
t
r
r
u
u
c
c
t
t
In this example Function returns a struct with name and age Properties.
Since struct can contain any number of Variables/ Properties you can use this approach to effectively return multiple
values from a Function even though you are just returning a single Instance of that struct.
Return struct
//DECLARE STRUCT.
struct Person {
var name : String
var age : Int
}
//DECLARE FUNCTION.
func test() -> (Person) {
var person = Person(name: "John", age: 20)
return(person)
}
//CALL FUNCTION.
var person = test()
//DISPLAY RETURN VALUE.
print(person)