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.
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.