Chapter 71: Functions
1. What is a function? (the most honest explanation)
A function is a named piece of reusable code that:
- takes zero or more inputs (parameters)
- does some work
- optionally returns a result
Think of a function as a small worker inside your program:
- You give it instructions (parameters)
- It does its job
- It hands back a result (or nothing)
Functions are the building blocks of every non-trivial program.
2. The basic syntax — all the common forms
Form 1 – Simplest function (no parameters, no return)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
func sayHello() { print("Hello, Hyderabad! 🌶️") } // Call it sayHello() // prints: Hello, Hyderabad! 🌶️ |
Form 2 – With parameters (most common)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
func greet(name: String) { print("Namaste, \(name)! 👋") } greet(name: "Rahul") // Namaste, Rahul! 👋 greet(name: "Priya") // Namaste, Priya! 👋 |
Important: In Swift, when you call the function, you must label the arguments (unless you use _ — more later).
Form 3 – With return value
|
0 1 2 3 4 5 6 7 8 9 10 11 |
func add(_ a: Int, _ b: Int) -> Int { return a + b } let sum = add(7, 8) // 15 print(sum) |
Shorthand return (very common & clean):
|
0 1 2 3 4 5 6 7 8 9 10 |
func multiply(_ a: Int, _ b: Int) -> Int { a * b // implicit return — no `return` keyword needed } print(multiply(6, 7)) // 42 |
Form 4 – Multiple parameters + labels
|
0 1 2 3 4 5 6 7 8 9 10 |
func introduce(name: String, age: Int, city: String) { print("\(name) is \(age) years old and lives in \(city).") } introduce(name: "Sneha", age: 24, city: "Hyderabad") |
You can make some labels external and some internal:
|
0 1 2 3 4 5 6 7 8 9 10 |
func greet(person name: String, from city: String) { print("Namaste \(name)! Greetings from \(city)!") } greet(person: "Aarav", from: "Bengaluru") |
3. Real-life examples — functions you will actually write
Example 1 – Format currency (very common)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
func formatIndianCurrency(_ amount: Double) -> String { let formatter = NumberFormatter() formatter.numberStyle = .currency formatter.currencyCode = "INR" formatter.currencySymbol = "₹" formatter.maximumFractionDigits = 2 return formatter.string(from: NSNumber(value: amount)) ?? "₹0.00" } let price = 12499.99 print("Price: \(formatIndianCurrency(price))") // Price: ₹12,499.99 |
Example 2 – Validate email (very common in forms)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
func isValidEmail(_ email: String) -> Bool { let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" let emailPredicate = NSPredicate(format: "SELF MATCHES %@", emailRegex) return emailPredicate.evaluate(with: email) } let testEmails = [ "rahul@example.com", "priya@gmail.com", "invalid-email", "a@b.c", "test@company.co.in" ] for email in testEmails { print("\(email): \(isValidEmail(email) ? "valid" : "invalid")") } |
Example 3 – Calculate discount (business logic)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
func calculateFinalPrice( basePrice: Double, quantity: Int, discountPercent: Double = 0, taxRate: Double = 0.18 ) -> Double { let subtotal = basePrice * Double(quantity) let discountAmount = subtotal * (discountPercent / 100) let priceAfterDiscount = subtotal - discountAmount let taxAmount = priceAfterDiscount * taxRate return priceAfterDiscount + taxAmount } let final = calculateFinalPrice( basePrice: 1499.99, quantity: 2, discountPercent: 10, taxRate: 0.18 ) print("Final amount: ₹\(String(format: "%.2f", final))") |
Example 4 – Filter active users (very common in social / admin apps)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
struct User { let id: Int let name: String let isActive: Bool let lastActive: Date? } func activeUsers(from users: [User], since daysAgo: Int = 30) -> [String] { let cutoff = Date().addingTimeInterval(-Double(daysAgo * 24 * 60 * 60)) return users .filter { $0.isActive || ($0.lastActive ?? .distantPast) >= cutoff } .map { $0.name } } let users = [ User(id: 1, name: "Rahul", isActive: true, lastActive: nil), User(id: 2, name: "Priya", isActive: false, lastActive: Date().addingTimeInterval(-15*24*60*60)), User(id: 3, name: "Aarav", isActive: false, lastActive: Date().addingTimeInterval(-45*24*60*60)) ] let activeNames = activeUsers(from: users) print("Active users:", activeNames) |
5. Very Common Beginner Mistakes & Correct Habits
| Mistake | Wrong / Risky code | Correct / Better habit | Why? |
|---|---|---|---|
| Forgetting to return value | func add(a: Int, b: Int) -> Int { a + b } | return a + b or use implicit return | Compile error if missing |
| Using var for parameters unnecessarily | func greet(name: String) { name = “Hello” + name } | Parameters are let by default — good! | Prevents accidental mutation |
| Force-unwrapping in functions | func getName(user: User?) -> String { user!.name } | guard let user else { return “Guest” } | Safer, better error handling |
| Too many parameters | func createUser(name, age, city, email, phone, …) | Use struct / DTO instead | More readable, easier to extend |
| Side-effects in pure functions | func calculateTotal(price: Double) -> Double { print(“Calculating…”); return price * 1.18 } | Keep functions pure when possible | Easier to test, predict |
6. Quick Reference – Function patterns you will use most
| Goal | Most idiomatic style | Notes / Tip |
|---|---|---|
| Simple action (no return) | func log(message: String) { print(message) } | Side-effect functions |
| Pure calculation | func add(_ a: Int, _ b: Int) -> Int { a + b } | Implicit return, no return keyword |
| Optional return | func findUser(id: Int) -> User? { … } | Return nil when not found |
| Multiple outputs | func divide(_ a: Double, by b: Double) -> (quotient: Double, remainder: Double) | Use tuple or custom struct |
| Default parameters | func greet(name: String = “Guest”) { … } | Very common for optional arguments |
| Throwing function | func loadData() throws -> Data { … } | Use try, do-catch when calling |
7. Small Practice – Try these
- Write a function greetUser(name: String, age: Int) that prints: “Namaste [name]! You are [age] years old.”
- Write a function calculateDiscountedPrice(original: Double, discountPercent: Double = 10) -> Double
- Write a function isStrongPassword(_ password: String) -> Bool that checks:
- length ≥ 8
- has at least one uppercase
- has at least one number
Paste your code here if you want feedback or want to see even cleaner versions!
What would you like to explore next?
- Functions with default parameters & variadic parameters (…)
- Throwing functions (throws, try, do-catch)
- Function types & closures (very important)
- Nested functions & higher-order functions
- Functions in SwiftUI (action closures, view builders)
- Or move to another topic (optionals, arrays, switch, loops…)
Just tell me — we’ll continue in the same clear, detailed, patient style 😊
