Mastering in Swift Functions

Functions that solve complex problems

Sridharan T
IVYMobility TechBytes
5 min readApr 1, 2020

--

Recursive Functions

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 infinitely.

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

In the above flow diagram, the recursion executes infinitely. However, it can be stopped when it meets with a condition.

Example :

When you run the above program, the output will be:

The above-discussed topics are basics and commonly known to all. Let see new things which were in Swift and some other languages.

Parameter Labels

We wrote our greet( ) function like this

Here we use the parameter name as the user, so we can use the same name inside the function to refer to it. But we must also use the name when running the function, like this:

Swift provides us a new concept where we can use two names for each parameter.

  • One to be used externally when calling the function.
  • Other to be used internally inside the function.

Here is an example that uses two names for its string parameter :

The parameter is called to user, which means externally it’s called to, but internally it’s called name.

If you don’t want an argument label for a parameter, write an underscore ( _ ) instead of an explicit argument label for that parameter.

Example :

So you don’t need to mention the parameter name while calling the function.

Default Parameters

You can define a default value for any parameter in a function by assigning a value to the parameter after that parameter’s type. If a default value is defined, you can omit that parameter when calling the function.

Example :

Here when you call greet(“Isac”), you don’t need to mention the second parameter. Because it has the default value which will be invoked during the execution. The print statement will display the output with default value as

But when greet(“Peter”, value : 2) is called. It will change the parameter value to the new value only for that function call. When you run the program, the output will be:

When you run the program again with parameter as greet(“Axe”), it will display the output with a default value.

Variadic Functions

A variadic parameter accepts zero or more values of a specified type. You use a variadic parameter to specify that the parameter can be passed a varying number of input values when the function is called.

  • You can make any parameter variadic by writing after its type. So, an Int parameter is a single integer, whereas Int… is zero or more integers.
  • The values passed to a variadic parameter are made available within the function’s body as an array of the appropriate type.
  • A function can have at most one variadic parameter.

For example, a variadic parameter with a name of numbers and a type of Int… is made available within the function’s body as a constant array called of numbers type [Int].

Example :

The output will be :

In-Out Parameters

All parameters passed into a Swift function are constants, so you can’t change them. If you want, you can pass in one or more parameters as inout, which means they can be changed inside your function, and those changes reflect in the original value outside the function.

Example :

The example above shows that the original values of someInt and anotherInt are modified by the swapTwoInts( ) function. You also need to pass the parameter to swapTwoInts( ) using an ampersand, &, before its name, which is an explicit recognition that you’re aware it is being used as inout.

Nested Functions

A function inside the body of another function is called a nested function.

Syntax :

Here the function anotherFuncname is the nested function, present inside the function funcname.

Example :

When you run the program, the output will be:

In the above program, the nested function addMessageAndGreet( ) is being called from the function greet( ).

The statement greet(user: “Isac”) calls the outer function. And the statement addMessageAndGreet( ) inside the outer function calls the method which outputs Hello, Isac in the console.

Keys to remember :

  • We can use parameter labels as names of parameters- internally and externally or omit the external name.
  • Parameters can have default values which is helpful to reduce the lines when we know specific values are the same.
  • Variadic parameters accept zero or more values of a specified type.
  • Using inout we can change variables inside the function.

“In software, the most beautiful code, the most beautiful functions, and the most beautiful programs are sometimes not there at all.” — Jon Bentley

Find it a good read?

Recommend this post (by clicking 👏 button) so other people can see it too…
reach me on Twitter Sridharan T

--

--