Introduction
You must have heard about the functions (or methods). Like in any other language, we have a method or say a way to enclose a block of statements under a single name. It’s a procedural way to make your code reusable. The name ‘Method’, ‘Subroutine’ and ‘Function’ are used interchangeably but, there is a little difference between them.
Methods are those functions, which belongs to some class (data member). Functions are quite generic. It is a function, which doesn’t belong to any class. Calling it ‘Method’ is wrong.
Function (say, Methods)
In Swift, you can declare your method, using func keyword. Call it by its name, as you do in any other language. It’s way simpler than Objective C, in terms of syntax.
- <code>
- func nameOfFunction(){
-
- }
- </code>
Parametrized Function
Functions are the ones, where we pass some values to the function as parameters. Its makes function most intelligent and dynamic.
- <code>
- func NameOFFunction(ParameterName:Type, ParameterName:Type)->ReturnType
- </code>
Here, we have a single parameter (i.e. name of the String type). Function takes the parameter and passes it to the block.
As you can see, you can pass the different value to the function and every time, functions behaves differently.
What will happen, when you have more than one parameter? In such cases, you have to declare the name of the parameter, while calling it.
If I want to pass age as a parameter, which is a Int type along with the name, the screen given below, appears-
As you can see, there is some warning in the calling part. Xcode suggests you to add name of the parameter, which you have passed, while declaring the function. After correction, it looks like-
It’s not required to name the first parameter. It is assumed, you have written in the function name itself. Its a part of Swift 2.x, where you can omit the first parameter name.
Function, which returns
There are some functions, which return the value, instead of having the result in the block. In Swift, we use (->) to denote the return type.
Default Parameter
What if you don’t pass any value to some parameter, then you can construct your function in such a way that it can take a default value, if nothing is passed to it.
If you don’t pass the age in the above test-case, screenshot is given below-
- <code>
- func NameOfFunction(ParamFirst:Type = DefaultValue){
-
- }
Basically, default parameter is used to pass an initial value, so that your function doesn’t behave ambiguously, when you don’t pass any value to it.
Here, we don’t have a default value for the any parameter. While calling the function, we just omitted one argument and it goes in the warning that ‘There is something missing’.
In the same scenario, if we add a default parameter, it will behave differently.
Because, here we have defined a default value. For the first parameter, it took passed argument but for second, it took the default value.
If you want to pass second argument, instead of the first, use the named-parameter to call the function.
Swift Series
- Introducing Swift Programming Language
- Control Statements In Swift Programming Language
- Loops In Swift Programming Language