Swift - Function Functions

Introduction

Swift functions perform tasks with data.

Functions organizes your code into small, repeatable chunks, like so:

Demo

func sayHello() { 
    print("Hello") 
} 
sayHello()//from w  ww. j a  v  a 2 s.c o m

Result

Functions can return a value to the code that calls them.

When you define a function with return type, you must indicate the type by using the arrow -> symbol.

Demo

func usefulNumber() -> Int { 
    return 123 //from   w w w .jav  a2  s. co  m
} 

let anUsefulNumber = usefulNumber() // 123 
print(anUsefulNumber)

Result

Related Topics

Exercise