Functions in Swift

Updated 31 May 2022

Save

Functions are groups of codes that perform a specific task. We give a function a name that identifies what it does. We can use this name to use the codes it contains. Or in other words, we call the functions with that name to perform its task whenever we want.

There are many types of functions like Functions Without Parameters, Functions With Multiple Parameters, Functions Without Return Values, Functions with Multiple Return Values

Defining and Calling Functions

While defining a function, we can define one or more named, typed values that the function takes as input, known as parameters. We can also define a type of value that the function will return as output when it will complete its task, known as its return type.

Every function has a name, which describes the code/task that the function will perform. To use the function’s code, we call the function by its name and pass the input values it requires. We call these value as arguments, that should have the same type as that of the function’s paraments. A function’s arguments should be in the order same as the function’s parameter.

The function in the example below is called play(game:), because that’s what it does—it takes a game’s name as input and returns a playing of that game. To accomplish this, you define one input parameter—a String value called game—and a return type of String, which will contain a playing of that game:

All of this information is wraped up in the function’s definition, which is prefixed with the func keyword. We indicate the function’s return type with the return arrow -> (a hyphen followed by a right angle bracket), which is followed by the name of the type to return.

The definition describes what a function will do. Also what are the inputs for this, which means what it expects to recieve. And it also describes the output, in other words, what will it return when it will complete its task. We can call the function with the help of definition, unambiguously from elsewhere in your code:

We call the play(game:) function by passing a String value after the game argument, such as play(game: "Cricket").

Functions Parameters and Return Values

Function parameters and return values are flexible in Swift. We can define anything from a simple utility function with a single unnamed parameter to a complex function with expressive parameter names and different parameter options.

Functions Without Parameters

It is not compulsory to define the input parameters. Below defined a function with no input parameters, which will return the same String message when called:

The function definition still needs a parentheses after the function’s name, even if it doesn’t take any parameters. When we call the function, there should be a pair of parentheses after the function’s name.

Functions With Multiple Parameters

Functions can also have multiple parameters as input, we use parentheses, saparated by commas to wirte these parameters. This function takes person’s name and the game they are playing as input and returns a String whatever that person is playing.

We call the playing(person:game:) function by passing it two String arguments in parenthesis, saparated by commas.

Functions With Return Values

We can set an output of the function, in other words, when we call the function, it returns a value. There can be single to multiple returns values. In the below exaple, we can see the function playing(person:game:) returns a string value.

The function playing(person:game:) returns a string value, we can save this value in a variable and use whenever required or we can even ignore the return value of the function.

Conclusion

In this blog, we discussed about the function in swift, calling a function and its types. You can learn more about function in swift from here.

You can read more of my blogs from this link.

author
. . .

Leave a Comment

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


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home