Javatpoint Logo
Javatpoint Logo

Swift Function Parameter and Return Value

The Swift 4 user-defined function can have multiple parameters and different return values.

a) Functions with no parameter and no return value

The Swift 4 functions with no parameter and no return value do not take any input and return value.

Syntax:

Example:

Output:

Good Morning Everyone

b) Functions with no parameters but having return value

Example:

Output:

Good Morning Everyone

In the above program, the return type is String. Now, the statement must return a string from the statement inside the function, otherwise it will give an error.

The return keyword transfers control of the program from body of the function to the function call. To return value from the function, we have to add value after the return keyword.

c) Function with Parameter but having no return value

In Swift 4, some functions have parameters but don?t have return value. Parameters are used to take input in the function.

Syntax:

Example:

Output:

Good Morning Everyone

In the above program, the function accepts a single parameter of type String. The parameter can only be used inside the function. You can call the function by passing it a string value with the parameter name as greeting(msg: "Good Morning Evryone"). The msg parameter name is visible only inside the function greeting().

After that, the statement print(msg) gives the output Good Morning Everyone.

d) Functions with Parameter and Return Value

When a function takes parameters and also returns value.

Syntax:

Example:

Output:

Welcome to JavaTpoint Ajeet

You can see that in the above program, the function accepts a single parameter of type String and also returns value Welcome to JavaTpoint Ajeet of type String.

e) Function with multiple parameters and multiple return values

These functions are used to take multiple parameters separated by comma and return multiple return values. In Swift, tuples are used to return multiple values.

Syntax:

Example:

Output:

Hello Mr./Ms.Ajeet
You have 20 coins left in your wallet.

Next TopicSwift Recursion





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA