So far we have touched on several important points that you should know before venturing into the use of functions, since functions are structurally composed of all the elements that we saw previously:
Functions are pieces of code that do something, a particular task; Unlike Kotlin, where we define a function with the fun keyword, in Swift we create a function with func; that is, we put an extra 'c' to our reserved word that refers to the definition of a function in Swift:
func hola_mundo() {
}
As you can see, in the previous lines we defined our first function in Switf, where the body of the function is defined by the content that includes the braces {}, which in this example is a simple impression; to call or execute the function we must do the following operation:
hola_mundo()
Although for our really unexciting example it doesn't do anything, in the console you won't see any response coming from this function because at the moment it's a function that has nothing defined in its body or structure.
Parameter passing in functions in Swift
The previous format is the simplest thing we can do to execute a function, it can also receive what are known as parameters, which is essentially a definition of a variable indicating the data type:
func hola_mundo(name:String) {
}
And from the invocation:
hola_mundo("Mi primera funci贸n con Swift");
But if we do this it will give us an error as we can see in the following image:
Since we have to specify what the label would be before placing our value, that is, the name of the function parameter:
func hola_mundo(name:String) {
print(name)
}
hola_mundo(name:"Mi primera funci贸n con Swift");
This is an important difference that Swift has with Kotlin, since in the latter specifying the label or name of a function parameter is completely optional.
In addition to this, we are going to add a console printout to display our message:
Default values for function parameters
We can also establish what would be default values in the parameters of the functions;to do this we use the following syntax:
func hola_mundo(name:String = "Andres") {
print(name)
}
hola_mundo()
As you can see, parameter passing is now optional:
And we're going to go a little further and concatenate a full text into our printout:
Also remember that we can interpolate the texts, as we explained in the previous Swift post:
func hola_mundo(name:String = "Andres") {
print("Hola \(name) como estas?")
}
hola_mundo()
Function return values: add two numbers in swift
Of course, like any function in Swift we can indicate a return value, for that we use the arrow function followed by the data type to return; and of course, the typical return with the returned value:
func suma(a:Int, b:Int = 5) -> Int {
return a + b
}
let a = 4
let b = 4
suma(a:a,b:b)
In our sum function of two values, we see the following result:
And to show a slightly more complete example, we present the same function above but with String interpolation or text strings:
func suma(a:Int, b:Int = 5) -> Int {
return a + b
}
let a = 4
let b = 4
print(" Suma de \(a) + \(b) = \()");
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter