Using Default Parameter Values in Swift Functions
📖 Scenario: You are creating a simple greeting app that can say hello to users. Sometimes, users provide their name, and sometimes they don't. You want the app to greet users properly in both cases.
🎯 Goal: Build a Swift function that uses default parameter values to greet users by name or with a generic greeting if no name is given.
📋 What You'll Learn
Create a function called
greet that takes one parameter name of type String.Set a default value for the
name parameter to "Guest".Call the
greet function twice: once without any argument and once with the argument "Alice".Print the greeting message inside the
greet function.💡 Why This Matters
🌍 Real World
Default parameter values are useful in apps where some inputs are optional, like greeting users who may or may not provide their names.
💼 Career
Understanding default parameters helps you write cleaner, more flexible functions, a skill important for Swift developers building user-friendly apps.
Progress0 / 4 steps