Functions – Swift 4

A Function is a block of statement and logics that together perform a specific task. A function that expects set of inputs called parameter and produce the output called result.

For example, a function expects a two input value a & b and produce the sum of a & b as output.

Let’s discuss one by one above code snippet:

  1. func: The function declaration always starts with func keyword.
  2. sumOfNumber: Name of the function always followed by the func keyword. This name used in order to call the function.
  3. (_ a: Int, _ b: Int) : Function parameter always follow by function name here is sumOfNumber. Function parameters follow the specific format i.e. name of the parameter, a colon, and the type of the parameter.
  4. ->Int : Type of the function return value.
  5. In the curly braces body of function which is the block of code and logics.
  6. return: in the above point 4, function return an Int value so you should return a value that is the type of Int, here is c (sum of a & b).

A function without a return type:

A function may be declared to return no value. There are three type of declaration for a function without a return type:

1. Function returning void:

2. Function returning (), an empty pair of parenthesis:

3. The last and mostly use, omit the arrow->operator:

A function without any parameter:

The parameter list of function declaration can be empty.

Above example show that you cannot omit the parentheses in function declaration and function call when function not taken any parameter. This function calling is too simple:

Call the test function and note that parentheses are must.

External Parameter Name:

The external parameter name appears in a call of the function as labels.

Let’s take a example:

There are several things to know in the above example:

  1. It clarifies the purpose of each argument. In the example (science, maths) is the external parameter name. It is easy to identify the behavior of the parameter, like the first parameter, take science marks and the second parameter take math’s marks.

Note: Here a and b is internal parameter name.

  1. It distinguishes the two functions. If two functions with same name and same parameter but different external parameter name are a different function. Let’s take another function with the same name of above function:

Both are different function.

 

  1. All the parameter externalized automatically, you can use the internal name as external parameter name. For example

here a, b is internal name and work as an external parameter name.

 

  1. We can suppress the external name of a parameter with an underscore. For example

Here we remove the external parameter name.

 

Example 2:

In the first parameter external parameter name student and in the second parameter we suppress the external parameter.

Important: We can’t use the external name into the function body. It just uses for enhancing the developer understanding.

 

Default Parameter in function:

We can use the default value for a parameter. This means that supply no argument for it. Let’s take an example:

Here marks has a default value 90, this mean that If you do not provide any value at the time of function call, it takes the default value of marks. In simple word, you can call the function in two manners:

  1. Provide a value of marks

  1. Use default value of marks

In this case, the function uses the default value of marks, here 90.

 

Variadic Parameter in function:

The variadic parameter means that you can pass as many values separated by a comma in the parameter. The function body will receive this value as an array. The indication of this parameter is three dots (…), which is followed by the parameter type.

Let’s take an example:

So, How to call the function:

Here is the sumOfNumber take a varadic parameter of type Int. So we passed the group of numbers.

Note: A function can have a maximum of one variadic parameter.

 

Ignored Parameters:

A Parameter can be ignored using his internal name underscore. For example:

Here is the isPass is ignored parameter. How to call this:

the isPass parameter does not use within the function body but you have to provide the ignore parameter.

 

in-out Parameter:

This is the most interesting among all. For understanding let’s take an example:

Is this function correct?

I think no because by default the parameter declares with let means constant value, you can’t change its value.

So, how to solve this problem?

Method 1:

You can declare a local variable inside the function body and assign a parameter value to it.

The local variable name can be same as the parameter name. According to your requirement, you can change a local variable name.

Problem:

Let’s call the function:

According to the function body, we have to change the value of maths marks. But we found that this is change locally means only in the function body not globally. Outside of the function value of maths marks remains same. But we also want to modify the value of maths marks outside of the function body. In the next method, you will solve this problem using in-out parameter.

Method 2:

To solve the problem of method 1 we use the inout parameter. Let’s take above example:

Notice that here we do not define a local variable and we directly change the value of maths marks. Because we have defined its parameter to inout, that means we can change its value inside the body of a function.

Does the value of maths marks also change outside of the function?

Let’s call the function:

 

Two things here you should notice that:

  1. I define the maths value as a var not let because we want to change the value of maths marks so it defines variable, not constant.
  2. I pass the address of maths marks rather than passing the variable as an argument. So I add an ampersand (&) with its name. This means that I pass the reference of math marks, not the value.

            Finally, I have to change the value of maths marks outside of the function body.

Function In Function:

A function can be declared inside the body of a function. Here is the scope of function within the same function.

Why and Where it is used?

Let’s take a example:

Here is the very simple example, in the performOperation function first I perform the addition operation on two number and also check the positive number, which in perform in another function isPositiveNumber. That’s the simple use of function in  a function. Several thing you should know about local function:

  1. Local function make the code clear and understandable.
  2. If the block of code repeats two or more times then you should use the local function.
  3. In example, whenever I need to check the number positivity then I call the isPositiveNumber function.

Function As a Value:

Several point you show should know about function:

  1. A function can be assigned to a variable.
  2. A function can be passed as an argument in a function call.
  3. A function can be returned as a the result of function.

If I want to use function as a value then I need to have function type.

So, What is function type?

Let’s take a example:

Here is a is type of Int, b is type of Int and its return a value of type Int. I can create a function type easily, Just remove the parameter names in function declaration and characterized the function by its inputs and outputs. So, the function type of sumOfNumbers function is:

The type of a function that take no parameter and no return value like:

then, the function type is:

2 Comments

Leave a Reply

Get free iOS Programming Tutorial with

Enter your email and stay on top of things,

Subscribe!