top of page
Home: Blog2
Home: Instagram_Widget

Functions in Swift

Updated: May 17, 2020

What is a function in swift?

I like to think of functions as small little macros like we use in excel, another way to look at it would be commands which executes a small block of code.


When should you use a function?

You would create functions to eliminate repetitive tasks, or repetitive lines of codes which do the same thing So imagine there is an activity which you need to do every time something happens. In such cases with the use of functions you would write some lines of code once and then call that function whenever you needed to run the same lines of codes but with a new parameter.


Whats the Function syntax?

func functionName(parameterList) -> ReturnType {
return returnValue
}

How to create a function?

When creating a function you first should come up with a name and then what it should do.

Using the example below, I have written up a function which has some inputs:

  • The name of the function is addUsers

  • The functions looks at executing a series of commands to add users to an existing Array (the code for the earlier declarations of variables have been left out here in this code snippet to focus on the function aspect.

  • The function then returns -> a String, which is "user successfully created"

func addUsers(addName: String, addPassword: Int) -> String {
 registeredUsers.append(addName)
 registeredPassword.append(addPassword)
 return "user successfully added"
}

Calling a function

Once you have created a function you can call the function at anytime so long your calling is within the scope. The scope determines when a function can be accessed and be used. Always keep your coding indented so that you know when your function can be accessed.


you can call the above function by entering the name addUsers on a new line and the rest of the input parameters should be auto displayed. addName and addPassword are the parameters.

addUsers(addName: <String>, addPassword: <Int>)

You then replace the above value types (in tech speak this is called the argument) and the you can execute the function. The argument is the value of the input for the parameter.

addUsers(addName: "bobby", addPassword: 111)

So at this point the function has been successfully called and the user has been added to the Array.


Noteworthy tips👌

  • Take note of the data types which have been assigned in the function. You can change the data type when calling a function.

  • The argument is the value of the input for the parameter. So it makes sense to give your parameter a meaningful name as a beginner so that you can logically make sense of the code in your head.

  • Writing many functions is a good thing for a beginner. I spent a lot of time on this area to get comfortable. You can break your code into multiple functions for starters and then take it from there.

  • Functions can be nested, these nested functions due to scope have access to variables that were declared in the outer function.

  • Swift defines functions are a first-class type, this means that. function can return another function as its value.

  • Functions are actually a special case of closures - blocks of code like I mentioned which can be called later.

  • Swift function can accept multiple input parameters but can only return one value.

  • A way to return multiple values from a function is to use in out parameters, which allows a function to change the value of an input parameter within that function

  • While there are valid use cases for changing out parameter values, returning a tuple has the advantage if returning a value type - rather than modifying input values.

  • A function can be one where you dont have to have a return value. There are 3 ways to do this. Return as a void, (0, or just remove the arrow operator. So when you omit the return value you should also remove the return statement in your function.

  • A function need not have any input parameters, but you cant omit the parameter list brackers i.e (). When making a function call you have to include the ()


Thank you for reading through this. I hope my layman explanation of a function made some sense to you. Do subscribe if you would like to see more updates such as this. Its a great motivator for me to keep going as I share my learnings with you.


Comments


Home: Subscribe

©2019 by CodingWithSuj. Proudly created with Wix.com

bottom of page