Swift Programming

Swift continue Statement

While you are working with loops or conditional statements, you may require to skip some statements inside the loop or terminate the loop immediately without checking the test expression.

In such cases, break and continue statements are used. Visit the page to learn more about break statement.

The continue statement stops the execution of statements inside a loop or a switch statement. It then skips back to evaluate the loop's test expression again.

Swift break Statement

When you are working with loops or conditional statements, you may require to skip some statements inside the loop or terminate the loop immediately without checking the test expression

In such cases, break and continue statements are used. You will learn about continue statement in the next chapter.

The break statement stops the execution of a loop or a switch statement. It then jumps to the next statement following the loop or switch statement.

Swift Nested Functions

If a function exists inside the body of another function, it's called nested function.

Syntax of a nested function

func funcname() {
    //statements of outer function
    func anotherFuncname() {
        //statements of inner function
    }
}

Here, the function anotherFuncname is inside the body of another function funcname.

Swift Recursion

A function that calls itself is known as a recursive function. And, this technique is known as recursion. While creating a recursive function, you must create a condition so that the function does not call itself indefinitely (infinitely).

How recursion works in Swift?

func recurse() {
    //statements
    recurse()
}
recurse()

The figure below shows how recursion works by calling itself over and over again.

Swift Function Parameters and Return Values

In the previous article Swift Functions, we learned about functions. Now, we'll look at the different ways and types we can create a function in Swift, i.e. handling input and output in a function.

Function with no parameter and no return value

These type of functions do not take any input and return value.

Swift Functions

What is a function?

A function is a group of statements that defines an action to be performed. The main use of a function is to make the code reusable.

Non technically, you can think of a function as a machine. A machine performs a specific task, asks for input, processes the input and returns the output.


Types of Functions

Depending on whether a function is predefined or created by programmer; there are two types of function:

Swift Closures

In the article Swift functions, we created a function using func keyword. However, there is another special type of function in Swift, known as closures that can be defined without using keyword func and a function name.

Like functions, closures can accept parameters and return values. It also contains a set of statements which executes after you call it and can be assigned to a variable/constant as functions.

Swift Function Overloading

Two or more functions having same name but different argument(s) are known as overloaded functions.

Why do we need function overloading?

Imagine you are developing a shooter game where the player can attack its enemies using a knife, a blade and a gun. Your solution for the attack functionality might be defining the actions into functions as:

Swift Dictionary

In the previous Swift Arrays article, we learned how we can store multiple values in a variable/constant. In this article, we are going to discuss how we can store data/values as key value pairs.

What is a dictionary?

A dictionary is simply a container that can hold multiple data as key-value pair in an unordered way.

Swift Arrays

In the previous Swift Data Types article, we learned about creating variables/constants of some Data Type that can hold a single value.

But, what if we want to store multiple values of the same data type. We use something called Array in Swift.

Pages