Create a function with no argument and return value in Swift

Overview

A function that takes a no argument and returns a value in Swift can be created easily. They perform a function when we call these functions, but they take no argument and return no value.

Syntax

funct functionName(){
// perform function here
}
Syntax for creating a function without argument and no return value

Parameters

functionName: This is the name for the function we want to create.

Example

import Swift
// create functions that take no argument and return value
func greetUser() {
print("Hi Welcome to Edpresso!!!")
}
func printEvenNumbersToTen(){
print("2, 4, 6, 8, 10")
}
func aboutSwift(){
print("Swift is a good programming language!")
}
func printValueOfPi(){
print("The value of Pi is \(Double.pi)")
}
// invoke the functions
greetUser()
printEvenNumbersToTen()
printValueOfPi()

Explanation

  • Lines 4–16: We create some functions that do not take any arguments and return no values.
  • Lines 21–23: We call the functions we have created.

Free Resources