What if you could write instructions once and use them forever without repeating yourself?
Why Function declaration syntax in Swift? - Purpose & Use Cases
Imagine you want to tell your friend how to make a sandwich step by step every time they ask. You have to repeat the same instructions over and over, which gets tiring and confusing.
Repeating instructions manually wastes time and can cause mistakes. If you forget a step or change the recipe, you have to explain everything again from scratch.
With function declaration syntax, you write the instructions once as a function. Then you just call the function whenever you want to make a sandwich, saving time and avoiding errors.
print("Step 1: Get bread") print("Step 2: Add peanut butter") print("Step 3: Add jelly") print("Step 4: Close sandwich")
func makeSandwich() {
print("Step 1: Get bread")
print("Step 2: Add peanut butter")
print("Step 3: Add jelly")
print("Step 4: Close sandwich")
}
makeSandwich()It lets you organize your code clearly and reuse instructions easily, making programming faster and less error-prone.
When building an app, you might need to show a welcome message many times. Instead of writing the message code repeatedly, you create a function once and call it whenever needed.
Functions let you write instructions once and reuse them.
This saves time and reduces mistakes.
Function declaration syntax is the way to create these reusable instructions.