iOS

iOS Lesson 05 – Methods OR Functions in Swift

So with this knowledge bit, we are going to talk about Functions or methods. People use both terms but in reality, they are the same.

Functions in Swift

Open up your Xcode and create a new playground. We are going to call this one “FunctionsOrMethods” and delete the Hello world line.

 

Functions in Swift

 

Function/Method:

Functions are core building blocks in programming applications that can actually do anything. The perfect example of why do we even need to use a function is let’s say I want to get an area of a Rectangle/square or any other shape that has some length and width. So to calculate the area we would multiply length width to get the area and with all the knowledge we have got up to this point from these knowledge bit series we would have written the code for calculating the area as something like this.

 

IOS lesson: Functions in Swift

 

and probably you would have something similar than this, wrote some variables, did some math operations and we now have the area right?

So now we have a shape but what if we have multiple shapes to determine the area. If you still come up with the same idea as before ”Write the same variables and do the arithmetic operation again”, that is fine BUT what if there is a third shape and you again come up with the same idea about writing new variables and doing the arithmetic operation then you should have already realized that there is something wrong or incomplete about the knowledge of swift because the current code convention (Write new variable, Do arithmetic operation every time) is violating the DRY principle that is “Don’t repeat yourself” and for any body who is doing programing can realize that this is not the best way to write this solution. This is where Functions/Methods come into place.

They are a way to repeat an operation with getting data and returning solutions.”

They take some input (Data) manipulate the data and gives some output (optional). So we have enough theory for now. Let’s get some hands-on functions in swift

Now, Let’s create a function. In Swift, we use “func” keyword to define the function and then the identifier (name) for the function. We are going to call this ”CalculateArea”

Code:

 


func CalculateShapeArea()
{
}

Now we have a function and its ready to do something but it is not ready to accept any input and to do that we are going to do is we are going to put two parameters in the round braces after the function name. we are going to give length and width as the parameters with the same way of declaration as we did for the variables.

Code:


length:Int

width:Int

 

Parameters are input that indicates what data will be given to a function at runtime and we have explicitly declared a datatype for parameters. For now, our function can take some input but it can not do anything yet as we haven’t defined any operations inside the functions or what it will be going to return (output) so we are going to do exactly the same thing. The way we can define the return output is by using a dash and right arrow ” -> (DATATYPE)” so we are going to put a return type in our function after the parameters. The whole declaration will look something like this.

Code:

func CalculateShapeArea(length:Int,width:Int) -> Int {

Now as we have the declaration ready we can start working on the operations inside the function and to do that we can use a constant in our code by using the keyword “let” so write down the following code inside your function.

 

Code:


let area = length * width

return area

you would probably be like what does it mean?? It’s really simple. We have created a variable(Constant) whose only work is to hold the value of multiplication of parameters (length and width). In the next line we have written the keyword “return” and then the variable name “area” it will be going to return the calculated area. You would have noticed that we are not getting any result in the ride side of Xcode as we were getting previously with other variables and exercises and its because we haven’t put our functions to action till yet.

Let’s try that out!!!

Copy the line of code into your Xcode to fire up that function

Code:



let newArea = CalculateShapeArea(length: 20, width: 50)

we have created a new constant with the keyword “let” then we are giving a name to our constant and then we are calling our function and we are passing 20 as the length its taking the parameter length and width and then it gets back to functions body then it performs the mathematic operation then it stores the result in the “area” constant that we have defined inside the function’s body and then using the return keyword it returns the calculated area constant that gets saved In our “newArea” variable. To sum it up in simple words.

We give it length and width, it performs an operation to calculate the area and it gives the area back that we saved in our “newArea” variable.

It doesn’t matter how many shapes we have for now we can calculate the area of any shape as long as we have its length and width.

 

This is the syntax of the function. You start with the func keyword then you give it a name then you have to put parenthesis. You really don’t have to have the parameters, as they are optional. The best practice in the very start is “take inputs, do processing, returns output without necessarily modifying your variables or properties” if it doesn’t make any sense for now then that’s absolutely fine because when you’ll do some practice it will start making more sense.

Similarly, do some practice in making custom methods with different parameters and return types to play with swift. There are a lot of concepts related to functions that will be covered in the coming knowledge bits.

So in this knowledge bit, we have covered numbers that you are going to use all the time. You can get all the code from here.

Please Like, share and subscribe for more!

 

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular

To Top