Javatpoint Logo
Javatpoint Logo

Swift Functions

Functions are the set of codes which are used to perform a specific task. It Swift 4, a function is used to pass local as well as global parameter values inside the function calls.

Functions have a specific name which is used to "call" the function to perform its task when needed.

Swift 4 functions contain parameter type and its return types.

Types of Functions

Types of functions depend on whether a function is predefined or created by programmer. In Swift 4, there are two types of functions:

  1. Library functions (Built-in Functions) - Library functions are the type of functions which are defined already in Swift Framework.
  2. User-defined functions - User defined functions are created by the programmer themselves.

Library Functions

Library functions are the user-defined functions which are already defined in Swift framework. These functions are used to solve common problems and simple operations in Swift like printing, finding minimum and maximum, etc. so that you don't have to solve them yourselves.

Library functions can be used directly by invoking (calling) it. For example: print() function

You can see all the functions inside the Swift framework by importing Swift. Open command prompt and click on it. You will see a new page. Search all the statements starting with func keyword.


When we run the above program, it will show an output JavaTpoint. It is just because, we have invoked a print function which is already defined in Swift Framework. The function is used to print output.

User-defined Functions

Swift 4 facilitates you to define your own functions. User defined functions are mainly used to solve problems or perform tasks not available in Swift Framework. These functions can also be reused to perform similar tasks in the future.

Function Syntax

Parameter Explanation

func- This is a keyword which you have to write to create a function

function_name- It specifies the name of the function. You can give it any name that defines what a function does.

args... It defines the input a function accepts.

-> This operator is used to indicate the return type of a function.

ReturnType- It defines the type of a value you can return from a function. E.g. Int, String etc.

return- It is a keyword used to transfer the control of a program to the function call and also return value from a function. Even if you don't specify the return keyword the function returns automatically after execution of last statement.

value- It represents the actual data being returned from the function. The value type must match the ReturnType.

How to define function in Swift 4?

We can execute a function by calling the function's name. When we define a new function, it may take one or many values as input parameters to the functions and it will process the functions in the main body and pass back the values to the functions as output 'return types'.


Explanation of the above function definition:

  • Keyword func specifies the start of function header.
  • Wish specifies the function name to uniquely identify and call function in the program.
  • (user:String) specifies the end of function header and accepts a parameter of type String.
  • The function consists of a print statement inside the body which executes after you call the function.

How to call function in Swift 4?



Good Morning Ajeet. Have a Good day.

In the above program, wish(user: "Ajeet") calls the function and passes a String type value Ajeet. It returns "Good Morning! \(user). Have a Good day" statement of type String and transfers the program to the function call. After that print statement executes inside the function.

Note: Always use a meaningful name of the function which reflects the purpose of the function. Make a function to do only one task. If a function does more than one task, break the function into multiple functions.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA