Swift Functions


Swift Functions – Task Units with Labels

A function in Swift is a named group of instructions that performs a particular job.Rather than duplicating code again and again, you wrap it into a function and call it whenever needed.


How to Build One

Start with func, then give it a title:

func displayMessage() {     
     print("Welcome aboard!") 
} 

Trigger it:

displayMessage()  // Prints: Welcome aboard!

Passing Inputs (Arguments)

You can send data into the block:

func sayHello(to person: String) {     
     print("Hello, \(person)!")
 } 

SayHello(to: "Liam")  // Output: Hello, Liam! 

Returning Results

Functions can give back outcomes:

func multiply(_ x: Int, by y: Int) -> Int {     
      return x * y 
} 

Let product = multiply(4, by: 2)  // product = 8 

Why They Matter

  • Improves structure
  • Promotes reuse
  • Simplifies updates
  • Enables modularity

Prefer Learning by Watching?

Watch these YouTube tutorials to understand GIT Tutorial visually:

What You'll Learn:
  • 📌 Swift - Functions
  • 📌 How to use Functions in Swift | Swift Basics #5
Previous Next