Swift Functions

In this tutorial you will learn about the Swift Functions and its application with practical example.

Swift Functions

In Swift, function gives you way to wrap up the set of statements that means to perform any specific task and give it a name, so that it can be invoked later from any where in the program.

Functions makes it easy to divide the complete program into sub-units that perform a specific task for that program, this way it enhance the modular approach and increase the code re-usability of the program.

We pass information in function call as its parameter and the function can either returns some value to the point it where it called from or returns nothing.

Advantages of function

  • It enhance the modularity of the program.
  • It enhance the re usability.
  • It makes development easy as development can be shared in team.
  • It reduces the coupling.
  • It reduces duplication.

Type Of Function

  • Built in function
  • User defined functions

Swift Built In Function

Built in functions are the functions available in swift Programming Language to perform some common and standard tasks that includes the functions for file access, mathematical computations, graphics, memory management etc.

Swift User defined functions

User defined function are custom function defined by user it self to perform as custom task that is not available as built in, in this way user can define and write subprograms as functions to perform a task relevant to their programs.

Defining a function In Swift

In swift, a function can be declared using “func” keyword with list of required parameters and return type if any. Below is the general syntax of a swift function.

Syntax:-

func :- It is swift a keyword which is used to define a function.
fun_name :- It is replaced with the name of the function .
parameter_list :- It represents the list of the parameters need to be passed when function call made.
return_type :- It represents return type of the function.

Syntax:-

Swift function without return value.

func :- It is swift a keyword which is used to define a function.
fun_name :- It is replaced with the name of the function .
parameter_list :- It represents the list of the parameters need to be passed when function call made.

Functions without parameters without return value

Example:-

Output:-

Function with Multiple Parameters and return value

Example:-

Output:-

Function with Multiple Parameters and no return value

Example:-

Output:-

swift_function_sum_of_two_numbers

Swift Function with Multiple Return Values

In Swift, it is possible to return multiple values from a function in a single return statement by using tuple type as a function return type.

Syntax:-

Example:-

Output:-

swift_function_multiple_return_value

Swift Function with Default Parameter Values

In Swift, we can assign default parameter values in a function definition, so when we call that function with no argument passed for a parameter then its default value assigned. When a function is called with argument, then these arguments is used as parameter values in function, but when a function is called or invoked without passing argument for any specific parameters than default parameter values been used as parameters value as per function definition.

Syntax:-

Example :-

Output:-

swift_function_default_arguments

Swift External parameter names

In Swift, we can assign external name or alias for function parameters, so we can use them making a function call.By default local name is used as external parameter name for parameters, which you can change by defining external parameter names just before the local name.

Example:-

Output:-

swift_external_parameter_name

If you want to ignore the external name then you can do it using _ in before parameter name as following –

Example:-

Output:-

swift_ignore_external_parameter_name

Swift Variadic Function

A variadic function is a function that can accept variable number of arguments. In Swift, when are you not sure about the input parameter for a function then you have option to create a A variadic function. A variadic function allows us to You can pass zero or more parameters for a function. A variadic function can be declare using an ellipsis/three dots after type of parameter.

Example:-

Output:-

swift_variadic_function

Swift Assign Function to Variable

In Swift, a function can be assigned to a variable.In order to assign a function to a variable, variable must be parameter type and return type same as the function.

Example:-

Output:-

swift_assign_function_to_variable

Swift Nested Functions

In Swift, we are allowed to define a function inside another function. A function inside another function is called nested function. Nested functions can only be accessed by their enclosing functions.

Example:-

Output:-

swift_nested_function

In this tutorial we have learn about the Swift Functions and its application with practical example. I hope you will like this tutorial.