Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

swift
communitycreator

Create a function with no argument and return value in Swift

Theodore Kelechukwu Onyejiaku

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()
Create functions without Arguments and Return Value in Swift

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.

RELATED TAGS

swift
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring