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 .  ja 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 //ww w.ja v a 2 s  .  co  m
} 

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

Result

Related Topics

Exercise